C# 禁用本地连接和修改IE设置
已好多天没有写博客了,由于公司最近网络有改变,上网要算钱,晕拉,所有就找了上网卡来上网,但是用了无线,局域网就上不去了。
来回切网还有改代理设置有点麻烦,所有就写了个小软件,已便自己方便。
这个小软件主要有二点功能,1、是启用禁用网卡,根据名称来设置 2、通过修改注册表来改IE设置
现贴两段代码上来
1、启用禁用网卡,这个是网上找的
/// <summary>
/// 实现启用或停用本地网络链接
/// </summary>
/// <param name="netWorkName">本地连接名称</param>
/// <param name="operation">操作,传入“启用”或“停用”</param>
static bool NetWork(string netWorkName, string operation)
{
bool result = false;
Shell32.Shell shell = new Shell32.ShellClass();
Shell32.Folder folder = shell.NameSpace(49);
foreach (Shell32.FolderItem fi in folder.Items())
{
if (fi.Name != netWorkName)
continue;
Shell32.ShellFolderItem folderItem = (Shell32.ShellFolderItem)fi;
foreach (Shell32.FolderItemVerb fiv in folderItem.Verbs())
{
if (!fiv.Name.Contains(operation))
{
continue;
}
else
{
result = true;
fiv.DoIt();
Thread.Sleep(1000);
break;
}
}
}
return result;
}
/// 实现启用或停用本地网络链接
/// </summary>
/// <param name="netWorkName">本地连接名称</param>
/// <param name="operation">操作,传入“启用”或“停用”</param>
static bool NetWork(string netWorkName, string operation)
{
bool result = false;
Shell32.Shell shell = new Shell32.ShellClass();
Shell32.Folder folder = shell.NameSpace(49);
foreach (Shell32.FolderItem fi in folder.Items())
{
if (fi.Name != netWorkName)
continue;
Shell32.ShellFolderItem folderItem = (Shell32.ShellFolderItem)fi;
foreach (Shell32.FolderItemVerb fiv in folderItem.Verbs())
{
if (!fiv.Name.Contains(operation))
{
continue;
}
else
{
result = true;
fiv.DoIt();
Thread.Sleep(1000);
break;
}
}
}
return result;
}
2、修改注册表,改IE设置
//打开注册表键
Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true);
//设置自动脚本地址
rk.SetValue("AutoConfigURL", "");
txtMessage.Text += "IE自动脚本配置成功!"+"\r\n";
//设置代理可用
rk.SetValue("ProxyEnable", 0);
//设置代理IP和端口
rk.SetValue("ProxyServer", ConfigurationManager.ConnectionStrings["ProxyServer"].ConnectionString);
txtMessage.Text += "IE代理服务器配置成功!" + "\r\n";
rk.Close();
Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true);
//设置自动脚本地址
rk.SetValue("AutoConfigURL", "");
txtMessage.Text += "IE自动脚本配置成功!"+"\r\n";
//设置代理可用
rk.SetValue("ProxyEnable", 0);
//设置代理IP和端口
rk.SetValue("ProxyServer", ConfigurationManager.ConnectionStrings["ProxyServer"].ConnectionString);
txtMessage.Text += "IE代理服务器配置成功!" + "\r\n";
rk.Close();
比较简单只是为了自己方便。
一些设置是可以在config文件里配置的。