2015-09-05 76 views
-3

我正在制作一个visual studio的程序,我希望这个程序能够做到这一点,所以我需要代码来做到这一点,我不是一个专业的CS。 这里是我的批处理文件代码:我怎样才能将蝙蝠码转换为cs?

@ echo off 
color c 
IF "%OS%"=="Windows_NT" (
SET HOSTFILE=%windir%\system32\drivers\etc\hosts 
) ELSE (
SET HOSTFILE=%windir%\hosts 
) 

ECHO.>> %HOSTFILE% 
ECHO "my ip" download.talesrunner.com>> %HOSTFILE% 
IPCONFIG -flushdns 
CLS 
+1

你试过了吗? – Rodolfo

+0

不.. ..:|我想编辑这个文件夹中的主机文件,“c:\ windows \ system32 \ drivers \ etc” –

+0

@MahmoudFaour为您添加了完全翻译的代码! – cramopy

回答

0

这里是我翻译成包括颜色setment和使用安全Path.Combine方式。它现在完全从您的代码中翻译过来!

using System; 
using System.IO; 
using System.Linq; 
using System.Net; 
using System.Net.Sockets; 
using System.Runtime.InteropServices; 

namespace StackOverflow 
{ 
    class Program 
    { 
     [DllImport("dnsapi", EntryPoint = "DnsFlushResolverCache")] 
     private static extern uint DnsFlushResolverCache(); 

     static void Main(string[] args) 
     { 
      //string varibale for hostilfe 
      var HOSTFILE = ""; 

      //set to color c => red 
      Console.ForegroundColor = ConsoleColor.Red; 

      //Get OperatingSystem information from the system namespace. 
      var OSInfo = Environment.OSVersion; 

      //Determine the platform. 
      if (OSInfo.Platform == PlatformID.Win32NT) 
      { 
       //is windows NT 
       HOSTFILE = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), @"system32\drivers\etc\hosts"); 
      } 
      else 
      { 
       //is no windows NT 
       HOSTFILE = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "hosts"); 
      } 

      //print hostfile 
      Console.WriteLine(HOSTFILE); 

      //get ip address 
      IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName()); 
      var myIP = host.AddressList.FirstOrDefault(ip => ip.AddressFamily == AddressFamily.InterNetwork).ToString(); 

      //append newline & myip to hostfile 
      File.AppendAllLines(HOSTFILE, new[] { "", $"{myIP} download.talesrunner.com" }); 

      //if the above does not work because you don't have C# 6.0 use the following line 
      //File.AppendAllLines(HOSTFILE, new[] { "", string.Format("{0} download.talesrunner.com", myIP)}); 

      //flush dns cache 
      DnsFlushResolverCache(); 

      //wait for user or sth else unless window will close immediately 
      Console.ReadLine(); 
     } 
    } 
}