Open XML file button
I have a button in my winform app. I would like that when the user clicks this button it will open the xml file from c:\myfile.xml in visual studio.开发者_C百科 now i don't know where the user intalled vs and which version he is using. if this is impossible to know then how can I open it in notepad? the reason i'm not using webbrowser for this task is because the user needs to edit the content of the file.
I'm using c#.
Thanks.
string filePath = @"d:\test.xml";
//Open in notepad
System.Diagnostics.Process.Start("notepad", filepath);
//Open in visual studio
System.Diagnostics.Process.Start("devenv", filepath);
Note that this only works when the program can be found in PATH environment variable, you'll have to catch exceptions and try with other applications... something like this :
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))
{ }
}
精彩评论