【问题标题】:Running procmon.exe programmatically using C#使用 C# 以编程方式运行 procmon.exe
【发布时间】:2011-05-16 01:25:14
【问题描述】:

我在 AutoIt 中有一个以编程方式运行 procmon.exe 的代码。现在我想将代码翻译成 C#,以便我可以通过 Microsoft Visual Studios 运行它,因此任何人都可以指导我完成它吗?

AutoIt 中的代码

{

Global $scriptDir = FileGetShortName(@ScriptDir)

Global $logDir = "C:\\log\\registry\\"

Global $date = @YEAR & @MON & @MDAY

Global $time = @HOUR & @MIN & @SEC

$ReadUsername = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\COM\Upload", "I")

Run("procmon.exe /LoadConfig " & $scriptDir 
    & "\\registrymonitoring.pmc /Quiet /AcceptEula /BackingFile "
    & $logDir & $ReadUsername & "-" & $date & "-" & $time, "", @SW_HIDE)

}

有什么建议将其翻译成 C# 吗?

【问题讨论】:

    标签: c# autoit


    【解决方案1】:

    应该是这样的:

    using System;
    using System.IO;
    using System.Diagnostics;
    using System.Text;
    using System.Runtime.InteropServices;
    using Microsoft.Win32;
    
    class Program
    {
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern int GetShortPathName(
            [MarshalAs(UnmanagedType.LPTStr)]
            string path,
            [MarshalAs(UnmanagedType.LPTStr)]
            StringBuilder shortPath,
            int shortPathLength
            );
    
        static void Main(string[] args)
        {
            string scriptDirLong = Directory.GetParent(Process.GetCurrentProcess().MainModule.FileName).FullName;
            StringBuilder scriptDir = new StringBuilder(255);
            GetShortPathName(scriptDirLong, scriptDir, 255);
    
            string logDir = @"C:\log\registry\";
            string date = System.DateTime.Now.ToString("yyyyMMdd");
            string time = System.DateTime.Now.ToString("HHmmss");
    
            string ReadUsername = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\COM\Upload", "I", null);
    
            Console.WriteLine(scriptDir + "\r\n" + logDir + "\r\n" + date + "\r\n" + time);
            Console.ReadKey();
    
            Process.Start("procmon.exe", 
                "/LoadConfig '" + scriptDir.ToString() + "\\registrymonitoring.pmc' /Quiet /AcceptEula /BackingFile " + 
                logDir + ReadUsername + "-" + date + "-" + time);
        }
    }
    

    我手头没有那个注册表项或 procmon,所以我依靠 Console.WriteLine 来查看它是正确的。我唯一不知道该怎么做的就是获取短名称,所以我只是导入了 winapi 函数并使用了它(取自 here)。

    【讨论】:

      【解决方案2】:

      看看Process 类。 您可以像这样使用静态方法 Start

      Process.Start(commandLine);

      其中 commandLine 是您在 Run 中使用的字符串。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-11-24
        • 1970-01-01
        • 1970-01-01
        • 2011-11-08
        • 1970-01-01
        • 2011-08-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多