views:

4987

answers:

10

The following code is suppose to open CMD from my C# application and open the file text.txt.

I tried to set the file path as an environment variable but when notepad opens it looks for %file%.txt instead of text.txt

Any idea why?

System.Diagnostics.Process proc = new System.Diagnostics.Process();
        proc.EnableRaisingEvents=false;
        proc.StartInfo.EnvironmentVariables.Add("file", "c:\\text.txt");
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.FileName = "notepad";

        proc.StartInfo.Arguments="%file%";
        proc.Start();
        proc.WaitForExit();

        Console.WriteLine(proc.ExitCode);
+1  A: 

One obvious problem is that you have UseShellExecute set false. This means you are executing notepad directly without passing via the command shell cmd.exe. Therefore environment variables aren't being expanded.

I'm not sure what you're trying to achieve (why do you need to add an environment variable?) but the following would work:

    System.Diagnostics.Process proc = 
        new System.Diagnostics.Process(); 
    proc.EnableRaisingEvents = false; 
    proc.StartInfo.EnvironmentVariables.Add("file", "c:\\text.txt"); 
    proc.StartInfo.UseShellExecute = false; 
    proc.StartInfo.FileName = "cmd.exe";
    proc.StartInfo.Arguments = "/c notepad %file%"; 
    proc.Start(); 
    proc.WaitForExit();
Joe
+1  A: 

set UseShellExecute = true

that way it should use the cmd.exe processor to expand the %file% variable

bwknight877
InvalidOperationException: The Process object must have the UseShellExecute property set to false in order to use environment variables.
Marc Gravell
+3  A: 

The short version is that I suspect you are going to have to pass the arg more directly, i.e.

 proc.StartInfo.Arguments = @"""c:\text.txt""";

Although you can set environment variables (for use within the process), I don't think you can use them during the process start.

Marc Gravell
+1  A: 

What are you trying to accomplish with %file%? The command line argument for notepad.exe is the file you want to open. You need to do something like this:

proc.StartInfo.Arguments = "c:\\text.txt";
BFree
A: 

Try this:

proc.StartInfo.Arguments = System.Environment.GetEnvironmentVariable("file");
brien
That gets the environment variable from the *current* process; the OP wanted to define the var for the *new* process, and use it there.
Marc Gravell
A: 

Perhaps it has to do with how the StartInfo.Arguments work. If you can't find anything better, this worked for me:

proc.StartInfo.FileName = "cmd";
proc.StartInfo.Arguments="/c notepad %my_file%";
kgiannakakis
+2  A: 

If your purpose is to start the editor with a .txt file (like the title of the question says) just use:

Process.Start("C:\\text.txt")
GvS
A: 

Hi everyone....sorry but my anonymous username got cleared or something. Now I am a registered user and I would like to say that the answer provided by kgiannakakis is the one i was looking for since I still wanted to use the environment variables and not just open a file.

A: 

I am willing to bet you need to set WorkingDirectory to get this to work. NOTEPAD.exe is usually located in %SYSTEMROOT% (C:\windows) however the default is %SYSTEMROOT%\system32. Try out the below.

System.Diagnostics.Process proc = new System.Diagnostics.Process();
        proc.EnableRaisingEvents=false;
        proc.StartInfo.WorkingDirectory = "%SYSTEMROOT%";
        proc.StartInfo.EnvironmentVariables.Add("file", "c:\\text.txt");
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.FileName = "notepad";

        proc.StartInfo.Arguments="%file%";
        proc.Start();
        proc.WaitForExit();

        Console.WriteLine(proc.ExitCode);
Nick Berardi
A: 

Did you try out the code? - it doeas not work, looks for a new file.

Stumped