【问题标题】:Open a file in C# using Process.Start使用 Process.Start 在 C# 中打开文件
【发布时间】:2011-08-01 08:16:57
【问题描述】:

我正在编写一个程序来监视文件夹并让您知道何时创建了文件。当用户单击确定时,我正在努力打开文件。请给我一些关于如何让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");
}

【问题讨论】:

    标签: c# .net winforms process.start


    【解决方案1】:

    试试Process.Start("Notepad.exe", e.FullPath);

    【讨论】:

    • 最好不要依赖%PATH%等——直接指定路径
    【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-19
        相关资源
        最近更新 更多