C# 进程。开始为组合框列表引发win32异常
下面是一段代码,我已经用了大约两个星期了,我认为它一直在工作,直到我输入了最后一个信息(类MyClient),现在我在这个过程中遇到了win32错误; 表示找不到指定的文件。我尝试将其设置为“iexplorer.exe”以加载IE作为URL,但没有更改C# 进程。开始为组合框列表引发win32异常,c#,process.start,C#,Process.start,下面是一段代码,我已经用了大约两个星期了,我认为它一直在工作,直到我输入了最后一个信息(类MyClient),现在我在这个过程中遇到了win32错误; 表示找不到指定的文件。我尝试将其设置为“iexplorer.exe”以加载IE作为URL,但没有更改 public partial class Form1 : Form { List<MyClient> clients; public Form1() { InitializeComponent(
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; }
}
公共部分类表单1:表单
{
列出客户名单;
公共表格1()
{
初始化组件();
clients=新列表();
添加(新的MyClient{ClientName=“Client 1”,urldaddress=@)http://www.google.com" });
BindBigClientsList();
}
私有void BindBigClientsList()
{
BigClientsList.DataSource=客户端;
BigClientsList.DisplayMember=“ClientName”;
BigClientsList.ValueMember=“UrlAddress”;
}
private void BigClientsList\u SelectedIndexChanged(对象发送方,事件参数e)
{
MyClient c=BigClientsList。选择EdItem作为MyClient;
如果(c!=null)
{
字符串url=c.ClientName;
Process.Start(“iexplorer.exe”,url);
}
}
}
类MyClient
{
公共字符串ClientName{get;set;}
公共字符串URL地址{get;set;}
}
}您正在使用
ClientName
作为URL,这是不正确的
string url = c.ClientName;
…应该是
string url = c.UrlAddress;
您也不应该指定iexplorer.exe
。默认情况下,操作系统使用默认web浏览器打开URL。除非您真的需要您的用户使用Internet Explorer,否则我建议让系统为您选择浏览器
更新作为对OP评论的回应 这取决于你所说的“空白”是什么意思。如果您的意思是
null
,则这是不可能的。当您尝试调用c.urldAddress
时,使用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" });
谢谢,真不敢相信我忽略了一个简单的错误,比如在旁注上,你认为根据上面的代码,有可能在第一个组合框条目中插入一个空格,这样它就不会自动加载URL吗?@user1666884-请查看我答案的更新。
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
}
}