简体   繁体   English

C# 为什么 Process.Start("notepad.exe" myFile) 正常工作,而 Process.Start("notepad++.exe" myFile) 不工作

[英]C# Why does Process.Start("notepad.exe" myFile) is working and Process.Start("notepad++.exe" myFile) is not working

The code, in both case is identical:两种情况下的代码都是相同的:

This is working and opening the text file in notepad这是工作并在记事本中打开文本文件

editor = "notepad.exe";
if (File.Exists(briefingFile))
{
  Process.Start(editor, briefingFile);
}

This one does is not work:这个是行不通的:

editor = "notepad++.exe";
if (File.Exists(briefingFile))
{
  Process.Start(editor, briefingFile);
}

It is the same test file and I have notepad++ installed.这是相同的测试文件,我安装了记事本++。 I Also tried to specify notepad++ with full path but the result is the same.我还尝试用完整路径指定记事本++,但结果是一样的。 Instead of opening notepad++ I get the attached error messages which tries to create new file or open missing files.我没有打开记事本++,而是收到了尝试创建新文件或打开丢失文件的附加错误消息。

第一个错误

我点击不我得到这个

The notepad.exe file is part of Windows and lives in the Windows folder, which is part of the default search path (an environment variable). notepad.exe文件是 Windows 的一部分,位于 Windows 文件夹中,该文件夹是默认搜索路径(环境变量)的一部分。 Notepad++.exe is not part of Windows, and so its home folder is not part of the default search path . Notepad++.exe不是Windows 的一部分,因此它的主文件夹不是默认搜索路径的一部分

Therefore, to open a process using Notepad++ you must also know the full path to the program.因此,要使用 Notepad++ 打开进程,您还必须知道程序的完整路径。

When trying the full path, make sure you escape the folder separator characters properly, and you must make sure to account for spaces in your path.尝试完整路径时,请确保正确转义文件夹分隔符,并且必须确保在路径中考虑空格。 In this case, the reason you see the C:\Program error is because you haven't yet accounted for the space in Program Files .在这种情况下,您看到C:\Program错误的原因是您尚未考虑Program Files中的空间。

editor = @"""C:\Program Files\Notepad++\notepad++.exe""";
try 
{
  Process.Start(editor, briefingFile);
}
catch(Exception ex)
{
   // Do something here
}

Also note how I switched to an exception handler instead of File.Exists() .还要注意我是如何切换到异常处理程序而不是File.Exists()的。 Disk I/O is one of those rare places where you should prefer handling the exception.磁盘 I/O 是您应该更喜欢处理异常的少数几个地方之一。 File.Exists() is particularly bad for this, and should be avoided . File.Exists()对此特别不利,应该避免


One other option here is if you have enough control for your target machines to know for sure Notepad++ is even installed, then you also have enough control register it as the default program for the files types your using, meaning you can skip selecting a program name at all:这里的另一个选择是,如果您有足够的控制权让您的目标机器知道甚至安装了 Notepad++,那么您也有足够的控制权将其注册为您使用的文件类型的默认程序,这意味着您可以跳过选择程序名称根本:

Process.Start(briefingFile);

The solution is to use double quotes in the text file , not in the application.解决方案是在文本文件中使用双引号,而不是在应用程序中。 In my case: change:就我而言:更改:

Process.Start(editor, briefingFile);

to

Process.Start(editor, $@"""{briefingFile}""");

The "editor" is a full path to the editor. “编辑器”是编辑器的完整路径。

There is nothing wrong with your code, although, as Joel stated, there are drawbacks to using File.Exists() .您的代码没有任何问题,尽管正如 Joel 所说,使用File.Exists()有一些缺点。 You should only need to make sure that the Notepad++ folder is in your User/System Environment PATH variables.您只需要确保 Notepad++ 文件夹位于您的用户/系统环境 PATH 变量中。 I added the path for my Notepad++ folder on this PC, which is just C:\Program Files\Notepad++\ , and ran the same code and it opens the file in Notepad++ just fine.我在这台 PC 上添加了我的 Notepad++ 文件夹的路径,即C:\Program Files\Notepad++\ ,并运行相同的代码,它在 Notepad++ 中打开文件就好了。

I ran the below code in an empty .NET 3.1 forms project and it can execute just fine.我在一个空的 .NET 3.1 forms 项目中运行了下面的代码,它可以执行得很好。 Do you still get your error in a new project?您还在新项目中遇到错误吗?

        OpenFileDialog fileDialog= new OpenFileDialog();
        fileDialog.Filter = "All Files (*.*)|*.*";
        fileDialog.FilterIndex = 1;
        string editor = "";
        if (fialDialog.ShowDialog() == DialogResult.OK)
        {
            editor  = fileDialog.FileName;
        }

        //Added the escaped quotes to the front & back of this in case the path contains spaces.
        var filePath = @"""C:\SOMELOCALFILEPATH\test.txt""";
        if (File.Exists(filePath))
        {
            Process.Start(editor, filePath);
        }

I set a break point and my editor is in this format when Process.Start executes: "C:\\Program Files\\Notepad++\\notepad++.exe"我设置了一个断点,当Process.Start执行时我的editor采用这种格式: "C:\\Program Files\\Notepad++\\notepad++.exe"

I ran into this issue and had also exactly same two errors when I tried to open a.txt file which was installed under C:\Program Files (x86)\MyProgram\SecondFolder\MyTxt.txt我遇到了这个问题,当我尝试打开安装在C:\Program Files (x86)\MyProgram\SecondFolder\MyTxt.txt下的 a.txt 文件时,我也遇到了完全相同的两个错误

The problem was when I copied the file path from windows explorer and pasted it into Visual Studio it deleted the blank space between Files and (x86) .问题是当我从 windows 资源管理器复制文件路径并将其粘贴到 Visual Studio 中时,它删除了Files(x86)之间的空白空间。

复制文件路径

粘贴到 Visual Studio 中的文件路径

happens when pasted directly into VS to edit it later.直接粘贴到 VS 以稍后编辑时发生。 Can be avoided if you paste the path into quotation marks如果将路径粘贴到引号中可以避免

You have to check if the file path is correct.您必须检查文件路径是否正确。 Otherwise it won't work.否则它将无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM