2012-06-07 24 views
4

我收到「系統找不到指定文件例外」在上的Process.Start TSCON系統無法找到文件過程開始(tscon.exe)指定的異常

工作:

Process.Start(new ProcessStartInfo(@"c:\Windows\System32\notepad.exe", "temp.txt")); 

不工作:

Process.Start(new ProcessStartInfo(@"c:\Windows\System32\tscon.exe", @"0 /dest:console")); 

我需要tscon.exe。 爲什麼我得到這個錯誤?

編輯:

  1. 證實tscon.exe確實是在c:\Windows\System32文件夾中。
  2. 我運行VS在管理員模式下

有沒有對文件中的一些硬化?不能理解這個...

+0

你確定'tscon.exe'在system32目錄下嗎?你確定你可以使用你的憑證訪問該軟件嗎? – Marco

+0

是的,我做到了。我不明白 – user829174

+0

嘗試使用「以管理員身份運行..」運行您的編譯的exe文件,看看它是不是權限。 – ericosg

回答

6

哦,這件事真的引起了我的注意。
我終於設法從Process.Start啓動tscon.exe。
您需要傳遞您的「管理員」帳戶信息,否則會出現「文件未找到」錯誤。

做這樣

ProcessStartInfo pi = new ProcessStartInfo(); 
pi.WorkingDirectory = @"C:\windows\System32"; //Not really needed 
pi.FileName = "tscon.exe"; 
pi.Arguments = "0 /dest:console"; 
pi.UserName = "steve"; 
System.Security.SecureString s = new System.Security.SecureString(); 
s.AppendChar('y'); 
s.AppendChar('o'); 
s.AppendChar('u'); 
s.AppendChar('r'); 
s.AppendChar('p'); 
s.AppendChar('a'); 
s.AppendChar('s'); 
s.AppendChar('s'); 
pi.Password = s; 
pi.UseShellExecute = false; 
Process.Start(pi); 

也看到該命令的結果更改以下兩行

pi.FileName = "cmd.exe"; 
pi.Arguments = "/k \"tscon.exe 0 /dest:console\""; 
+0

感謝您的努力,但是我收到了異常「」存根收到錯誤的數據「 我驗證了用戶名和密碼是正確的,我試圖把USERNAME或DOMAIN \ USERNAME都設置爲 – user829174

+0

但是我認爲這個錯誤來自TSCON,也許是另一個問題。嘗試我的第二個例子沒有參數''/ k \「tscon.exe /?\」「;' – Steve

0

雖然它看起來像你找到了一個解決辦法不久前,我有一個解釋至於爲什麼會出現這個問題以及可以說是更好的解決方案。我遇到了與shadow.exe相同的問題。

如果您使用Process Monitor進行觀察,由於File System Redirection和您的程序是32位,您將看到它實際上是在C:\ Windows \ SysWOW64 \而不是C:\ Windows \ system32 \中查找文件處理。

解決方法是編譯爲x64而不是任何CPU,或使用P/Invoke臨時懷疑並使用Wow64DisableWow64FsRedirection和Wow64RevertWow64FsRedirection Win API函數重新啓用文件系統重定向。

internal static class NativeMethods 
{ 
    [DllImport("kernel32.dll", SetLastError = true)] 
    public static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr); 

    [DllImport("kernel32.dll", SetLastError = true)] 
    public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr); 
} 

//////////////// 

IntPtr wow64backup = IntPtr.Zero; 
if (!Environment.Is64BitProcess && Environment.Is64BitOperatingSystem) 
{        
    NativeMethods.Wow64DisableWow64FsRedirection(ref wow64backup); 
} 

Process.Start(new ProcessStartInfo(@"c:\Windows\System32\tscon.exe", @"0 /dest:console")) 

if (!Environment.Is64BitProcess && Environment.Is64BitOperatingSystem) 
{ 
    NativeMethods.Wow64RevertWow64FsRedirection(wow64backup); 
} 
         } 
相關問題