我在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#?
答案 0 :(得分:4)
应该是这样:
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)。
答案 1 :(得分:1)