Пример #1
0
 public static void Using <T>(this T client, Action <T> work)
     where T : ICommunicationObject
 {
     try
     {
         work(client);
         client.Close();
     }
     catch (CommunicationException e)
     {
         LogTextHelper.WriteLine(e.ToString());
         client.Abort();
     }
     catch (TimeoutException e)
     {
         LogTextHelper.WriteLine(e.ToString());
         client.Abort();
     }
     catch (Exception e)
     {
         LogTextHelper.WriteLine(e.ToString());
         client.Abort();
         throw;
     }
 }
Пример #2
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
            }
        }
Пример #3
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
     }
 }
Пример #4
0
        /// <summary>
        /// 获取QueryString中的数据
        /// </summary>
        public static bool ValidUrlGetData()
        {
            bool result = false;

            for (int i = 0; i < HttpContext.Current.Request.QueryString.Count; i++)
            {
                result = HasInjectionData(HttpContext.Current.Request.QueryString[i].ToString());
                if (result)
                {
                    LogTextHelper.Info("检测出GET恶意数据: 【" + HttpContext.Current.Request.QueryString[i].ToString() + "】 URL: 【" + HttpContext.Current.Request.RawUrl + "】来源: 【" + HttpContext.Current.Request.UserHostAddress + "】");
                    break;
                }
            }
            return(result);
        }
Пример #5
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);
     }
 }
Пример #6
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);
 }
Пример #7
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)));
        }
Пример #8
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);
 }
Пример #9
0
        private static void InternalRegisterFileAssociations(
            bool unregister, string progId, bool registerInHKCU,
            string appId, string openWith, string[] extensions)
        {
            string Arguments = string.Format("{0} {1} {2} \"{3}\" {4} {5}",
                                             progId,         // 0
                                             registerInHKCU, // 1
                                             appId,          // 2
                                             openWith,
                                             unregister,
                                             string.Join(" ", extensions));

            try
            {
                Process(Arguments.Split(' '));
            }
            catch (Win32Exception e)
            {
                if (e.NativeErrorCode == 1223) // 1223: The operation was canceled by the user.
                {
                    LogTextHelper.Info("该操作已经被用户取消。");
                }
            }
        }