C# 做winform,在程序里选择文件(比如说word文档或图片),就直接调用对应程序打开该文件,如何实现啊!

来源:百度知道 编辑:UC知道 时间:2024/05/13 16:41:48
如题,急等!

可以调用cmd来实现
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd";
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.CreateNoWindow = false; cmd.Start();
cmd.StandardInput.WriteLine(@"d:\a.doc"); //这里可以换成从文件对话框取得文件名
cmd.Close();

用默认程序打开文件
private void OpenFiles()
{
string fileName = "";//文件名

//打开文件对话框
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
fileName = ofd.FileName;
try
{
if (fileName != "" && System.IO.File.Exists(fileName))
{
System.Diagnostics.Process.Start(fileName);
}
}
catch (System.Exception e)