简体   繁体   English

使用C#以编程方式运行procmon.exe

[英]Running procmon.exe programmatically using C#

I have a code in AutoIt that runs procmon.exe programmatically. 我在AutoIt中有一个以编程方式运行procmon.exe的代码。 Now I want to translate the code into C# so that I can run it via Microsoft Visual Studios thus can anyone guide me to completing it ? 现在,我想将代码转换为C#,以便可以通过Microsoft Visual Studios运行它,因此有人可以指导我完成它吗?

Code in AutoIt 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)

}

Any advice is translating it to C# ? 任何建议将其翻译为C#吗?

This should be it: 应该是这样:

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

I don't have that registry key or procmon to hand so I'm relying on the Console.WriteLine to see it it's right. 我没有该注册表项或procmon,所以我依靠Console.WriteLine来查看它是正确的。 The only thing I couldn't figure out how to do was getting the short name, so I just imported the winapi function and used that (taken from here ). 我唯一不知道怎么做的就是获取短名称,所以我只是导入了winapi函数并使用了它(从此处获取 )。

Have a look at the Process class. 看一下Process类。 You can use the static method Start like this: 您可以使用静态方法启动,如下所示:

Process.Start(commandLine);

where commandLine is the string you use in your Run . 其中commandLine是您在Run中使用的字符串。

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

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