剪断这个长字符串的最佳方法

Best way to snip this long string

我想要 运行 一段代码,它会告诉我防火墙是打开还是关闭,用于 public 和专用网络。为此,我 运行 在 CMD 中使用 "netsh advfirewall show allprofiles" 命令,将输出放入一个字符串中,现在我想剪掉它,这样我就可以得到 public 的防火墙状态和私人。这是我将其保存在字符串中的代码:

TextBox TxtResult = new TextBox();
string Command = "netsh advfirewall show allprofiles";

System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + Command);

procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;

procStartInfo.CreateNoWindow = true;

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();

TxtResult.Text += proc.StandardOutput.ReadToEnd();

并给出以下字符串:

netsh advfirewall show allprofiles

Domain Profile Settings: 
----------------------------------------------------------------------
State                                 ON
Firewall Policy                       BlockInbound,AllowOutbound
LocalFirewallRules                    N/A (GPO-store only)
LocalConSecRules                      N/A (GPO-store only)
InboundUserNotification               Enable
RemoteManagement                      Disable
UnicastResponseToMulticast            Enable

Logging:
LogAllowedConnections                 Disable
LogDroppedConnections                 Disable
FileName                              %systemroot%\system32\LogFiles\Firewall\pfirewall.log
MaxFileSize                           4096


Private Profile Settings: 
----------------------------------------------------------------------
State                                 ON
Firewall Policy                       BlockInbound,AllowOutbound
LocalFirewallRules                    N/A (GPO-store only)
LocalConSecRules                      N/A (GPO-store only)
InboundUserNotification               Enable
RemoteManagement                      Disable
UnicastResponseToMulticast            Enable

Logging:
LogAllowedConnections                 Disable
LogDroppedConnections                 Disable
FileName                              %systemroot%\system32\LogFiles\Firewall\pfirewall.log
MaxFileSize                           4096


Public Profile Settings: 
----------------------------------------------------------------------
State                                 ON
Firewall Policy                       BlockInbound,AllowOutbound
LocalFirewallRules                    N/A (GPO-store only)
LocalConSecRules                      N/A (GPO-store only)
InboundUserNotification               Enable
RemoteManagement                      Disable
UnicastResponseToMulticast            Enable

Logging:
LogAllowedConnections                 Disable
LogDroppedConnections                 Disable
FileName                              %systemroot%\system32\LogFiles\Firewall\pfirewall.log
MaxFileSize                           4096

Ok.

将私人个人资料和 public 个人资料的状态 (on/off) 转换为 2 个变量的最佳方法是什么。我是否应该搜索单词 "state" 的第二个实例,删除它之前的所有内容,然后将接下来的 8 个左右的字母保存到一个字符串中?我不确定那有多可靠;任何建议表示赞赏。

解析它的最佳方法是使用正则表达式。尝试这样的事情:

Regex rx = new Regex(@"Private Profile Settings:[\s\S]*?State\s+(\w+)");
string status = rx.Match(text).Groups[1].Value;

()中的部分是您要查找的组,它有#1(#0是一个完整的匹配文本)-因此您可以在rx.Match表达式中引用它。

如果您要查找布尔值:

bool statusbool = string.Equals(status, "ON", StringComparison.InvariantCultureIgnoreCase);

你不应该这样做。它可能有效,但它不稳定。请记住,不是每个人都有英文系统设置。

也就是说,您最好使用 api 来检索您要查找的值。

来自Rod Stephens's Blog

// Create the firewall type.
Type FWManagerType = Type.GetTypeFromProgID("HNetCfg.FwMgr");

// Use the firewall type to create a firewall manager object.
dynamic FWManager = Activator.CreateInstance(FWManagerType);

// Check the status of the firewall.
bool enabled = FWManager.LocalPolicy.CurrentProfile.FirewallEnabled;

他还继续介绍了如何指定配置文件:

// Create consts for firewall types.
const int NET_FW_PROFILE2_DOMAIN = 1;
const int NET_FW_PROFILE2_PRIVATE = 2;
const int NET_FW_PROFILE2_PUBLIC = 4;

// Create the firewall type.
Type FWManagerType = Type.GetTypeFromProgID("HNetCfg.FwPolicy2");

// Use the firewall type to create a firewall manager object.
dynamic FWManager = Activator.CreateInstance(FWManagerType);

// Get the firewall settings.
bool CheckDomain =
    FWManager.FirewallEnabled(NET_FW_PROFILE2_DOMAIN);
bool CheckPrivate =
    FWManager.FirewallEnabled(NET_FW_PROFILE2_PRIVATE);
bool CheckPublic =
    FWManager.FirewallEnabled(NET_FW_PROFILE2_PUBLIC);

基于Retrieving Firewall Settings,您可以使用HNetCfg.FwPolicy2:

dynamic fwPolicy2 = Activator.CreateInstance(Type.GetTypeFromProgID("HNetCfg.FwPolicy2")); // as NetFwTypeLib.INetFwPolicy2;

bool DomainProfileIsOn  = fwPolicy2.FirewallEnabled[1]; // fwPolicy2.FirewallEnabled[NetFwTypeLib.NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_DOMAIN];
bool PrivateProfileIsOn = fwPolicy2.FirewallEnabled[2]; // fwPolicy2.FirewallEnabled[NetFwTypeLib.NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PRIVATE];
bool PublicProfileIsOn  = fwPolicy2.FirewallEnabled[4]; // fwPolicy2.FirewallEnabled[NetFwTypeLib.NET_FW_PROFILE_TYPE2_.NET_FW_PROFILE2_PUBLIC];

提前绑定,可以添加参考C:\Windows\System32\FirewallAPI.dll,使用评论中的代码。