惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
U
Unit 42
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
Microsoft Azure Blog
Microsoft Azure Blog
有赞技术团队
有赞技术团队
Stack Overflow Blog
Stack Overflow Blog
爱范儿
爱范儿
博客园 - 司徒正美
Vercel News
Vercel News
I
InfoQ
GbyAI
GbyAI
C
Check Point Blog
B
Blog RSS Feed
Martin Fowler
Martin Fowler
B
Blog
MyScale Blog
MyScale Blog
腾讯CDC
博客园 - Franky
Blog — PlanetScale
Blog — PlanetScale
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hugging Face - Blog
Hugging Face - Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 三生石上(FineUI控件)

博客园 - 谢堂文(Darren Xie)

Camstar MES 5.8 發現Ajax事件失效 验证(C#和正则表达式) 新MES已上線 Microsoft.ACE.OLEDB.12.0' provider is not registered on the local machine Microsoft SQL Server Reporting Services (SSRS)报表技巧之在图表中增加参考线 MS SQL,数据库增长时众多表中谁是你关注的目标 SQL调优日记之发挥SQL性能与你的写法有关--对比三种方式实现相同功能 想个法子找出性能差的SQL T-SQL 日期处理总结 从MS SQL删除大数据说开去 一步一步用C#写一个检查SQL Server假死警报服务 MM的Windows 7 登录密码忘记之后 基于工作实际需求的Ext.Net和C#搭配应用之一 取出网域(AD)中所有计算机名及位置描述等信息 MS SQL自定义字符串拆分函数的惨痛经历 2012,我的C#全能Excel操作(无需Office,不使用XML) 代码注释规范 2012年开发者该做的11件事 取出AD中一個組的所有成員信息(C#實現功能配合EXT做的界面) C#路径/文件/目录/I/O常见操作汇总
一個文件文件和路徑的類
谢堂文(Darren Xie) · 2012-01-03 · via 博客园 - 谢堂文(Darren Xie)
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;
            }
        }
 
    }
}