我如何在c#中打开文件?我的意思不是通过textreader和readline()来阅读它.我的意思是在记事本中将其作为独立文件打开.
Are*_*ren 182
你需要System.Diagnostics.Process.Start().
最简单的例子:
Process.Start("notepad.exe", fileName);
Run Code Online (Sandbox Code Playgroud)
更通用的方法:
Process.Start(fileName);
Run Code Online (Sandbox Code Playgroud)
第二种方法可能是更好的做法,因为这将导致Windows Shell使用它的关联编辑器打开您的文件.此外,如果指定的文件没有关联,它将使用Open With...Windows中的对话框.
请注意评论中的那些,谢谢你的意见.我的快速回答略有偏差,我已经更新了答案以反映正确的方法.
Col*_*ard 26
这将使用默认的Windows程序打开文件(如果你没有更改它,记事本);
Process.Start(@"c:\myfile.txt")
Run Code Online (Sandbox Code Playgroud)
Tim*_*hyP 26
您没有提供大量信息,但假设您想要使用为该文件类型的默认处理程序指定的应用程序打开计算机上的任何文件,您可以使用以下内容:
var fileToOpen = "SomeFilePathHere";
var process = new Process();
process.StartInfo = new ProcessStartInfo()
{
UseShellExecute = true,
FileName = fileToOpen
};
process.Start();
process.WaitForExit();
Run Code Online (Sandbox Code Playgroud)
UseShellExecute参数告诉Windows使用默认程序来处理您要打开的文件类型.
WaitForExit将使您的应用程序等待您关闭的应用程序关闭.
Vai*_*hav 15
System.Diagnostics.Process.Start( "notepad.exe", "text.txt");
Run Code Online (Sandbox Code Playgroud)
Ode*_*ded 12
您可以使用Process.Start,notepad.exe使用该文件作为参数进行调用.
Process.Start(@"notepad.exe", pathToFile);
Run Code Online (Sandbox Code Playgroud)