2012-09-17 50 views
0

下面是我已經使用了大約兩個星期的代碼,並且認爲我已經工作了,直到我放入最後一個信息(Class MyClient),現在我是在Process.Start(url)獲得win32錯誤; 說找不到指定的文件。我已經嘗試將其設置爲「iexplorer.exe」,讓它加載IE的URL,但沒有改變。Process.Start爲Combobox列表拋出win32異常

public partial class Form1 : Form 
{ 
    List<MyClient> clients; 
    public Form1() 
    { 
     InitializeComponent(); 
     clients = new List<MyClient>(); 
     clients.Add(new MyClient { ClientName = "Client 1", UrlAddress = @"http://www.google.com" }); 
     BindBigClientsList(); 
    } 

    private void BindBigClientsList() 
    { 
     BigClientsList.DataSource = clients; 
     BigClientsList.DisplayMember = "ClientName"; 
     BigClientsList.ValueMember = "UrlAddress"; 
    } 

    private void BigClientsList_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     MyClient c = BigClientsList.SelectedItem as MyClient; 
     if (c != null) 
     { 
      string url = c.ClientName; 
      Process.Start("iexplorer.exe",url); 
     } 
    } 
} 
class MyClient 
{ 
    public string ClientName { get; set; } 
    public string UrlAddress { get; set; } 
} 

}

回答

2

您正在使用ClientName的URL,這是不正確的......

string url = c.ClientName; 

...應該是...

string url = c.UrlAddress; 

您也不應指定iexplorer.exe。默認情況下,操作系統的開放URL與默認的網頁瀏覽器。除非您確實需要使用Internet Explorer的用戶,否則我建議讓系統爲您選擇瀏覽器。

UPDATE
在respose到OP的評論...

這取決於你的意思是 「空白」 的東西。如果你的意思是null,這是不可能的。當您嘗試撥打c.UrlAddress時,使用null作爲列表中的第一個條目將導致NullReferenceException。您可能能夠使用一個佔位符MyClient例如虛擬值...

clients = new List<MyClient>(); 
clients.Add(new MyClient { ClientName = "", UrlAddress = null }); 
clients.Add(new MyClient { ClientName = "Client 1", UrlAddress = @"http://www.google.com" }); 

但你必須改變你的操作方法是這樣的......

private void BigClientsList_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    MyClient c = BigClientsList.SelectedItem as MyClient; 
    if (c != null && !String.IsNullOrWhiteSpace(c.UrlAddress)) 
    { 
     string url = c.ClientName; 
     Process.Start("iexplorer.exe",url); 
    } 
    else 
    { 
     // do something different if they select a list item without a Client instance or URL 
    } 
} 
+0

謝謝,不能相信我忽略了這樣一個簡單的錯誤 – user1666884

+0

在附註中,你是否相信基於上面的代碼插入第一個組合框條目的空白是可能的,因此它不會自動加載URL? – user1666884

+0

@ user1666884 - 請參閱我的回答更新。 –