Пример #1
0
        /// <summary>
        /// 同步方式执行DOS命令
        /// </summary>
        /// <param name="command">DOS命令</param>
        public static void ExecuteCommandSync(object command)
        {
            try
            {
                // create the ProcessStartInfo using "cmd" as the program to be run,
                // and "/c " as the parameters.
                // Incidentally, /c tells cmd that we want it to execute the command that follows,
                // and then exit.
                System.Diagnostics.ProcessStartInfo procStartInfo =
                    new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);

                // The following commands are needed to redirect the standard output.
                // This means that it will be redirected to the Process.StandardOutput StreamReader.
                procStartInfo.RedirectStandardOutput = true;
                procStartInfo.UseShellExecute        = false;
                // Do not create the black window.
                procStartInfo.CreateNoWindow = true;
                // Now we create a process, assign its ProcessStartInfo and start it
                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                proc.StartInfo = procStartInfo;
                proc.Start();
                // Get the output into a string
                string result = proc.StandardOutput.ReadToEnd();
                // Display the command output.
                Console.WriteLine(result);
            }
            catch (Exception objException)
            {
                LogTextHelper.Error(objException);
                // Log the exception
            }
        }
Пример #2
0
 /// <summary>
 /// 异步方式执行DOS命令
 /// </summary>
 /// <param name="command">DOS命令字符串</param>
 public static void ExecuteCommandAsync(string command)
 {
     try
     {
         //Asynchronously start the Thread to process the Execute command request.
         Thread objThread = new Thread(new ParameterizedThreadStart(ExecuteCommandSync));
         //Make the thread as background thread.
         objThread.IsBackground = true;
         //Set the Priority of the thread.
         objThread.Priority = ThreadPriority.AboveNormal;
         //Start the thread.
         objThread.Start(command);
     }
     catch (ThreadStartException objException)
     {
         LogTextHelper.Error(objException);
         // Log the exception
     }
     catch (ThreadAbortException objException)
     {
         LogTextHelper.Error(objException);
         // Log the exception
     }
     catch (Exception objException)
     {
         LogTextHelper.Error(objException);
         // Log the exception
     }
 }
Пример #3
0
 /// <summary>
 /// 获取服务启动类型 2为自动 3为手动 4 为禁用
 /// </summary>
 /// <param name="serviceName"></param>
 /// <returns></returns>
 public static string GetServiceStartType(string serviceName)
 {
     try
     {
         RegistryKey regist            = Registry.LocalMachine;
         RegistryKey sysReg            = regist.OpenSubKey("SYSTEM");
         RegistryKey currentControlSet = sysReg.OpenSubKey("CurrentControlSet");
         RegistryKey services          = currentControlSet.OpenSubKey("Services");
         RegistryKey servicesName      = services.OpenSubKey(serviceName, true);
         return(servicesName.GetValue("Start").ToString());
     }
     catch (Exception ex)
     {
         LogTextHelper.Error(ex);
         return(string.Empty);
     }
 }
Пример #4
0
 /// <summary>
 /// 修改服务的启动项 2为自动,3为手动
 /// </summary>
 /// <param name="startType"></param>
 /// <param name="serviceName"></param>
 /// <returns></returns>
 public static bool ChangeServiceStartType(int startType, string serviceName)
 {
     try
     {
         RegistryKey regist            = Registry.LocalMachine;
         RegistryKey sysReg            = regist.OpenSubKey("SYSTEM");
         RegistryKey currentControlSet = sysReg.OpenSubKey("CurrentControlSet");
         RegistryKey services          = currentControlSet.OpenSubKey("Services");
         RegistryKey servicesName      = services.OpenSubKey(serviceName, true);
         servicesName.SetValue("Start", startType);
     }
     catch (Exception ex)
     {
         LogTextHelper.Error(ex);
         return(false);
     }
     return(true);
 }
Пример #5
0
        /// <summary>
        /// 附件的BASE64编码字符串
        /// </summary>
        /// <param name="path">附件路径</param>
        private string AttachmentB64Str(string path)
        {
            FileStream fs;

            try
            {
                fs = new FileStream(path, System.IO.FileMode.Open, FileAccess.Read, FileShare.Read);
            }
            catch (Exception ex)
            {
                errmsg += "要附加的文件不存在" + enter;
                LogTextHelper.Error(errmsg, ex);

                return(Base64Encode("要附加的文件:" + path + "不存在"));
            }
            int fl = (int)fs.Length;

            byte[] barray = new byte[fl];
            fs.Read(barray, 0, fl);
            fs.Close();
            return(B64StrLine(Convert.ToBase64String(barray)));
        }
Пример #6
0
 /// <summary>
 /// 停止服务
 /// </summary>
 /// <param name="serviseName"></param>
 /// <returns></returns>
 public static bool StopService(string serviseName)
 {
     try
     {
         ServiceController service = new ServiceController(serviseName);
         if (service.Status == ServiceControllerStatus.Stopped)
         {
             return(true);
         }
         else
         {
             TimeSpan timeout = TimeSpan.FromMilliseconds(1000 * 10);
             service.Stop();
             service.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
         }
     }
     catch (Exception ex)
     {
         LogTextHelper.Error(ex);
         return(false);
     }
     return(true);
 }