I want to run a peice of code, and it tell me if my firewall is on or off, for public and private networks. To do this I'm running the "netsh advfirewall show allprofiles" command in CMD, putting the output into a string and now I want to snip it so I just get the status of the firewall for public and private. This is my code for saving it in a string:
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();
and that gives the following string:
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.
What would be the best way to get the state (on/off) for private profile and public profile into 2 variables. Should I search for the 2nd instance of the word "state", delete everything before it, and save the next 8 letters or so into a string? I'm not sure how reliable that is; any advice is appreciated.
The best way to parse it is using a regular expression. Try something like this:
Regex rx = new Regex(@"Private Profile Settings:[\s\S]*?State\s+(\w+)");
string status = rx.Match(text).Groups[1].Value;
The part in () is a group you are looking for, it has #1 (#0 is a whole matched text) - so you can refer to it in the rx.Match expression.
If you're looking for a boolean value:
bool statusbool = string.Equals(status, "ON", StringComparison.InvariantCultureIgnoreCase);