Open txt file from C# application

老子叫甜甜 提交于 2019-12-12 11:34:51

问题


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:


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



回答2:


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.




回答3:


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



回答4:


set UseShellExecute = true

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




回答5:


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



回答6:


Try this:

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



回答7:


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



回答8:


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


来源:https://stackoverflow.com/questions/420484/open-txt-file-from-c-sharp-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!