Exemple #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.Debug(objException);
                // Log the exception
            }
        }
Exemple #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.Debug(objException);
         // Log the exception
     }
     catch (ThreadAbortException objException)
     {
         LogTextHelper.Debug(objException);
         // Log the exception
     }
     catch (Exception objException)
     {
         LogTextHelper.Debug(objException);
         // Log the exception
     }
 }
Exemple #3
0
        /// <summary>
        /// 初始化模板引擎
        /// </summary>
        protected virtual void InitTemplateEngine()
        {
            try
            {
                //Velocity.Init(NVELOCITY_PROPERTY);
                VelocityEngine templateEngine = new VelocityEngine();
                templateEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");

                templateEngine.SetProperty(RuntimeConstants.INPUT_ENCODING, "utf-8");
                templateEngine.SetProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8");

                //如果设置了FILE_RESOURCE_LOADER_PATH属性,那么模板文件的基础路径就是基于这个设置的目录,否则默认当前运行目录
                templateEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, AppDomain.CurrentDomain.BaseDirectory);

                templateEngine.Init();

                template = templateEngine.GetTemplate(templateFile);
            }
            catch (ResourceNotFoundException re)
            {
                string error = string.Format("Cannot find template " + templateFile);

                LogTextHelper.Debug(error);
                throw new Exception(error, re);
            }
            catch (ParseErrorException pee)
            {
                string error = string.Format("Syntax error in template " + templateFile + ":" + pee.StackTrace);
                LogTextHelper.Debug(error);
                throw new Exception(error, pee);
            }
        }
Exemple #4
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.Debug(ex);
         return(string.Empty);
     }
 }
Exemple #5
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.Debug(ex);
         return(false);
     }
     return(true);
 }
Exemple #6
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.Debug(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)));
        }
Exemple #7
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.Debug(ex);
         return(false);
     }
     return(true);
 }