C# 如何停用本地连接和启用本地连接?
若以下回答无法解决问题,邀请你更新回答
1个回答
推荐于2016-09-18
展开全部
//禁用 SetNetworkAdapter(False)
//启用 SetNetworkAdapter(True)
//添加引用system32/shell32.dll
private static bool SetNetworkAdapter(bool status)
{
const string discVerb = "停用(&B)"; // "停用(&B)";
const string connVerb = "启用(&A)"; // "启用(&A)";
const string network = "网络连接"; //"网络连接";
const string networkConnection = "VMware Network Adapter VMnet1"; // "本地连接"
string sVerb = null;
if (status)
{
sVerb = connVerb;
}
else
{
sVerb = discVerb;
}
Shell32.Shell sh = new Shell32.Shell();
Shell32.Folder folder = sh.NameSpace(Shell32.ShellSpecialFolderConstants.ssfCONTROLS);
try
{
//进入控制面板的所有选项
foreach (Shell32.FolderItem myItem in folder.Items())
{
//进入网络连接
if (myItem.Name == network)
{
Shell32.Folder fd = (Shell32.Folder)myItem.GetFolder;
foreach (Shell32.FolderItem fi in fd.Items())
{
//找到本地连接
if ((fi.Name == networkConnection))
{
//找本地连接的所有右键功能菜单
foreach (Shell32.FolderItemVerb Fib in fi.Verbs())
{
if (Fib.Name == sVerb)
{
Fib.DoIt();
return true;
}
}
}
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
return true;
}
//启用 SetNetworkAdapter(True)
//添加引用system32/shell32.dll
private static bool SetNetworkAdapter(bool status)
{
const string discVerb = "停用(&B)"; // "停用(&B)";
const string connVerb = "启用(&A)"; // "启用(&A)";
const string network = "网络连接"; //"网络连接";
const string networkConnection = "VMware Network Adapter VMnet1"; // "本地连接"
string sVerb = null;
if (status)
{
sVerb = connVerb;
}
else
{
sVerb = discVerb;
}
Shell32.Shell sh = new Shell32.Shell();
Shell32.Folder folder = sh.NameSpace(Shell32.ShellSpecialFolderConstants.ssfCONTROLS);
try
{
//进入控制面板的所有选项
foreach (Shell32.FolderItem myItem in folder.Items())
{
//进入网络连接
if (myItem.Name == network)
{
Shell32.Folder fd = (Shell32.Folder)myItem.GetFolder;
foreach (Shell32.FolderItem fi in fd.Items())
{
//找到本地连接
if ((fi.Name == networkConnection))
{
//找本地连接的所有右键功能菜单
foreach (Shell32.FolderItemVerb Fib in fi.Verbs())
{
if (Fib.Name == sVerb)
{
Fib.DoIt();
return true;
}
}
}
}
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
return true;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询