zoukankan      html  css  js  c++  java
  • C# 获取文件路径,读取项目中某程序集下文件

    获取文件路径  

    -------------------------------------------------------------------------

    winform获取文件路径:

    string str1 =Process.GetCurrentProcess().MainModule.FileName;//获得当前执行的exe的文件名。
    string str2=Environment.CurrentDirectory;//获取和设置当前目录的完全限定路径。
    string str3=Directory.GetCurrentDirectory();//获取应用程序的当前工作目录。
    string str4=AppDomain.CurrentDomain.BaseDirectory;//获取基目录,它由程序集冲突解决程序用来探测程序集。
    string str5=Application.StartupPath;//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。
    string str6=Application.ExecutablePath;//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
    string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//获取或设置包含该应用程序的目录的名称。

    “Application.StartupPath”:获取当前应用程序所在目录的路径,最后不包含“”;
    “Application.ExecutablePath ”:获取当前应用程序文件的路径,包含文件的名称;
    “AppDomain.CurrentDomain.BaseDirectory”:获取当前应用程序所在目录的路径,最后包含“”;
    “System.Threading.Thread.GetDomain().BaseDirectory”:获取当前应用程序所在目录的路径,最后包含“”;
    “Environment.CurrentDirectory”:获取当前应用程序的路径,最后不包含“”;
    “System.IO.Directory.GetCurrentDirectory”:获取当前应用程序的路径,最后不包含“”;

    String[] files = System.IO.Directory.GetFiles(path) //返回指定目录下的文件名

    string str = System.IO.Path.GetFileNameWithoutExtension(path);//返回不具有扩展名的指定路径字符串的文件名

    取得控制台应用程序的根目录方法    

    方法1、Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径
    方法2、AppDomain.CurrentDomain.BaseDirectory 获取基目录,它由程序集冲突解决程序用来探测程序集

    取得Web应用程序的根目录方法

    方法1、HttpRuntime.AppDomainAppPath.ToString();//获取承载在当前应用程序域中的应用程序的应用程序目录的物理驱动器路径。用于App_Data中获取 D:wwwrootDllTest
    方法2、Server.MapPath("") 或者 Server.MapPath("~/");//返回与Web服务器上的指定的虚拟路径相对的物理文件路径 D:wwwrootDllTest
    方法3、Request.ApplicationPath;//获取服务器上ASP.NET应用程序的虚拟应用程序根目录

    取得WinForm应用程序的根目录方法

    1、Environment.CurrentDirectory.ToString();//获取或设置当前工作目录的完全限定路径
    2、Application.StartupPath.ToString();//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称
    3、Directory.GetCurrentDirectory();//获取应用程序的当前工作目录
    4、AppDomain.CurrentDomain.BaseDirectory;//获取基目录,它由程序集冲突解决程序用来探测程序集
    5、AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//获取或设置包含该应用程序的目录的名称
    其中:以下两个方法可以获取执行文件名称
    1、Process.GetCurrentProcess().MainModule.FileName;//可获得当前执行的exe的文件名。
    2、Application.ExecutablePath;//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称

    3、System.IO.Path类中有一些获取路径的方法,可以在控制台程序或者WinForm中根据相对路径来获取绝对路径

    获取web物理路径的方法

    System.Web.HttpContext.Current.Server.MapPath("~/a/b.config");//获取指定虚拟路径下对应的实际物理路径

     读取项目中某程序集下文件

    --------------------------------------------------------------------------

    例如:在一个项目中,程序集A,程序集B   在程序集A中要读取程序集B中的某一模板文件(目录:BEmailTempletesJobsubscritionList.templete) 

    解决方法如下:

            /// <summary>
            /// 读取模版
            /// </summary>
            /// <param name="fileName">模版名称</param>
            /// <returns>模版内容</returns>
            private static string ReadEmbedFile(string fileName)
            {
                fileName = "B.EmailTempletes.JobsubscritionList.templete";//为方便我直接赋值测试了
                Assembly assembly = Assembly.Load("Linkin.Service");
                Stream fs = null;
                StreamReader sr = null;
                try
                {
                    fs = assembly.GetManifestResourceStream(fileName);
                    sr = new StreamReader(fs);
                    return sr.ReadToEnd();
                }
                catch
                {
                    return null;
                }
                finally
                {
                    if (fs != null)
                        fs.Dispose();
                    if (sr != null)
                        sr.Dispose();
                }
            }
    
  • 相关阅读:
    如何将网格式报表打印成其它样式
    拥有与实力不相称的脾气是种灾难——北漂18年(23)
    8.8.1 Optimizing Queries with EXPLAIN
    mysql 没有rowid 怎么实现根据rowid回表呢?
    secondary index
    8.5.5 Bulk Data Loading for InnoDB Tables 批量数据加载
    mysql 中key 指的是索引
    8.5.4 Optimizing InnoDB Redo Logging 优化InnoDB Redo 日志
    8.5.3 Optimizing InnoDB Read-Only Transactions 优化InnoDB 只读事务
    8.5.1 Optimizing Storage Layout for InnoDB Tables InnoDB表的存储布局优化
  • 原文地址:https://www.cnblogs.com/xishuqingchun/p/4063413.html
Copyright © 2011-2022 走看看