Exemple #1
0
        /// <summary>
        /// 找到路徑最後的元件,不管是資料夾或者檔名
        /// 例如:
        /// C:\\....\\DirectoryA
        /// C:\\....\\DirectoryA\\ ==>自動除去最後的斜線
        /// C:\\....\\DirectoryA\\FileB.txt
        /// 亦可
        /// </summary>
        /// <param name="strFullPath">路徑字串</param>
        /// <returns>最後元件字串</returns>
        public static string findPathLastItem(string strFullPath)
        {
            int nLastSlash    = strFullPath.LastIndexOf("\\");
            int nStringLength = strFullPath.Length;

            if (nStringLength <= 0)
            {
                return("");
            }

            ///若路徑最後為"\\"則自動去除
            if (strFullPath.Length == nLastSlash + 1)
            {
                string strDummy = strFullPath;
                strDummy      = strDummy.Remove(nLastSlash, 1);
                nLastSlash    = strDummy.LastIndexOf("\\");
                nStringLength = strDummy.Length;
            }
            try
            {
                string strlastItem = strFullPath.Substring(nLastSlash + 1, nStringLength - 1 - nLastSlash);
                return(strlastItem);
            }
            catch (Exception e)
            {
                CUtil.jlogEx("[Uitl-Err][{0}]", e.ToString());
                return("");
            }
        }
Exemple #2
0
        public static bool checkDirectory(string strFolderPath, bool blCreateDirectory = true)
        {
            if (!Directory.Exists(strFolderPath))
            {
                ///沒有要建立資料夾
                if (blCreateDirectory == false)
                {
                    return(false);
                }

                try
                {
                    Directory.CreateDirectory(strFolderPath);
                    if (!Directory.Exists(strFolderPath))
                    {
                        return(false);
                    }
                    else
                    {
                        return(true);
                    }
                }
                catch (Exception ex)
                {
                    CUtil.jlogEx("[checkDirectory][Err][{0}]", ex.ToString());
                    return(false);
                }
            }
            else
            {
                ///有資料夾
                return(true);
            }
        }
Exemple #3
0
 public static bool FolderMove(string strPathSource, string strPathTarget)
 {
     try
     {
         Directory.Move(strPathSource, strPathTarget);
         return(true);
     }
     catch (IOException eMsg)
     {
         CUtil.jlogEx("搬移檔案錯誤:\n{0}", eMsg.ToString());
         return(false);
     }
 }
        /// <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 #6
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 #7
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 #8
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);
        }