我目前正在與reg.exe
合作,我正在創建reg.exe
作爲Process.FileName
的過程。使用DirectoryInfo時可以使用引號括起路徑的其他方法?
當我嘗試執行REG.EXE像下面
REG EXPORT HKLM\\SOFTWARE\\Intel\\IntelAMTUNS D:\\Backups\\Test.reg
寄託都工作正常。
但只要我嘗試這樣
REG EXPORT HKLM\\SOFTWARE\\Intel\\IntelAMTUNS D:\\Backups\\Backup folder 1\\Test.reg
沒有執行它發生 - 我知道爲什麼!目標路徑未放在引號中。只要我這樣做,一切再次正常工作。
我現在的問題是,我正在處理我所有的文件和文件夾路徑作爲DirectoryInfo
的實例。當我將帶引號的路徑作爲字符串傳遞時,例如這樣
DirectoryInfo targetFolder = new DirectoryInfo("\"D:\\Backups\\Backup folder 1\\Test.reg\"")
我即時收到一個異常告訴我,給定路徑的格式不支持。
有沒有什麼辦法把路徑放在引號中,仍然可以使用DirecotryInfo? 我真的需要把我的路徑放在引號中 - 否則命令將不起作用。
下面是一些示例代碼:
DirectoryInfo backupPath = new DirectoryInfo("D:\\Backups\\Backup folder 1\\Test.reg");
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "reg.exe";
startInfo.CreateNoWindow = true;
startInfo.Arguments = "REG EXPORT HKLM\\SOFTWARE\\Intel\\IntelAMTUNS " + backupPath.FullName;
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
當我運行這段代碼,什麼都不會發生 - 也沒有錯誤或異常。 .reg文件本身也不會創建。
當我嘗試這樣
DirectoryInfo backupPath = new DirectoryInfo("\"D:\\Backups\\Backup folder 1\\Test.reg\"");
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "reg.exe";
startInfo.CreateNoWindow = true;
startInfo.Arguments = "REG EXPORT HKLM\\SOFTWARE\\Intel\\IntelAMTUNS " + backupPath.FullName;
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
運行它,我得到一個System.NotSupportedException
告訴我「給出的路徑的格式不支持。」但實際上我需要把路徑用引號 - 否則,命令本身將無法正常工作......
Arn't你缺少的路徑關閉報價? – AssaultingCuccos
編輯並添加了收盤報價! –