C# 根据服务名打开所在文件夹
using System.Management;
private string button_open_Click(object sender, EventArgs e)
{
try
{
string servicename = this.listView_serverlist.FocusedItem.SubItems[0].Text;
string RetString = string.Empty;
string query = "select PathName from Win32_Service where name ='" + servicename + "'";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject ob in searcher.Get())
{
RetString = ob["PathName"].ToString();
break;
}
searcher.Dispose();
return RetString; // RetString直接到文件名
// RetString = RetString.Substring(0, RetString.LastIndexOf('\\') + 1); // RetString 是目录
// Process.Start("explorer.exe", RetString); // 通过程序打开文件夹
}
catch (Exception)
{
}
}