2012-11-12 28 views
5

Process.Start không thể tìm thấy tệp hiện có

  • Tôi có đường dẫn của tệp thi hành (C:\Test\n4.TestConsole.exe).
  • File.Exists(path) trả về true.
  • File.OpenRead(path) mang lại cho tôi luồng của nó mà không có vấn đề gì.
  • Process.Start(path) ném một System.ComponentModel.Win32Exception với tin nhắn này:

    Hệ thống không thể tìm thấy các tập tin cụ thể.

Tôi đang làm gì sai?

Windows 8 Professional x64 - .NET Framework 4,5


Edit: Đây là mã.

public partial class Form1 : Form 
{ 
    public string Path { get; set; } 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     // I put a breakpoint here and verify the Path's value is 
     // C:\Test\n4.TestConsole.exe. 

     // File.Exists returns true. 
     MessageBox.Show(File.Exists(Path)); 

     // File.OpenRead doesn't throw an exception. 
     using (var stream = File.OpenRead(Path)) { } 

     // This throws the exception. 
     Process.Start(Path); 
    } 
} 
+0

loại của tập tin bạn đang cố gắng để thực hiện là gì? Có 'thực thi' không? Bạn có thể hiển thị giá trị của đường dẫn không? –

+0

@WouterdeKort: Ứng dụng bảng điều khiển. Nó mở ra và chờ đợi một đầu vào khi tôi nhấp đúp vào nó. Đường dẫn là: 'C: \ Test \ n4.TestConsole.exe' –

+0

Giá trị của đường dẫn là gì? Bạn cần sử dụng đường dẫn tệp đầy đủ nếu tệp không nằm trong System32 –

Trả lời

2

Đây có thể là tệp DLL bị thiếu hoặc phụ thuộc khác. Bạn có thể muốn so sánh biến môi trường PATH khi bạn chạy trực tiếp thông qua Process.Start(exe_path) và khi bạn chạy nó qua Process.Start("cmd", "/k " + exe_path).

1

Hãy thử điều này:

private void button1_Click(object sender, EventArgs e) 
{ 
    ProcessStartInfo psi = new ProcessStartInfo(); 
    psi.WorkingDirectory = @"C:\Test"; 
    psi.FileName = "n4.TestConsole.exe"; 
    Process.Start(psi); 
} 
Các vấn đề liên quan