开发者

Running procmon.exe programmatically using C#

开发者 https://www.devze.com 2023-03-06 07:13 出处:网络
I have a code in AutoIt that runs procmon.exe programmatically. 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 ?

I have a code in AutoIt that runs procmon.exe programmatically. 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 ?

Code in AutoIt

{

Global $scriptDi开发者_如何学Pythonr = 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# ?


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. 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).


Have a look at the Process class. You can use the static method Start like this:

Process.Start(commandLine);

where commandLine is the string you use in your Run.

0

精彩评论

暂无评论...
验证码 换一张
取 消