Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

finding executable name

ddahlkeddahlke Member Posts: 2
I'm pulling my hair out, as usual with VS. All I want is the executable name of the program they are running from the command line to test if for security reasons. Apparently Environment.CommandLine has quotes (") around it, but I was able to get rid of them with Split. It also has the whole path name. I've looked into FileInfo class as well, and still not quite what I want. ie. if I have C:My Programsmy executablesother directoryMy Program.exe coming back from the above commandline variable, I want to somehow get "My Program" back as a string to compare in my database. I already know about about Product name as well, but spaces are exhanged with underscores. I'll still probably compare to that later as well, but for now, I'd just like to figure this out. I've looked through all the sites and books till my eyes are crossed. Anyone have any ideas? I would greatly appreciate any help.. Thanks in advance.

Doug Dahlke
Shields Bag & Printing
dfdahlke@shieldsbag.com

Comments

  • pingpongpingpong Member Posts: 937
    How about this:
    [code]
    // remove the "'s
    string commandLine = Environment.CommandLine.Replace(""", "");
    // use the Path class to extract the file name without extension
    Console.WriteLine(System.IO.Path.GetFileNameWithoutExtension(commandLine));
    [/code]

    Dont know if you need this, but System.Reflection.Assembly.Assembly.GetExecutingAssembly will return the original name of your EXE. You can use it to prevent people from renaming it. Something like:
    [code]
    System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
    string[] names = a.FullName.Split(',');

    string commandLine = Environment.CommandLine.Replace(""", "");
    string testName = System.IO.Path.GetFileNameWithoutExtension(commandLine);

    if(string.Compare(names[0], testName, true) != 0)
    {
    Console.WriteLine("Dont rename please");
    // exit program here
    }
    [/code]


  • ddahlkeddahlke Member Posts: 2
    Excellent! That's the same as I was trying to figure out how to do. We went down about a dozen different paths trying to find something like this. I wish MS would teach a class on just using their IDE tool more efficiently.. If they did, I'd probably take it. Perl's $0 is what I'm used to, but this is perfect. Thanks.

    Doug

    : How about this:
    : [code]
    : // remove the "'s
    : string commandLine = Environment.CommandLine.Replace(""", "");
    : // use the Path class to extract the file name without extension
    : Console.WriteLine(System.IO.Path.GetFileNameWithoutExtension(commandLine));
    : [/code]
    :
    : Dont know if you need this, but System.Reflection.Assembly.Assembly.GetExecutingAssembly will return the original name of your EXE. You can use it to prevent people from renaming it. Something like:
    : [code]
    : System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly();
    : string[] names = a.FullName.Split(',');
    :
    : string commandLine = Environment.CommandLine.Replace(""", "");
    : string testName = System.IO.Path.GetFileNameWithoutExtension(commandLine);
    :
    : if(string.Compare(names[0], testName, true) != 0)
    : {
    : Console.WriteLine("Dont rename please");
    : // exit program here
    : }
    : [/code]
    :
    :
    :

Sign In or Register to comment.