从 C# 应用程序打开 txt 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/420484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 02:42:56  来源:igfitidea点击:

Open txt file from C# application

c#command-linecmdenvironment-variablesnotepad

提问by

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

下面的代码假设从我的 C# 应用程序打开 CMD 并打开文件 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

我试图将文件路径设置为环境变量,但是当记事本打开时,它会查找 %file%.txt 而不是 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);

回答by Joe

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.

一个明显的问题是您将 UseShellExecute 设置为 false。这意味着您是直接执行记事本,而不是通过命令 shell cmd.exe 传递。因此环境变量没有被扩展。

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

回答by bwknight877

set UseShellExecute = true

设置 UseShellExecute = true

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

这样它应该使用 cmd.exe 处理器来扩展 %file% 变量

回答by Marc Gravell

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

简短的版本是我怀疑你将不得不更直接地传递 arg,即

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

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

虽然你可以设置环境变量(进程使用),但我认为你不能在进程启动时使用它们。

回答by BFree

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:

你想用 %file% 完成什么?notepad.exe 的命令行参数是您要打开的文件。你需要做这样的事情:

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

回答by brien

Try this:

尝试这个:

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

回答by kgiannakakis

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

也许它与 StartInfo.Arguments 的工作方式有关。如果你找不到更好的东西,这对我有用:

proc.StartInfo.FileName = "cmd";
proc.StartInfo.Arguments="/c notepad %my_file%";

回答by GvS

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

如果您的目的是使用 .txt 文件启动编辑器(如问题标题所述),请使用:

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

回答by Nick Berardi

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

我敢打赌你需要设置WorkingDirectory才能让它工作。 NOTEPAD.exe通常位于%SYSTEMROOT% (C:\windows) 中,但默认为%SYSTEMROOT%\system32。试试下面的。

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