本文整理汇总了C#中System.IO.StreamWriter.CleanUpDefaultPortsInNetSh方法的典型用法代码示例。如果您正苦于以下问题:C# StreamWriter.CleanUpDefaultPortsInNetSh方法的具体用法?C# StreamWriter.CleanUpDefaultPortsInNetSh怎么用?C# StreamWriter.CleanUpDefaultPortsInNetSh使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.StreamWriter
的用法示例。
在下文中一共展示了StreamWriter.CleanUpDefaultPortsInNetSh方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetNetworkRequirements
public static bool SetNetworkRequirements(string Port, string FilePort, string OldPort, string OldFilePort)
{
string BatchFile = Path.Combine(System.IO.Path.GetTempPath(), "NetworkConfig.bat");
int exitCode = -1;
Process proc = new Process();
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = String.Format(@"/c {0}", BatchFile);
proc.StartInfo.Verb = "runas";
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.UseShellExecute = true;
try
{
StreamWriter BatchFileStream = new StreamWriter(BatchFile);
//Cleanup previous
try
{
BatchFileStream.CleanUpDefaultPortsInNetSh(new[] {int.Parse(OldPort), int.Parse(OldFilePort)});
BatchFileStream.WriteLine("netsh advfirewall firewall delete rule name=\"JMM Server - Client Port\"");
BatchFileStream.WriteLine("netsh advfirewall firewall delete rule name=\"JMM Server - File Port\"");
BatchFileStream.WriteLine(
[email protected]"netsh advfirewall firewall add rule name=""JMM Server - Client Port"" dir=in action=allow protocol=TCP localport={Port}");
Paths.ForEach(a => BatchFileStream.NetSh(a, "add", Port));
BatchFileStream.WriteLine(
[email protected]"netsh advfirewall firewall add rule name=""JMM Server - File Port"" dir=in action=allow protocol=TCP localport={FilePort}");
BatchFileStream.NetSh("", "add", FilePort);
}
finally
{
BatchFileStream.Close();
}
proc.Start();
proc.WaitForExit();
exitCode = proc.ExitCode;
proc.Close();
File.Delete(BatchFile);
if (exitCode != 0)
{
logger.Error("Temporary batch process for netsh and firewall settings returned error code: " + exitCode);
return false;
}
}
catch (Exception ex)
{
logger.Error(ex, ex.ToString());
return false;
}
logger.Info("Network requirements set.");
return true;
}