2011-06-23 117 views
0

我在我的winform应用程序中有一个按钮。我希望当用户点击这个按钮时,它会从Visual Studio的c:\ myfile.xml中打开xml文件。现在我不知道用户所在的位置以及他正在使用哪个版本。 如果这是不可能知道的,那我该如何在记事本中打开它? 我不使用webbrowser进行此任务的原因是因为用户需要编辑文件的内容。打开XML文件按钮

我正在使用c#。

谢谢。

回答

2
string filePath = @"d:\test.xml"; 
//Open in notepad 
System.Diagnostics.Process.Start("notepad", filepath); 
//Open in visual studio 
System.Diagnostics.Process.Start("devenv", filepath); 

注意,当程序可以在PATH环境变量中找到这只作品中,你必须捕捉异常,并与其他应用程序尝试......是这样的:

bool TryStart(string application, string arguments) 
{ 
    try 
    { 
    using (Process.Start(application, arguments)) 
     return true; 
    } 
    catch (Win32Exception) 
    { 
    return false; 
    } 
    catch (FileNotFoundException) 
    { 
    return false; 
    } 
} 

void OpenXml(string filePath) 
{ 
    if (!TryStart("devenv", filePath) && !TryStart("notepad", filePath)) 
     using (Process.Start(filePath)) 
     { } 
} 
+0

完美! !谢谢! –