• 一個文件文件和路徑的類


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
     
    namespace Core
    {
        public static class FilePathTools
        {
            /// <summary>
            /// 取出文件擴展名
            /// </summary>
            /// <param name="FileName">文件名</param>
            /// <returns></returns>
            public static string GetExtFileTypeName(string FileName)
            {
                string sFile = FileName;
                sFile = sFile.Substring(sFile.LastIndexOf("\\") + 1);
                sFile = sFile.Substring(sFile.LastIndexOf(".")).ToLower();
                return sFile;
            }
     
            /// <summary>
            /// 取出文件名,包括擴展名的完整名稱
            /// </summary>
            /// <param name="FileName">文件名</param>
            /// <returns></returns>
            public static string GetFileFullName(string FileName)
            {
                string sFile = FileName;
                sFile = sFile.Substring(sFile.LastIndexOf("\\") + 1);
                //sFile = sFile.Substring(sFile.LastIndexOf(".")).ToLower();
                return sFile;
            }
     
            /// <summary>
            /// 取出文件名,不包括擴展名
            /// </summary>
            /// <param name="FileName">文件名</param>
            /// <returns></returns>
            public static string GetFileName(string FileName)
            {
                string sFile = FileName;
                sFile = sFile.Substring(sFile.LastIndexOf("\\") + 1);
                sFile = sFile.Substring(0,sFile.LastIndexOf(".")).ToLower();
                return sFile;
            }
     
            /// <summary>
            /// 取出文件所在的路徑
            /// </summary>
            /// <param name="FileName">文件名</param>
            /// <returns></returns>
            public static string GetFilePath(string FileName)
            {
                string sFile = FileName;
                sFile = sFile.Substring(0,sFile.LastIndexOf("\\") );            
                return sFile;
            }
     
            /// <summary>
            /// 產生另一個擴展名的文件完整路徑字符串
            /// </summary>
            /// <param name="fileName"></param>
            /// <param name="newext"></param>
            /// <returns></returns>
            public static string NewExtFileName(string fileName, string newext)
            {
                return GetFilePath(fileName) + "\\" + GetFileName(fileName) + newext;
            }
     
            /// <summary>
            /// 文件所在的路徑是否存在
            /// </summary>
            /// <param name="FileName">文件名</param>
            /// <returns></returns>
            public static bool HasFilePath(string FileName)
            {
                return Directory.Exists(GetFilePath(FileName));
            }
     
            /// <summary>
            /// 路徑創建
            /// </summary>
            /// <param name="FileName">文件名</param>
            /// <returns></returns>
            public static bool CreatePath(string path)
            {
                try
                {
                    if (!HasFilePath(GetFilePath(path)))
                    {
                        CreatePath(GetFilePath(path));
                    }
                    Directory.CreateDirectory(path);
                    return true;
                }
                catch(Exception ee)
                {
                    throw new Exception(ee.Message);
                }
            }
     
            /// <summary>
            /// 文件是否存在
            /// </summary>
            /// <param name="FileName">文件名</param>
            /// <returns></returns>
            public static bool HasFile(string FileName)
            {
                return File.Exists(FileName);
            }
            /// <summary>
            /// 取一文件夾下所有的文件名完整路徑,包括子文件夾下的
            /// </summary>
            /// <param name="path">一個路徑字符串</param>
            /// <returns>List<string></returns>
            public static List<string> GetAllFileForPath(string path)
            {
                List<string> lsf = new List<string>();
                try
                {
                    if (Directory.Exists(path))
                    {
                        List<string> ldirs = GetAllDirForPath(path);
                        foreach (string s in ldirs)
                        {
                            try
                            {
                                if (Directory.Exists(s))
                                    lsf.AddRange(Directory.GetFiles(s));
                            }
                            catch (UnauthorizedAccessException ue)
                            {
                               //碰到沒有訪問權限的,跳過
                            }
                            catch (NullReferenceException ne)
                            {
                                //碰到空引用,跳過
                            }
                        }
     
                    }
     
                    return lsf;
                }
                catch (UnauthorizedAccessException ue)
                {
                    return null;
                    //這個文件或文件夾沒有辦法訪問,產生空值
                }
                catch (NullReferenceException ne)
                {
                    return lsf;
                    //本文件夾是空值,返回已有的內容
                }
                catch (Exception ee)
                {
                    throw ee;
                }
            }
            /// <summary>
            /// 取一文件夾下所有的文件夾完整路徑,包括子文件夾下的
            /// </summary>
            /// <param name="path">一個路徑字符串</param>
            /// <returns>List<string></returns>
            public static List<string> GetAllDirForPath(string path)
            {
                try
                {
                    List<string> dirs = new List<string>();
                    try
                    {
                        if (!Directory.Exists(path))
                        {
                            throw new Exception(path + "不存在.");
                        }
                        if (!dirs.Contains(path))
                        {
                            dirs.Add(path);
                        }
                    }
                    catch (UnauthorizedAccessException ue)
                    {
                    }
                    catch (NullReferenceException ne)
                    {
     
                    }
                    if (Directory.GetDirectories(path).Length > 0)
                    {
                        foreach (string dir in Directory.GetDirectories(path))
                        {
                            try
                            {
                                if (!dirs.Contains(dir))
                                {
                                    dirs.Add(dir);
                                }
                            }
                            catch (UnauthorizedAccessException ue)
                            {
                            }
                            catch (NullReferenceException ne)
                            {
     
                            }
     
     
                            foreach (string s in GetAllDirForPath(dir))
                            {
                                try
                                {
                                    if (!dirs.Contains(s))
                                    {
                                        dirs.Add(s);
                                    }
                                }
                                catch (UnauthorizedAccessException ue)
                                {
                                }
                                catch (NullReferenceException ne)
                                {
     
                                }
                            }
                        }
                    }
     
                    return dirs;
                }
                catch (UnauthorizedAccessException ue)
                {
                    return null;
                }
                catch(Exception ee)
                {
                    throw ee;
                }
            }
     
        }
    }
  • 相关阅读:
    tab切换与表格展示
    ajax
    api
    slice() 方法
    iframe跳转
    快排序
    【问题排查】cpu占用过高排查
    LOJ6013 负载平衡 [最小费用最大流]
    随机序列 [思维题, 组合数]
    P1777 帮助 [状压dp]
  • 原文地址:https://www.cnblogs.com/yiyumeng/p/2310794.html
Copyright © 2020-2023  润新知