/// <summary> /// 创建bat脚本 /// </summary> /// <param name="batName">文件名</param> /// <param name="fileContent">文件内容</param> /// <param name="u"></param> internal static void CreationBat(string batName, string fileContent, string u = null) { string filepath = System.IO.Directory.GetCurrentDirectory(); string batpath = filepath + @"" + batName; writeBATFile(fileContent, batpath); } /// <summary> /// 写入文件 /// </summary> /// <param name="fileContent">文件内容</param> /// <param name="filePath">路径</param> internal static void writeBATFile(string fileContent, string filePath) { if (!File.Exists(""" + filePath + """)) { FileStream fs1 = new FileStream(filePath, FileMode.Create, FileAccess.Write);//创建写入文件 StreamWriter sw = new StreamWriter(fs1); sw.WriteLine(fileContent);//开始写入值 sw.Close(); fs1.Close(); } else { FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Write); StreamWriter sr = new StreamWriter(fs); sr.WriteLine(fileContent);//开始写入值 sr.Close(); fs.Close(); } } /// <summary> /// 调用执行bat文件 /// </summary> /// <param name="batName">文件名</param> /// <param name="thisbatpath">路径</param> internal static void ExecuteBat(string batName, string thisbatpath) { Process proc = null; try { string targetDir = string.Format(thisbatpath);//this is where testChange.bat lies proc = new Process(); proc.StartInfo.WorkingDirectory = targetDir; proc.StartInfo.FileName = batName; proc.StartInfo.Arguments = string.Format("10");//this is argument proc.StartInfo.RedirectStandardError = false; proc.StartInfo.CreateNoWindow = true; proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//这里设置DOS窗口不显示,经实践可行 proc.Start(); proc.WaitForExit(); } catch (Exception ex) { Console.WriteLine("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace.ToString()); } } /// <summary> /// 调用执行vbs文件 /// </summary> /// <param name="vbsName">文件名</param> internal static void ExecuteVbs(string vbsName) { string path = System.IO.Directory.GetCurrentDirectory() + @"" + vbsName; ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "wscript.exe"; startInfo.Arguments = path; //Process.Start(startInfo); Process.Start(startInfo.FileName, """ + startInfo.Arguments + """); } /// <summary> /// 创建vbs文件 /// </summary> /// <param name="vbsName">vbs文件名</param> internal static void NewVbs(string vbsName) { string path = System.IO.Directory.GetCurrentDirectory() + @"" + vbsName; FileStream fsvbs = new FileStream(path, FileMode.Create, FileAccess.Write); StreamWriter runBat = new StreamWriter(path); runBat.WriteLine("set ws=WScript.CreateObject("WScript.Shell")"); runBat.WriteLine("ws.Run"runRdp.bat",0"); runBat.Close(); fsvbs.Close(); }