如何在 c # 中打开文件?我不是指通过 ttreader 和 readline ()来阅读它。我的意思是打开它作为一个独立的文件在记事本。
你需要 System.Diagnostics.Process.Start()。
System.Diagnostics.Process.Start()
最简单的例子:
Process.Start("notepad.exe", fileName);
更一般的方法:
Process.Start(fileName);
第二种方法可能是更好的做法,因为这将导致 Windows Shell 使用相关联的编辑器打开您的文件。此外,如果指定的文件没有关联,它将使用窗口中的 Open With...对话框。
Open With...
请注意那些在评论中,感谢您的投入。我的快速和肮脏的答案有点偏差,我已经更新了答案,以反映正确的方式。
这将打开文件与默认的窗口程序(记事本,如果你没有改变它) ;
Process.Start(@"c:\myfile.txt")
可以使用 Process.Start,将文件作为参数调用 notepad.exe。
Process.Start
notepad.exe
Process.Start(@"notepad.exe", pathToFile);
System.Diagnostics.Process.Start( "notepad.exe", "text.txt");
使用 系统,诊断,过程启动 Notepad.exe 的实例。
你没有提供很多信息, 但假设你想打开电脑上的任何文件 与为该文件类型的默认处理程序指定的应用程序一起, 你可以使用这样的东西:
var fileToOpen = "SomeFilePathHere"; var process = new Process(); process.StartInfo = new ProcessStartInfo() { UseShellExecute = true, FileName = fileToOpen }; process.Start(); process.WaitForExit();
UseShellExecute 参数告诉 Windows 对要打开的文件类型使用默认程序。
WaitForExit 将导致应用程序等待,直到您启动的应用程序已关闭。