简体   繁体   English

如何从C#运行msinfo32控制台命令?

[英]How to run msinfo32 console command from C#?

I would like to make a report from msinfo32 command to a nfo file in user's desktop folder. 我想从msinfo32命令向用户桌面文件夹中的nfo文件进行报告。 I run this exe directly because command msinfo32 sometimes is not in XP's PATH. 我直接运行该exe文件,因为有时msinfo32命令不在XP的PATH中。 So, this is what I would like from C#: 所以,这就是我想要的C#:

"C:\Program Files\Common Files\Microsoft Shared\MSInfo\msinfo32.exe" /nfo C:\Users\someUser\Desktop\my_pc.nfo

I have this code for now, it calls UAC and then the cmd window closes. 我现在有此代码,它调用UAC,然后cmd窗口关闭。 The file is not created. 未创建文件。 Why is this not working? 为什么这不起作用?

        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();

NB : MSInfo doesn't set an errorlevel. 注意 :MSInfo没有设置错误级别。

Your MSINFO32 command line doesn't quote the saved filename. 您的MSINFO32命令行未引用已保存的文件名。 So if it contains spaces it won't work. 因此,如果包含空格,它将无法正常工作。

For a completely unknown reason you are calling CMD even though you don't want it to do anything. 出于完全未知的原因,即使您不希望它执行任何操作,您仍在呼叫CMD。

You are using a unsupported way to elevate, it only works if the configuration of exe file association hasn't been changed. 您使用的是不支持的提升方式,只有在exe文件关联的配置未更改的情况下,它才有效。 You use a manifest to elevate. 您使用清单来提升。 See Run batch script as admin during Maven build 请参阅在Maven构建期间以管理员身份运行批处理脚本

Also see wmi as a program should be doing. 也可以将wmi看作是程序应该做的事情。 You can experiment with wmic command line tool. 您可以尝试使用wmic命令行工具。 Programs are for users not other programs. 程序面向用户,而非其他程序。

This is looking for wifi networks 这正在寻找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

This list services, 此清单服务,

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

Monitors 显示器

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

This waits for power events to occur and either kills or starts calculator. 这等待电源事件发生,并杀死或启动计算器。

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

From @CatCat's suggestion I managed to run this programm as admin. 根据@CatCat的建议,我设法以管理员身份运行此程序。 You'll want to modify the manifest that gets embedded in the program. 您将需要修改嵌入到程序中的清单。 This works on Visual Studio 2008 and higher: Project + Add New Item, select "Application Manifest File". 这适用于Visual Studio 2008和更高版本:Project + Add New Item,选择“ Application Manifest File”。 Change the <requestedExecutionLevel> element to: <requestedExecutionLevel>元素更改为:

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

The user gets the UAC prompt when they start the program. 用户在启动程序时收到UAC提示。 I concatenated Enviroment.SpecialFolder.Desktop with my other parameters to an process arugment and now this is working as I wanted. 我将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;
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM