2012-09-17 126 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 - 请参阅我的回答更新。 –