4

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);

8 Answers8

11

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
  • 52,015
  • 16
  • 101
  • 139
3

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,026,079
  • 266
  • 2,566
  • 2,900
2

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
  • 102,548
  • 21
  • 159
  • 201
1

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
  • 122,218
  • 32
  • 205
  • 338
1

set UseShellExecute = true

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

bwknight877
  • 142
  • 1
  • 5
  • 1
    InvalidOperationException: The Process object must have the UseShellExecute property set to false in order to use environment variables. – Marc Gravell Jan 07 '09 at 14:36
0

Try this:

proc.StartInfo.Arguments = System.Environment.GetEnvironmentVariable("file");
brien
  • 4,400
  • 4
  • 30
  • 32
  • 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 Jan 07 '09 at 14:41
0

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
  • 103,016
  • 27
  • 158
  • 194
0

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
  • 54,393
  • 15
  • 113
  • 135