示例#1
0
        public static string GeServiceTag()
        {
            try
            {
                string ret = Win32_BIOSReader.Read().SerialNumber;

                if (ret != null)
                {
                    if (ret.Length > SERVICE_TAG_SIZE)
                    {
                        ret = ret.Substring(0, SERVICE_TAG_SIZE);
                    }
                    else
                    {
                        string strDefault = "1234567";
                        ret = ret + strDefault.Substring(0, SERVICE_TAG_SIZE - ret.Length);
                    }
                    return(ret);
                }
                else
                {
                    return("0000000");
                }
            }
            catch (Exception ex)
            {
                CommonTools.HandleException(ex);
                return("1234567");
            }
        }
示例#2
0
        public static bool GetInteger(int min, int max, out int ret)
        {
            if (max <= min)
            {
                ret = 0;
                return(false);
            }

            try
            {
                RNGCryptoServiceProvider gen = new RNGCryptoServiceProvider();
                byte[] rand = new byte[4];
                gen.GetBytes(rand);
                var v = BitConverter.ToInt32(rand, 0);

                v = (Math.Abs(v) % (max - min + 1));

                v  += min;
                ret = v;

                return(true);
            }
            catch (Exception ex)
            {
                CommonTools.HandleException(ex);
                ret = 0;
                return(false);
            }
        }
示例#3
0
        public static bool IsValidCredentialByLogonUser(string user, string password, out int errorCode)
        {
            bool isValid = false;

            errorCode = 0;

            IntPtr token = new IntPtr();

            try
            {
                if (true == LogonUser(user, mDomain, password, LogonTypes.Interactive, LogonProviders.Default, out token))
                {
                    isValid = true;
                }
                else
                {
                    errorCode = Marshal.GetLastWin32Error();
                    if (ERROR_ACCOUNT_RESTRICTION == errorCode)
                    {
                        isValid = true;
                    }
                }
            }
            catch (Exception ex)
            {
                CommonTools.HandleException(ex);
            }
            finally
            {
                CloseHandle(token);
            }

            return(isValid);
        }
示例#4
0
        public static bool OpenInDefaultApplication(string path, string arg = null)
        {
            bool bSuccess = false;

            try
            {
                if (File.Exists(path) && !IsUnsafeFileType(path))
                {
                    CommonTools.Log("[Opening " + @path + "].");
                    System.Diagnostics.Process.Start(path, arg);
                    bSuccess = true;
                    CommonTools.Log("[" + path + "] opened.");
                }
                else
                {
                    CommonTools.Log("[" + path + "] not found.");
                }
            }
            catch (Exception ex)
            {
                CommonTools.HandleException(ex);
            }

            return(bSuccess);
        }
示例#5
0
 public static string GetDisplayName()
 {
     try
     {
         return(System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName);
     }
     catch (Exception ex)
     {
         CommonTools.HandleException(ex);
         return(null);
     }
 }
示例#6
0
 public static string ToJson <T>(object obj)
 {
     try
     {
         JavaScriptSerializer json = new JavaScriptSerializer();
         string strJson            = json.Serialize(obj);
         return(strJson);
     }
     catch (Exception ex)
     {
         CommonTools.HandleException(ex);
         return(null);
     }
 }
示例#7
0
        public static T ParseJason <T>(string jason)
        {
            string ret = string.Empty;

            try
            {
                JavaScriptSerializer ser = new JavaScriptSerializer();
                return(ser.Deserialize <T>(jason));
            }
            catch (Exception ex)
            {
                CommonTools.HandleException(ex);
                return(default(T));
            }
        }
示例#8
0
        public static bool OpenURL(string url)
        {
            bool bSuccess = false;

            try
            {
                if (false == String.IsNullOrEmpty(url))
                {
                    CommonTools.Log("[Opening URL =>" + @url + "].");
                    System.Diagnostics.Process.Start(url);
                    bSuccess = true;
                }
            }
            catch (Exception ex)
            {
                CommonTools.HandleException(ex);
            }
            return(bSuccess);
        }
示例#9
0
        public static Rect GetGlobalBounds()
        {
            try
            {
                int left   = 10000;
                int top    = 10000;
                int bottom = 0;
                int right  = 0;

                foreach (System.Windows.Forms.Screen screen in System.Windows.Forms.Screen.AllScreens)
                {
                    if (screen.Bounds.Left < left)
                    {
                        left = screen.Bounds.Left;
                    }

                    if (screen.Bounds.Top < top)
                    {
                        top = screen.Bounds.Top;
                    }

                    if (screen.Bounds.Right > right)
                    {
                        right = screen.Bounds.Right;
                    }

                    if (screen.Bounds.Bottom > bottom)
                    {
                        bottom = screen.Bounds.Bottom;
                    }
                }

                Rect rect = new Rect(left, top, right - left, bottom - top);

                return(rect);
            }
            catch (Exception ex)
            {
                CommonTools.HandleException(ex);
                return(new Rect(0, 0, 0, 0));
            }
        }
示例#10
0
 public static bool LogOnUser(string user, string password)
 {
     try
     {
         int ret;
         if (LogonUtility.IsValidCredentialByLogonUser(user, password, out ret))
         {
             return(true);
         }
         else
         {
             return(false);
         }
     }
     catch (Exception ex)
     {
         CommonTools.HandleException(ex);
         return(false);
     }
 }
示例#11
0
        public static string GetMIME(string sRequestedFile)
        {
            String sFileExt = "";

            sRequestedFile = sRequestedFile.ToLower();
            int iStartPos = sRequestedFile.LastIndexOf(".") + 1;

            sFileExt = sRequestedFile.Substring(iStartPos);

            try
            {
                if (sFileExt.Equals("jpg") || sFileExt.Equals("JPG"))
                {
                    return("image/jpeg");
                }
                else if (sFileExt.Equals("mp4"))
                {
                    return("video/mp4");
                }
                else if (sFileExt.Equals("mp3"))
                {
                    return("audio/mpeg");
                }
                else if (sFileExt.Equals("3gp"))
                {
                    return("video/3gpp");
                }
                else
                {
                    return("");
                }
            }
            catch (Exception ex)
            {
                CommonTools.HandleException(ex);
            }
            return("");
        }
示例#12
0
        public static Bitmap Capture(int x, int y, int w, int h)
        {
            try
            {
                Bitmap bmpScreenCapture = new Bitmap(w, h);

                using (Graphics g = Graphics.FromImage(bmpScreenCapture))
                {
                    g.CopyFromScreen(x - w / 2,
                                     y - w / 2,
                                     0, 0,
                                     bmpScreenCapture.Size,
                                     CopyPixelOperation.SourceCopy);
                }

                return(bmpScreenCapture);
            }
            catch (Exception ex)
            {
                CommonTools.HandleException(ex);
                return(null);
            }
        }