Exemple #1
0
 /// <summary>
 /// 在DEBUG模式中顯示資訊(不顯示行號)
 /// </summary>
 /// <param name="strBugMsg"></param>
 /// <param name="args"></param>
 public static void jmsg(String strBugMsg, params Object[] args)
 {
     try
     {
         Debug.Write(String.Format(strBugMsg + "\n", args));
     }
     catch (Exception e)
     {
         //strBugMsg = strBugMsg.Replace(System.Environment.NewLine, string.Empty);
         //strBugMsg = strBugMsg.Trim();
         //strBugMsg = strBugMsg.Replace("{", "{{");
         //strBugMsg = strBugMsg.Replace("}", "}}");
         strBugMsg = CUtil.fixStringFormateError(strBugMsg);
         Debug.Write(String.Format(strBugMsg + ":" + e.ToString() + "\n", args));
     }
 }
        /// <summary>
        /// 清除程式占用的記憶體
        /// </summary>
        public static void clearMemory()
        {
            System.Diagnostics.Process loProcess = System.Diagnostics.Process.GetCurrentProcess();

            try
            {
                loProcess.MaxWorkingSet = (IntPtr)((int)loProcess.MaxWorkingSet - 1); //1,409,024
                loProcess.MinWorkingSet = (IntPtr)((int)loProcess.MinWorkingSet - 1); //  200,704
                CUtil.jlogEx("[clearMemory]MaxWorkingSet={0} MinWorkingSet={1}", loProcess.MaxWorkingSet, loProcess.MinWorkingSet);
            }
            catch (System.Exception)
            {
                loProcess.MaxWorkingSet = (IntPtr)((int)1024 * 1024);
                loProcess.MinWorkingSet = (IntPtr)((int)1024 * 8);
                CUtil.jlogEx("[clearMemory][●catch●]MaxWorkingSet={0} MinWorkingSet={1}", loProcess.MaxWorkingSet, loProcess.MinWorkingSet);
            }
        }
        /// <summary>
        /// 指定程式能使用最多記憶體(Byte),同時亦清除記憶體
        /// </summary>
        /// <param name="nMaxMemByte"></param>
        public static void setMaxMemory(int nMaxMemByte)
        {
            System.Diagnostics.Process loProcess = System.Diagnostics.Process.GetCurrentProcess();

            try
            {
                loProcess.MinWorkingSet = (IntPtr)(nMaxMemByte / 10);
                loProcess.MaxWorkingSet = (IntPtr)(nMaxMemByte);
                CUtil.jlogEx("[setMaxMemory]MaxWorkingSet={0} MinWorkingSet={1}", loProcess.MaxWorkingSet, loProcess.MinWorkingSet);
            }
            catch (System.Exception e)
            {
                //loProcess.MaxWorkingSet = (IntPtr)((int)1024 * 1024);
                //loProcess.MinWorkingSet = (IntPtr)((int)1024 * 8);
                //loProcess.MaxWorkingSet = (IntPtr)(loProcess.MinWorkingSet+1024);
                clearMemory();
                //loProcess.MinWorkingSet = (IntPtr)((int)loProcess.MinWorkingSet * (int)(2));
                CUtil.jlogEx("[setMaxMemory][●catch●]MaxWorkingSet={0} MinWorkingSet={1} [e: {2}]", loProcess.MaxWorkingSet, loProcess.MinWorkingSet, e.ToString());
            }
        }
Exemple #4
0
        public static bool clearDirectory(string strPath)
        {
            if (isFolder(strPath) == false)
            {
                CUtil.jlogEx("[clearDirectory]Error: invalid Folder Path:{0}", strPath);
                return(false);
            }

            System.IO.DirectoryInfo di = new DirectoryInfo(strPath);


            foreach (FileInfo file in di.GetFiles())
            {
                file.Delete();
            }
            foreach (DirectoryInfo dir in di.GetDirectories())
            {
                dir.Delete(true);
            }
            return(true);
        }
Exemple #5
0
        /// <summary>
        /// 在DEBUG模式中顯示資訊顯示行號,並連結回程式碼
        /// </summary>
        /// <param name="strBugMsg"></param>
        /// <param name="args"></param>
        public static void jmsgEx(String strBugMsg, params Object[] args)
        {
            String     strInfo;
            StackTrace stack = new StackTrace(1, true); //●取得stackframe的階層(視架構可能需要改變)
            var        sf    = stack.GetFrame(0);       //●取最遠的

            string strFile     = sf.GetFileName();
            string strFunction = sf.GetMethod().ToString();
            int    iLine       = sf.GetFileLineNumber();
            int    iColumn     = sf.GetFileColumnNumber();

            strInfo = strFile + "(" + iLine + ")" + ":[" + strFunction + "]"; //●可以連結回來的格式
            try
            {
                Debug.Write(String.Format(strInfo + ">>\n" + strBugMsg + "\n", args));
            }
            catch //(Exception e)
            {
                strBugMsg = CUtil.fixStringFormateError(strBugMsg);
                Debug.Write(String.Format(strInfo + ">>\n" + strBugMsg + "\n", args));
            }
        }
Exemple #6
0
        public static bool FolderRename(string strPathSource, string strNewFolderName)
        {
            string strFolderUpperPath = findUpperPath(strPathSource);

            if (strFolderUpperPath == "" || strFolderUpperPath.Length <= 1)
            {
                CUtil.jlogEx("搬移檔案錯誤:[{0}]=>上層檔案錯誤[{1}]", strPathSource, strFolderUpperPath);
                return(false);
            }
            string strTargetPath = strFolderUpperPath + "\\" + strNewFolderName;

            try
            {
                Directory.Move(strPathSource, strTargetPath);
                return(true);
            }
            catch (IOException eMsg)
            {
                CUtil.jlogEx("搬移檔案錯誤:[{0}]=>[{1}]\nerr:{2}", strPathSource, strTargetPath, eMsg.ToString());
                return(false);
            }
        }
Exemple #7
0
        public static bool deleteDirectory(string strPath)
        {
            if (isFolder(strPath) == false)
            {
                CUtil.jlogEx("[deleteDirectory]Error: invalid Folder Path:{0}", strPath);
                return(false);
            }

            System.IO.DirectoryInfo di = new DirectoryInfo(strPath);


            foreach (FileInfo file in di.GetFiles())
            {
                file.Delete();
            }
            foreach (DirectoryInfo dir in di.GetDirectories())
            {
                try
                {
                    dir.Delete(true);
                }
                catch (IOException ioExc)
                {
                    CUtil.jlogEx("[deleteDirectory]Error:{0}", ioExc.ToString());
                }
            }

            try
            {
                di.Delete();
            }
            catch (IOException ioExc)
            {
                CUtil.jlogEx("[deleteDirectory]Error:{0}", ioExc.ToString());
                return(false);
            }
            return(true);
        }
 public void startHook()
 {
     if (_isHooking() == false)
     {
         using (Process curProcess = Process.GetCurrentProcess())
             using (ProcessModule curModule = curProcess.MainModule)
             {
                 //全系統監控HOOK
                 if (m_hookType == HookType.WH_MOUSE_LL || m_hookType == HookType.WH_KEYBOARD_LL)
                 {
                     hookHandle = SetWindowsHookEx(m_hookType, m_hookProc, GetModuleHandle(curModule.ModuleName), 0);
                 }
                 //指監控HOOK此應用程式本身
                 else
                 {
                     hookHandle = SetWindowsHookEx(m_hookType, m_hookProc, GetModuleHandle(curModule.ModuleName), CUtil.GetCurrentThreadId());
                 }
             }
     }
 }