anonymous
anonymous

Reputation:

Open txt file from C# application

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

Upvotes: 4

Views: 24038

Answers (8)

Nick Berardi
Nick Berardi

Reputation: 54854

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

Upvotes: 0

GvS
GvS

Reputation: 52518

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

Upvotes: 11

to StackOverflow
to StackOverflow

Reputation: 124696

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

Upvotes: 1

kgiannakakis
kgiannakakis

Reputation: 104178

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%";

Upvotes: 0

brien
brien

Reputation: 4440

Try this:

proc.StartInfo.Arguments = System.Environment.GetEnvironmentVariable("file");

Upvotes: 0

BFree
BFree

Reputation: 103740

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

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1062780

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.

Upvotes: 3

bwknight877
bwknight877

Reputation: 142

set UseShellExecute = true

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

Upvotes: 1

Related Questions