C# 如何使用C为sql server express的windows防火墙添加异常#
我想使用C#为sql server express 2008 R2的windows防火墙添加例外 我该怎么做 情景:C# 如何使用C为sql server express的windows防火墙添加异常#,c#,sql-server-2008,firewall,C#,Sql Server 2008,Firewall,我想使用C#为sql server express 2008 R2的windows防火墙添加例外 我该怎么做 情景: 我有一个安装了Sql Server 2008 express R2数据库的应用程序 我想为防火墙添加例外,以便其他用户可以连接到数据库。(在sql express的配置文件中启用了TCP) 如何使用C#实现这一点,我希望在第一次启动应用程序时或在安装过程中使用自定义操作自动完成此操作。尝试此操作。它结束了对windows防火墙API的访问 我们应该做到这一点 编辑: 实际上这
- 我有一个安装了Sql Server 2008 express R2数据库的应用程序
- 我想为防火墙添加例外,以便其他用户可以连接到数据库。(在sql express的配置文件中启用了TCP)
1: INetFwOpenPorts ports;
2: INetFwOpenPort port;
3: port.Port = 1433; /* port no */
4: port.Name = “Application1”; /*name of the application using the port */
5: port.Enabled = true; /* enable the port */
6: /*other properties like Protocol, IP Version can also be set accordingly
7: now add this to the GloballyOpenPorts collection */
8:
9: Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
10: INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
11: ports = (INetFwOpenPorts)mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts;
12:
13: ports.Add(port);
14:
注意:在Visual Studio中,您需要向项目添加NetFwTypeLib COM
引用,并在项目中包括NetFwTypeLib(使用NetFwTypeLib;
)尝试此操作。它结束了对windows防火墙API的访问
我们应该做到这一点
编辑:
实际上这看起来更好-->
您应该能够使用此命令打开所需的端口
1: INetFwOpenPorts ports;
2: INetFwOpenPort port;
3: port.Port = 1433; /* port no */
4: port.Name = “Application1”; /*name of the application using the port */
5: port.Enabled = true; /* enable the port */
6: /*other properties like Protocol, IP Version can also be set accordingly
7: now add this to the GloballyOpenPorts collection */
8:
9: Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
10: INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
11: ports = (INetFwOpenPorts)mgr.LocalPolicy.CurrentProfile.GloballyOpenPorts;
12:
13: ports.Add(port);
14:
注意:在Visual Studio中,您需要向您的项目添加
NetFwTypeLib COM
引用,并在项目中包括NetFwTypeLib(使用NetFwTypeLib;
)在C中很容易做到这一点;在使用windows防火墙的其他简洁方法中,请参阅本文以了解更多信息
您还可以查看发布的答案。在C语言中可以很容易地做到这一点。除了windows防火墙的其他优点外,有关更多信息,请参阅本文
您还可以查看发布的答案。以下是一些选项: Windows XP SP2防火墙控制器 通过COM互操作使用C#控制Windows防火墙 Netsh防火墙上下文的Netsh命令语法
以下是一些选项: Windows XP SP2防火墙控制器 通过COM互操作使用C#控制Windows防火墙 Netsh防火墙上下文的Netsh命令语法
在这里,您可以通过C#为sql server 1433端口创建入站防火墙规则,该端口具有公共和私有多功能文件域 首先,您必须从系统“C:\Windows\System32/FirewallAPI.dll”导入一个dll。 将此DLL添加到项目引用中。 转到visual studio中的解决方案资源管理器->引用->添加引用->单击“确定”附近的“浏览”按钮,然后单击“取消”按钮->浏览路径C:\Windows\System32/FirewallAPI.dll并添加dll 添加dll后,在代码中使用名称空间“NetFwTypeLib”,如上面程序中所示。 使用NetFwTypeLib
using NetFwTypeLib;
namespace ConsoleAppTestDemo
{
class Program
{
static void Main(string[] args)
{
Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2);
// Let's create a new rule
INetFwRule2 inboundRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
inboundRule.Enabled = true;
//Allow through firewall
inboundRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
//For all profile
inboundRule.Profiles = (int)NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_ALL;
//Using protocol TCP
inboundRule.Protocol = 6; // TCP
//Local Port 1433
inboundRule.LocalPorts = "1433";
//Name of rule
inboundRule.Name = "SQLRule";
// Now add the rule
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.Rules.Add(inboundRule);
}
}
}
之后,您可以检查防火墙入站规则。
在这里,您可以通过C#为sql server 1433端口创建入站防火墙规则,该端口具有公共和私有多功能文件域 首先,您必须从系统“C:\Windows\System32/FirewallAPI.dll”导入一个dll。 将此DLL添加到项目引用中。 转到visual studio中的解决方案资源管理器->引用->添加引用->单击“确定”附近的“浏览”按钮,然后单击“取消”按钮->浏览路径C:\Windows\System32/FirewallAPI.dll并添加dll 添加dll后,在代码中使用名称空间“NetFwTypeLib”,如上面程序中所示。 使用NetFwTypeLib
using NetFwTypeLib;
namespace ConsoleAppTestDemo
{
class Program
{
static void Main(string[] args)
{
Type tNetFwPolicy2 = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");
INetFwPolicy2 fwPolicy2 = (INetFwPolicy2)Activator.CreateInstance(tNetFwPolicy2);
// Let's create a new rule
INetFwRule2 inboundRule = (INetFwRule2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FWRule"));
inboundRule.Enabled = true;
//Allow through firewall
inboundRule.Action = NET_FW_ACTION_.NET_FW_ACTION_ALLOW;
//For all profile
inboundRule.Profiles = (int)NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_ALL;
//Using protocol TCP
inboundRule.Protocol = 6; // TCP
//Local Port 1433
inboundRule.LocalPorts = "1433";
//Name of rule
inboundRule.Name = "SQLRule";
// Now add the rule
INetFwPolicy2 firewallPolicy = (INetFwPolicy2)Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2"));
firewallPolicy.Rules.Add(inboundRule);
}
}
}
之后,您可以检查防火墙入站规则。
Hi的可能重复,我正在尝试此示例代码,但收到消息Error 258使用未分配的局部变量“port”。你知道如何创建新的对象端口吗?我应该使用什么类?刚刚尝试了这个代码段类型Type=Type.GetTypeFromProgID(“HNetCfg.FWOpenPort”);port=Activator.CreateInstance(类型)作为INetFwOpenPort;不幸的是,我得到了NullReferenceException,尽管所有的局部变量都不是null。嗨,我正在尝试这个示例代码,但是我得到消息Error 258使用未分配的局部变量“port”。你知道如何创建新的对象端口吗?我应该使用什么类?刚刚尝试了这个代码段类型Type=Type.GetTypeFromProgID(“HNetCfg.FWOpenPort”);port=Activator.CreateInstance(类型)作为INetFwOpenPort;不幸的是,我得到的是NullReferenceException,尽管所有的局部变量都不是null。我刚刚尝试了来自的示例,得到的是exception UnauthorizedAccessException,访问被拒绝。(来自HRESULT:0x80070005(E_ACCESSDENIED)的异常)我已经解决了,我需要以管理员身份运行应用程序。我刚刚尝试了来自的示例,得到了异常UnauthorizedAccessException,访问被拒绝。(来自HRESULT的异常:0x80070005(E_ACCESSDENIED))我已经弄明白了,我需要以管理员身份运行应用程序。msdn论坛中的示例也可以很好地使用管理员权限,但是使用端口的示例更适合我。投票赞成。msdn论坛上的示例也可以很好地使用管理员权限,但是使用端口的示例更适合我。投票赞成。