我想从msinfo32命令向用户桌面文件夹中的nfo文件报告。我直接运行这个exe,因为命令msinfo32有时不在XP的路径中。所以,这就是我想从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();发布于 2018-09-27 05:56:00
根据@CatCat的建议,我成功地以管理员的身份运行了这个程序。您将希望修改嵌入到程序中的清单。此操作适用于Visual 2008及更高版本:项目+添加新项,选择“应用程序报表文件”。将<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;
}
}
}
}发布于 2018-09-20 07:04:05
NB: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
Loophttps://stackoverflow.com/questions/52418822
复制相似问题