C#访问文件路径通用类

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

 

 

namespace Utility

{

    /// <summary>

    /// @Author:梁继龙

    /// @Date:2012/8/1

    /// @Email:jilongliang1988ora@163.com

    /// @Description:文件处理类

    /// </summary>

    public class AccessDirFileHelper

    {

        public static AccessDirFileHelper instance = null;

        private static readonly object lockObj = new object();

        protected static System.Reflection.Assembly assembly = null; //反射 

        /// <summary>

        /// 单例模式

        /// </summary>

        /// <returns></returns>

        public static AccessDirFileHelper GetInstance()

        {

            //lock (AccessFileHelper.instance){ }

            if (instance == null)

            {

                lock (lockObj)

                {

                    instance = new AccessDirFileHelper();

                }

            }

            return instance;

        }

 

        /// <summary>

        /// 获取当前进程的完整路径,包含文件名(进程名)

        /// result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名

        /// </summary>

        /// <returns></returns>

        public string GetLocalProcessFileName()

        {

            try

            {

                return this.GetType().Assembly.Location;

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

 

        /// <summary>

        /// 获取新的 Process 组件并将其与当前活动的进程关联的主模块的完整路径,包含文件名(进程名).

        //result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)

        /// </summary>

        /// <returns></returns>

        public string GetCurrentProcessFileNamePath()

        {

            try

            {

                return System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

 

        /// <summary>

        /// 获取和设置当前目录(即该进程从中启动的目录)的完全限定路径.

        /// result: X:\xxx\xxx (.exe文件所在的目录)

        /// </summary>

        /// <returns></returns>

        public String GetCurrentEnvironmentDirectoryPath()

        {

            try

            {

                return System.Environment.CurrentDirectory;

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

 

        /// <summary>

        /// 获取当前 Thread 的当前应用程序域的基目录,它由程序集冲突解决程序用来探测程序集。

        ///string str = System.AppDomain.CurrentDomain.BaseDirectory;

        ///result: X:\xxx\xxx\ (.exe文件所在的目录+"\")

        ///Example:用来判断winfrom会少一个Debug  E:\\ASP.NET\\MyApp\\WebApp\\bin\\Debug\\bin\\Utility.dll"  web

        ///Example:用来判断winfrom会多一个Debug  E:\\ASP.NET\\MyApp\\WinApp\\bin\\Debug\\Utility.dll"  winform

        ///此方法一般判断一个解决方案启动是web还是winform读取文件(dll,config,txt等..)看情况定

        /// </summary>

        public string GetCurrentDomainBaseDirectory()

        {

            try

            {   //效果一样

                //System.Threading.Thread.GetDomain().BaseDirectory

                return System.AppDomain.CurrentDomain.BaseDirectory;

            }

            catch (Exception e)

            {

                throw e;

            }

        }

 

        /// <summary>

        /// 获取和设置包含该应用程序的目录的名称。

        ///string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

        ///result: X:\xxx\xxx\ (.exe文件所在的目录+"\")

        /// </summary>

        /// <returns></returns>

        public string GetCurrentDomainSetupInformationName()

        {

            try

            {

                return System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

 

        /// <summary>

        /// 获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称

        /// string str = System.Windows.Forms.Application.StartupPath;

        /// result: X:\xxx\xxx (.exe文件所在的目录)

        /// </summary>

        /// <returns></returns>

        public  string GetWindowsApplicationStartupPath()

        {

            try

            {

                return System.Windows.Forms.Application.StartupPath;

            }

            catch (Exception e)

            {

                throw e;

            }

        }

 

        /// <summary>

        ///获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。

        ///string str = System.Windows.Forms.Application.ExecutablePath;

        ///result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)

        /// </summary>

        /// <returns></returns>

        public string GetWindowsExecutablePath()

        {

            try

            {

                return System.Windows.Forms.Application.ExecutablePath;

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

 

        /// <summary>

        /// 获取应用程序的当前工作目录(不可靠)

        /// /string str = System.IO.Directory.GetCurrentDirectory();

        /// result: X:\xxx\xxx (.exe文件所在的目录)

        /// </summary>

        /// <returns></returns>

        public string GetCurrentDirectory()

        {

            try

            {

                return System.IO.Directory.GetCurrentDirectory();

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

        /// <summary>

        /// 这个方法的意思是:在同一个解决方案里面有两个工程,启动的时候

        /// 去判断哪个工程是Winform或者是Web的配置

        ///Example:E:\\ASP.NET\\MyApp\\WinApp\\bin\\Debug\\MyApp.vshost.exe.Config" //winform

        ///Example:"E:\\ASP.NET\\MyApp\\WebApp\\web.config"  //web

        ///从后面的配置截取Example:String index=Path3.Substring(config.Length - 8);截取

        ///后面字符串判去判断,最好把都转换成小写或者大写

        ///index.ToUpper()="E.CONFIG"   /   index.ToLower()=="e.config"

        /// </summary>

        /// <returns></returns>

        public System.Reflection.Assembly GetCurrentDomainConfigurationFile(string executeWinformConfig, string executeWebConfig)

        {

            try

            {

                string config = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

                string index = config.Substring(config.Length - 8);

 

                if (index.ToLower() == "e.config")  //把截取到的字符串转为为小写

                {

                    assembly = System.Reflection.Assembly.LoadFile(executeWinformConfig); //加载dll文件

                }

                if (index.ToLower() == "b.config")

                {

                    assembly = System.Reflection.Assembly.LoadFile(executeWebConfig); //加载dll文件

                }

                return assembly;

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

        /// <summary>

        /// 无参数的GetCurrentDomainConfigurationFile方法

        /// GetCurrentDomainConfigurationFile

        /// </summary>

        /// <returns></returns>

        public string GetCurrentDomainConfigurationFile()

        {

            try

            {

                return AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

 

        /// <summary>

        /// Application.StartupPath与AppDomain.CurrentDomain.BaseDirectory都是判断

        /// 当前启动的工程,并体现能找到

        /// Example:E:\\ASP.NET\\MyApp\\MyApp\\bin\\Debug路径下面,唯有不同的就是

        /// AppDomain.CurrentDomain.BaseDirectory多了个 \\

        /// Application.StartupPath而且是System.Windows.Forms

        /// </summary>

        /// <returns></returns>

        public string GetStartupPath()

        {

            try

            {

                return System.Windows.Forms.Application.StartupPath;

            }

            catch (Exception)

            {

                throw;

            }

        }

 

        /// <summary>

        /// 获取文件的大小,一般指文件有多少个字节

        /// </summary>

        /// <param name="fileName"></param>

        /// <returns></returns>

        public string GetFileSize(string fileName)

        {

            try

            {

                if (File.Exists(fileName))

                {

                    return Convert.ToString(new FileInfo(fileName).Length);

                }

                else

                    return fileName;

            }

            catch (Exception)

            {

                throw;

            }

        }

        ///<summary>

        ///在卸载程序获取系统安装的目录:

        ///System.Reflection.Assembly curPath = System.Reflection.Assembly.GetExecutingAssembly();

        /// string path=curPath.Location;//得到安装程序类SetupLibrary文件的路径,获取这个文件路径

        ///所在的目录即得到安装程序的目录;

        ///</summary>

        /// <returns></returns>

        public String GetExecutingAssemblyDirectory()

        {

            try

            {

                System.Reflection.Assembly curPath = System.Reflection.Assembly.GetExecutingAssembly();

                string path = curPath.Location;

                return path;

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

 

 

        #region

        

        /// <summary>

        /// 创建目录

        /// </summary>

        /// <param name="dir">此地路径相对站点而言</param>

        public static void CreateDir(string dir)

        {

            if (dir.Length == 0) return;

            if (!System.IO.Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(dir)))

                System.IO.Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(dir));

        }

        /// <summary>

        /// 创建目录路径

        /// </summary>

        /// <param name="folderPath">物理路径</param>

        public static void CreateFolder(string folderPath)

        {

            if (!System.IO.Directory.Exists(folderPath))

                System.IO.Directory.CreateDirectory(folderPath);

        }

        /// <summary>

        /// 删除目录

        /// </summary>

        /// <param name="dir">此地路径相对站点而言</param>

        public static void DeleteDir(string dir)

        {

            if (dir.Length == 0) return;

            if (System.IO.Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(dir)))

                System.IO.Directory.Delete(System.Web.HttpContext.Current.Server.MapPath(dir), true);

        }

        /// <summary>

        /// 判断文件是否存在

        /// </summary>

        /// <param name="file">格式:a/b.htm,相对根目录</param>

        /// <returns></returns>

        public static bool FileExists(string file)

        {

            if (File.Exists(System.Web.HttpContext.Current.Server.MapPath(file)))

                return true;

            else

                return false;

        }

        /// <summary>

        /// 读取文件内容

        /// </summary>

        /// <param name="file">格式:a/b.htm,相对根目录</param>

        /// <returns></returns>

        public static string ReadFile(string file)

        {

            if (!FileExists(file))

                return "";

            try

            {

                System.IO.StreamReader sr = new System.IO.StreamReader(System.Web.HttpContext.Current.Server.MapPath(file), System.Text.Encoding.UTF8);

                string str = sr.ReadToEnd();

                sr.Close();

                return str;

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

        /// <summary>

        /// 保存为不带Bom的文件

        /// </summary>

        /// <param name="TxtStr"></param>

        /// <param name="tempDir">格式:a/b.htm,相对根目录</param>

        public static void SaveFile(string TxtStr, string tempDir)

        {

            SaveFile(TxtStr, tempDir, true);

        }

        /// <summary>

        /// 保存文件内容,自动创建目录

        /// </summary>

        /// <param name="TxtStr"></param>

        /// <param name="tempDir">格式:a/b.htm,相对根目录</param>

        /// <param name="noBom"></param>

        public static void SaveFile(string TxtStr, string tempDir, bool noBom)

        {

            try

            {

                CreateDir(GetFolderPath(true, tempDir));

                System.IO.StreamWriter sw;

                if (noBom)

                    sw = new System.IO.StreamWriter(System.Web.HttpContext.Current.Server.MapPath(tempDir), false, new System.Text.UTF8Encoding(false));

                else

                    sw = new System.IO.StreamWriter(System.Web.HttpContext.Current.Server.MapPath(tempDir), false, System.Text.Encoding.UTF8);

 

                sw.Write(TxtStr);

                sw.Close();

            }

            catch (Exception ex)

            {

                throw ex;

            }

        }

        /// <summary>

        /// 复制文件

        /// 这个方法在6.0版本后改写,虽看似比前面的版本冗长,但避免了file2文件一直被占用的问题

        /// </summary>

        /// <param name="file1"></param>

        /// <param name="file2"></param>

        /// <param name="overwrite">如果已经存在是否覆盖?</param>

        public static void CopyFile(string file1, string file2, bool overwrite)

        {

            if (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(file1)))

            {

                if (overwrite)

                    System.IO.File.Copy(System.Web.HttpContext.Current.Server.MapPath(file1), System.Web.HttpContext.Current.Server.MapPath(file2), true);

                else

                {

                    if (!System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(file2)))

                        System.IO.File.Copy(System.Web.HttpContext.Current.Server.MapPath(file1), System.Web.HttpContext.Current.Server.MapPath(file2));

                }

            }

        }

        /// <summary>

        /// 删除文件

        /// </summary>

        /// <param name="file">此地路径相对程序路径而言</param>

        public static void DeleteFile(string file)

        {

            if (file.Length == 0) return;

            if (System.IO.File.Exists(System.Web.HttpContext.Current.Server.MapPath(file)))

                System.IO.File.Delete(System.Web.HttpContext.Current.Server.MapPath(file));

        }

        /// <summary>

        /// 获得文件的目录路径

        /// </summary>

        /// <param name="filePath">文件路径</param>

        /// <returns>以\结尾</returns>

        public static string GetFolderPath(string filePath)

        {

            return GetFolderPath(false, filePath);

        }

        /// <summary>

        /// 获得文件的目录路径

        /// </summary>

        /// <param name="isUrl">是否是网址</param>

        /// <param name="filePath">文件路径</param>

        /// <returns>以\或/结尾</returns>

        public static string GetFolderPath(bool isUrl, string filePath)

        {

            if (isUrl)

                return filePath.Substring(0, filePath.LastIndexOf("/") + 1);

            else

                return filePath.Substring(0, filePath.LastIndexOf("\\") + 1);

        }

        /// <summary>

        /// 获得文件的名称

        /// </summary>

        /// <param name="filePath"></param>

        /// <returns></returns>

        public static string GetFileName(string filePath)

        {

            return GetFileName(false, filePath);

        }

        /// <summary>

        /// 获得文件的名称

        /// </summary>

        /// <param name="isUrl">是否是网址</param>

        /// <param name="filePath"></param>

        /// <returns></returns>

        public static string GetFileName(bool isUrl, string filePath)

        {

            if (isUrl)

                return filePath.Substring(filePath.LastIndexOf("/") + 1, filePath.Length - filePath.LastIndexOf("/") - 1);

            else

                return filePath.Substring(filePath.LastIndexOf("\\") + 1, filePath.Length - filePath.LastIndexOf("\\") - 1);

        }

        /// <summary>

        /// 获得文件的后缀

        /// 不带点,小写

        /// </summary>

        /// <param name="filePath"></param>

        /// <returns></returns>

        public static string GetFileExt(string filePath)

        {

            return filePath.Substring(filePath.LastIndexOf(".") + 1, filePath.Length - filePath.LastIndexOf(".") - 1).ToLower();

        }

        /// <summary>

        /// 目录拷贝

        /// </summary>

        /// <param name="OldDir"></param>

        /// <param name="NewDir"></param>

        public static void CopyDir(string OldDir, string NewDir)

        {

            DirectoryInfo OldDirectory = new DirectoryInfo(OldDir);

            DirectoryInfo NewDirectory = new DirectoryInfo(NewDir);

            CopyDir(OldDirectory, NewDirectory);

        }

        private static void CopyDir(DirectoryInfo OldDirectory, DirectoryInfo NewDirectory)

        {

            string NewDirectoryFullName = NewDirectory.FullName + "\\" + OldDirectory.Name;

 

            if (!Directory.Exists(NewDirectoryFullName))

                Directory.CreateDirectory(NewDirectoryFullName);

 

            FileInfo[] OldFileAry = OldDirectory.GetFiles();

            foreach (FileInfo aFile in OldFileAry)

                File.Copy(aFile.FullName, NewDirectoryFullName + "\\" + aFile.Name, true);

 

            DirectoryInfo[] OldDirectoryAry = OldDirectory.GetDirectories();

            foreach (DirectoryInfo aOldDirectory in OldDirectoryAry)

            {

                DirectoryInfo aNewDirectory = new DirectoryInfo(NewDirectoryFullName);

                CopyDir(aOldDirectory, aNewDirectory);

            }

        }

        /// <summary>

        /// 目录删除

        /// </summary>

        /// <param name="OldDir"></param>

        public static void DelDir(string OldDir)

        {

            DirectoryInfo OldDirectory = new DirectoryInfo(OldDir);

            OldDirectory.Delete(true);

        }

 

        /// <summary>

        /// 目录剪切

        /// </summary>

        /// <param name="OldDirectory"></param>

        /// <param name="NewDirectory"></param>

        public static void CopyAndDelDir(string OldDirectory, string NewDirectory)

        {

            CopyDir(OldDirectory, NewDirectory);

            DelDir(OldDirectory);

        }

        /// <summary>

        /// 文件下载

        /// </summary>

        /// <param name="_Request"></param>

        /// <param name="_Response"></param>

        /// <param name="_fullPath">源文件路径</param>

        /// <param name="_speed"></param>

        /// <returns></returns>

        public static bool DownloadFile(System.Web.HttpRequest _Request, System.Web.HttpResponse _Response, string _fullPath, long _speed)

        {

            string _fileName = GetFileName(false, _fullPath);

            try

            {

                FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

                BinaryReader br = new BinaryReader(myFile);

                try

                {

                    _Response.AddHeader("Accept-Ranges", "bytes");

                    _Response.Buffer = false;

                    long fileLength = myFile.Length;

                    long startBytes = 0;

 

                    double pack = 10240; //10K bytes

                    //int sleep = 200;   //每秒5次   即5*10K bytes每秒

                    int sleep = (int)Math.Floor(1000 * pack / _speed) + 1;

                    if (_Request.Headers["Range"] != null)

                    {

                        _Response.StatusCode = 206;

                        string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' });

                        startBytes = Convert.ToInt64(range[1]);

                    }

                    _Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());

                    _Response.AddHeader("Connection", "Keep-Alive");

                    _Response.ContentType = "application/octet-stream";

                    _Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8));

 

                    br.BaseStream.Seek(startBytes, SeekOrigin.Begin);

                    int maxCount = (int)Math.Floor((fileLength - startBytes) / pack) + 1;

 

                    for (int i = 0; i < maxCount; i++)

                    {

                        if (_Response.IsClientConnected)

                        {

                            _Response.BinaryWrite(br.ReadBytes(int.Parse(pack.ToString())));

                            System.Threading.Thread.Sleep(sleep);

                        }

                        else

                        {

                            i = maxCount;

                        }

                    }

                }

                catch

                {

                    return false;

                }

                finally

                {

                    br.Close();

                    myFile.Close();

                }

            }

            catch

            {

                return false;

            }

            return true;

        }

        #endregion

 

    }

}