How to use PATH environment variable for an application

Hi all,

I am not understanding the concept of PATH variable.

we are defining different path in this PATH variable. I am using one zip file and not specifying the path. See the ziputility variable in below code..

{

string arguments = @" -o " + ZipFileName;

string zipUtility = @"unzip.exe";

ProcessStartInfo startInfo = new ProcessStartInfo(zipUtility, arguments);

startInfo.CreateNoWindow = true;

startInfo.UseShellExecute = false;

startInfo.WindowStyle = ProcessWindowStyle.Hidden;

startInfo.RedirectStandardOutput = false;

startInfo.WorkingDirectory = directoryName;

Process proc = Process.Start(startInfo);

proc.WaitForExit();

}

i am not giving the path for unzip.exe file. so it should take from Environment.CurrentDirectory.. right

but in Environment.CurrentDirectory(C:\myProject\bin) also file is not there. still it's working for one application as in PATH (Environmen Variable) it's path(C:\Program Files\ZipUtility) is there.

But for another application with the same code it's giving error saying that file not found. Please Help me out in this.................................

HOW CAN I USE PATH ENVIRONMENT VARIABLE IN MY APPLICATION SO THAT APPLICATION ALSO LOOKS FOR THE PATH VARIABLE'S paths FOR EXECUTABLES




Answer this question

How to use PATH environment variable for an application

  • Burr Sutter

    Thanks Appel for reply.....

    I have seen that System.Diagnositic.Process does not lookup for the PATH variable if file not found in working directory.

    I have created 2-3 application for the testing and all are behaving the same fashion ie. they are not taking path from the PATH variable.

    So I m wondering do we need to write extra for that and as I told one appliation is taking the value from it. and that application is too huge to debug...

    so I am coming to u guys for help................

    My question is there in bold letters..........please reply..



  • supagu

    When using System.Diagnostics.Process it does lookup the executable on the PATH if not found in the working directory. What error do you get If Process.Start throws a Win32Exception with the message "The system cannot find the file specified", are you sure the application is in your PATH

  • Prabu.

    hmmm................

    Hey appel............... don't try the default exe's of windows.....................

    put one exe in c:\test folder. add that path to PATH variable and

    execute the same above program. then see what happens

    It will give an error message..................



  • hdp203

    And it works just fine. What I did was:
    1. Created folder c:\test
    2. Copied an application to c:\test
    3. Added c:\test to PATH using the System Properties dialog
    4. Restarted Visual Studio and opened the project that contained the code above (this step is important since environment variables are inherited when the process is started)
    5. Changed the filename from notepad.exe to the application I copied to c:\test
    6. Ran the application
    Are you sure that you've changed the PATH environment variable for your application when it fails You can verify this by checking the return value of



    Environment.GetEnvironmentVariable("PATH")



    using the quickwatch window for example.

  • Seiggy

    Hey appel..............

    this step is important since environment variables are inherited when the process is started)

    this is the point I wanted.............I got the solution now......

    Can you explain me the point in bold I mean to say Where can I get detailed information of the Process Loading.........I know what is process but  I don't know much about what other things happend while process is loaded..

     

    thanks..................

     



  • Koray Samsun

    Could you post a sample program that shows is behavior

    I tried the following and it worked just fine.



    using System;
    using System.Diagnostics;

    namespace ProcessStartPathTest
    {
    class Program
    {
    static void Main(string[] args)
    {
    ProcessStartInfo info = new ProcessStartInfo("notepad.exe");
    Process p = Process.Start(info);
    Console.ReadLine();
    }
    }
    }



  • How to use PATH environment variable for an application