Open txt file from C# application

anonymous picture anonymous · Jan 7, 2009 · Viewed 21.8k times · Source

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

Answer

GvS picture GvS · Jan 7, 2009

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