zoukankan      html  css  js  c++  java
  • C# Path 有关于文件路径获取的问题 的方法

    string Current = Directory.GetCurrentDirectory();//获取当前根目录
    //private string strFilePath = Application.StartupPath + "\" + "FileConfig.ini";//获取INI文件路径

    //MessageBox.Show("strFilePath: "+strFilePath);
    //MessageBox.Show("Application.StartupPath: " + Application.StartupPath+"\");
    //MessageBox.Show("Current: "+Current);
    //MessageBox.Show("ExecutablePath: " + System.Windows.Forms.Application.ExecutablePath.ToString());
    //MessageBox.Show("目录名称:{0}", Path.GetDirectoryName(strFilePath));
    //MessageBox.Show("路径扩展名:{0}", Path.GetExtension(strFilePath));
    //MessageBox.Show("文件名:{0}", Path.GetFileName(strFilePath));
    //MessageBox.Show("不带扩展名的名称:{0}", Path.GetFileNameWithoutExtension(strFilePath));
    //MessageBox.Show("绝对全路径:{0}", Path.GetFullPath(strFilePath));
    //MessageBox.Show("根目录:{0}", Path.GetPathRoot(strFilePath));
    //MessageBox.Show("不带根目录的路径:{0}", Path.GetFullPath(strFilePath).Remove(0, 3));
    //MessageBox.Show(System.Windows.Forms.Application.StartupPath);
    //MessageBox.Show(strFilePath);
    //MessageBox.Show(Application.ExecutablePath);
    //MessageBox.Show(AppDomain.CurrentDomain.BaseDirectory);
    //MessageBox.Show(Thread.GetDomain().BaseDirectory);
    //MessageBox.Show(Environment.CurrentDirectory);
    //MessageBox.Show(Directory.GetCurrentDirectory());
    //MessageBox.Show(Assembly.GetExecutingAssembly().Location);

    C# .NET 获取路径信息

    C# .NET 获取路径信息

    Application.StartupPath  // 获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。 
     Application.ExecutablePath  // 获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。  
    AppDomain.CurrentDomain.BaseDirectory  // 获取基目录,它由程序集冲突解决程序用来探测程序集。  
    Thread.GetDomain().BaseDirectory  // 获取基目录,它由程序集冲突解决程序用来探测程序集。 
     Environment.CurrentDirectory  // 获取或设置当前工作目录的完全限定路径。  
    Directory.GetCurrentDirectory()  // 获取应用程序的当前工作目录。  
    Assembly.GetExecutingAssembly().Location // 获取包含清单的已加载文件的路径或 UNC 位置。

    通过Request属性获取:

    // 获取当前正在执行的服务器应用程序的根目录的物理文件系统路径。
    Request.PhysicalApplicationPath;    // E:解决方案项目// 获取与请求的 URL 相对应的物理文件系统路径。 
    Request.PhysicalPath;   // E:\解决方案项目zzzz.aspx

    获取虚拟路径和URL信息:(URL:http://localhost/aspnet/zz/zz.aspx/info?name=wk )

    // 获取服务器上 ASP.NET 应用程序的虚拟应用程序根路径:/Request.ApplicationPath;// /aspnet  // 获取应用程序根的虚拟路径,并通过对应用程序根使用波形符 (~) 表示法使该路径成为相对路径。
    Request.AppRelativeCurrentExecutionFilePath;   // ~/zz/zz.aspx  // 获取当前请求的虚拟路径
    Request.CurrentExecutionFilePath;// /aspnet/zz/zz.aspxRequest.FilePath;// /aspnet/zz/zz.aspx  // 获取CurrentExecutionFilePath属性中指定的文件名的扩展名。
    Request.CurrentExecutionFilePathExtension;  // .aspx   // 获取当前请求的虚拟路径(包括附件路径信息)
    Request.Path;// /aspnet/zz/zz.aspx/info  // 获取具有 URL 扩展名的资源的附加路径信息。
    Request.PathInfo; // /info  // 获取有关当前请求的 URL 的信息。Request.Url;//http://localhost/aspnet/zz/zz.aspx/inf?name=wk  // 获取当前请求的原始 URLRequest.RawUrl; // /aspnet/zz/zz.aspx/inf?name=wk  // 获取有关客户端上次请求的 URL 的信息,该请求链接到当前的 URL。Request.UrlReferrer;// System.Uri

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

    C# Path

    using System.IO;

    Path类的静态方法:  

        ChangeExtension 更改路径字符串的扩展名。

        Combine(String()) 将字符串数组组合成一个路径。

        Combine(String, String) 将两个字符串组合成一个路径。

        Combine(String, String, String) 将三个字符串组合成一个路径。

        Combine(String, String, String, String) 将四个字符串组合成一个路径。

        GetDirectoryName 返回指定路径字符串的目录信息。

        GetExtension 返回指定的路径字符串的扩展名。

        GetFileName 返回指定路径字符串的文件名和扩展名。

        GetFileNameWithoutExtension 返回不具有扩展名的指定路径字符串的文件名。

        GetFullPath 返回指定路径字符串的绝对路径。

        GetInvalidFileNameChars 获取包含不允许在文件名中使用的字符的数组。

        GetInvalidPathChars 获取包含不允许在路径名中使用的字符的数组。

        GetPathRoot 获取指定路径的根目录信息。

        GetRandomFileName 返回随机文件夹名或文件名。

        GetTempFileName 创建磁盘上唯一命名的零字节的临时文件并返回该文件的完整路径。

        GetTempPath 返回当前用户的临时文件夹的路径。

        HasExtension 确定路径是否包括文件扩展名。

        IsPathRooted 获取指示指定的路径字符串是否包含根的值。

        

          程序实例:

          static void Main(string[] args)

            {

                if (args == null || args.Length < 1)

                    return;

                string myPath = args[0];

                Console.WriteLine("目录名称:{0}", Path.GetDirectoryName(myPath));

                Console.WriteLine("路径扩展名:{0}", Path.GetExtension(myPath));

                Console.WriteLine("文件名:{0}", Path.GetFileName(myPath));

                Console.WriteLine("不带扩展名的名称:{0}", Path.GetFileNameWithoutExtension(myPath));

                Console.WriteLine("绝对全路径:{0}", Path.GetFullPath(myPath));

                Console.WriteLine("根目录:{0}", Path.GetPathRoot(myPath));

                Console.WriteLine("不带根目录的路径:{0}", Path.GetFullPath(myPath).Remove(0, 3));

                Console.ReadKey();

            }

  • 相关阅读:
    python连接SMTP的TLS(587端口)发邮件python发邮件(带认证,587端口)202010
    JAVA抓取通过JS渲染的网站(动态)网页数据
    moviepy音视频剪辑:与大小相关的视频变换函数详解
    区块链知识博文1: 共识算法之争(PBFT,Raft,PoW,PoS,DPoS,Ripple)
    MoviePy v2.0.0.dev1尚不成熟,不建议大家使用
    区块链学习2:一些概念性的基础知识笔记
    老猿学5G:3GPP 5G规范中的URI资源概念
    区块链学习1:Merkle树(默克尔树)和Merkle根
    老猿Python博客文章目录索引
    老猿学5G:融合计费场景的Nchf_ConvergedCharging_Create、Update和Release融合计费消息交互过程
  • 原文地址:https://www.cnblogs.com/meimao5211/p/3338685.html
Copyright © 2011-2022 走看看