使用Process.Start在C#中打开文件

3

我正在编写一个监控文件夹并告知用户有新文件创建的程序。但我在用户单击确定按钮时无法打开文件。请问如何让 Process.Start() 正常工作?我尝试从 e.Fullpath 中获取文件位置以加载记事本中的文本文件。

private void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e)
{
    DialogResult messageresult = MessageBox.Show("You have a Collection Form: " + e.Name);
    if (messageresult == DialogResult.OK)
        Process.Start("Notepad.exe", "e.FullPath");
}
3个回答

8
尝试使用 Process.Start("Notepad.exe", e.FullPath); 启动记事本。

1
最好不要依赖于%PATH%等环境变量,而是直接指定路径。 - abatishchev

6
Process.Start的第二个参数是一个字符串,但您正在传递一个字符串类型,因此您不需要在其周围使用引号。只有像第一个参数这样的字符串字面量需要引号。

3
string notepadPath = Path.Combine(Environment.SystemDirectory, "notepad.exe");
if (File.Exists(notepadPath))
    Process.Start(notepadPath, e.FullPath);
else
    throw new Exception("Can't locate Notepad");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接