用JAVA與C#程序調(diào)用DOS命令PK
1,、JAVA
package CmdTest;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader;
public class CmdTest { public static void main(String[] args) throws Throwable { // TODO Auto-generated method stub // 定義一個(gè)字符串用來(lái)輸出DOS命令的結(jié)果 String strCmd; // 用來(lái)執(zhí)行DOS命令 Process process = Runtime.getRuntime().exec("cmd /c netstat -an"); // 將命令的結(jié)果以流的方式讀入 BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(process.getInputStream())); // 將結(jié)果循環(huán)打印輸出 while ((strCmd = bufferedReader.readLine()) != null) { System.out.println(strCmd); } // 子線程結(jié)束后要?dú)⒌?br> process.destroy(); }
}
2、C#
/// <summary> /// 備份文件 /// </summary> /// <param name="scrPath"></param> /// <param name="desPath"></param>
internal static void CopyDirectory(string scrPath, string desPath) { System.Diagnostics.Process process = new System.Diagnostics.Process(); try { process.StartInfo.FileName = "XCopy"; process.StartInfo.Arguments = string.Format(" \"{0}\" \"{1}\" /E/H/Y/I", scrPath, desPath); process.StartInfo.CreateNoWindow = true; process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; process.Start(); process.WaitForExit();
} catch { throw; } finally { process.Dispose(); process = null;
} }
|