我想从msinfo32命令向用户桌面文件夹中的nfo文件进行报告。我直接运行该exe文件,因为命令msinfo32
有时不在XP的PATH中。所以,这就是我想要的C#代码:
"C:\Program Files\Common Files\Microsoft Shared\MSInfo\msinfo32.exe" /nfo C:\Users\someUser\Desktop\my_pc.nfo
我现在有此代码,它调用UAC,然后cmd窗口关闭。未创建文件。为什么这不起作用?
var proc1 = new ProcessStartInfo();
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string myFile = "my_pc.nfo";
string myFullPath = Path.Combine(desktopPath, myFile);
string myCommand = @"/C C:\Program Files\Common Files\Microsoft Shared\MSInfo\msinfo32.exe /nfo " + myFullPath;
proc1.UseShellExecute = true;
proc1.WorkingDirectory = @"C:\Windows\System32";
proc1.FileName = @"C:\Windows\System32\cmd.exe";
proc1.Verb = "runas";
char quote = '"';
proc1.Arguments = "/C " + quote + myCommand + quote;
proc1.WindowStyle = ProcessWindowStyle.Normal;
Process.Start(proc1);
Console.ReadLine();
答案 0 :(得分:1)
注意事项:MSInfo没有设置错误级别。
您的MSINFO32命令行未引用已保存的文件名。因此,如果包含空格,它将无法正常工作。
出于完全未知的原因,即使您不希望它执行任何操作,您仍在呼叫CMD。
您使用的升降方式不受支持,只有在exe文件关联的配置未更改的情况下,它才起作用。您使用清单来提升。参见Run batch script as admin during Maven build
也可以将wmi视为程序应该做的事情。您可以尝试使用wmic命令行工具。程序供用户使用,而不是其他程序。
这是在寻找wifi网络
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From WiFi_AvailableNetwork")
'msgbox colitems
For Each objItem in colItems
msgbox objItem.name & " " & objItem.Description
Next
此列表服务
Set objWMIService = GetObject("winmgmts:\\127.0.0.1\root\cimv2")
Set config = objWMIService.ExecQuery("Select * From Win32_Service")
For Each thing in Config
Msgbox thing.Caption
Next
显示器
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_DesktopMonitor")
For Each objItem in colItems
msgbox objItem.Model & " " & objItem.Manufacturer & " " & objItem.SerialNumber
Next
这会等待电源事件发生,并杀死或启动计算器。
Set colMonitoredEvents = GetObject("winmgmts:")._
ExecNotificationQuery("SELECT * FROM Win32_PowerManagementEvent")
Do
Set strLatestEvent = colMonitoredEvents.NextEvent
If strLatestEvent.EventType = 4 Then
Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")
For Each objItem in colItems
If objItem.name = "Calculator.exe" then objItem.terminate
Next
ElseIf strLatestEvent.EventType = 7 Then
wscript.sleep 2000
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "calc.exe", 1, false
End If
Loop
答案 1 :(得分:0)
根据@CatCat的建议,我设法以管理员身份运行此程序。您将需要修改嵌入到程序中的清单。这适用于Visual Studio 2008和更高版本:Project + Add New Item,选择“ Application Manifest File”。将<requestedExecutionLevel>
元素更改为:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
用户在启动程序时收到UAC提示。我将Enviroment.SpecialFolder.Desktop
和我的其他参数连接到流程的改进上,现在它可以按我的意愿工作。
using System;
using System.Diagnostics;
using System.ServiceProcess;
namespace WinTImeSync
{
class Program
{
static void Main(string[] args)
{
if (MsInfoReport() == true)
{
Console.WriteLine("Command ran successfully.");
}
else
{
Console.WriteLine("Did not run.");
}
Console.Write("Press any key to continue...");
Console.ReadKey();
}
public static bool MsInfoReport()
{
try
{
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Process processTime = new Process();
processTime.StartInfo.FileName = @"C:\Program Files\Common Files\microsoft shared\MSInfo\msinfo32.exe";
processTime.StartInfo.Arguments = "/report " + desktopPath + "\\mypc_info.nfo /categories +systemsummary";
processTime.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processTime.Start();
processTime.WaitForExit();
return true;
}
catch (Exception exception)
{
Trace.TraceWarning("unable to run msinfo32", exception);
return false;
}
}
}
}