• c#基础语言编程-Path和Directory


    引言
    在程序常会对文件操作,在对文件操作中需要对文件路径的进行定位,在.Net中针对寻找文件提供两个静态类以供调用,Path和Directory。

    Path类

    来自命名空间SYstem.IO,Path类提供的方法是对党获取文件路径后处理方法,更应该算是对字符串操作。
    例子代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace Example
    {
    
        class Program
        {
    
            static void Main(string[] args)
            {
                // Path类就是对字符串的操作,与实际的文件没有任何关系
                string path = @"C:Documents and SettingsAdministratorMy DocumentsexercisesITCAST-102ipmsg.exe";
                //获取文件名,带后缀
                string filename = Path.GetFileName(path);
                Console.WriteLine(filename);
    
    
                //获取文件名不带后缀
                string fname = Path.GetFileNameWithoutExtension(path);
                Console.WriteLine(fname);
    
    
                //只获取文件后缀
                string ext = Path.GetExtension(path);
                Console.WriteLine(ext);
    
    
                //截取文件的路径部分,不带文件名、
                string filePath = Path.GetDirectoryName(path);
                Console.WriteLine(filePath);
    
                //只是把字符串中的文件的路径改了,与磁盘上的文件无关。
                string newpath = Path.ChangeExtension(path, ".txt");
                Console.WriteLine(newpath);
    
                string s1 = @"c:abcxyzaa";
                string s2 = "abc.avi";
    
                //连接两个路径。
                string full = Path.Combine(s1, s2);  //s1 + s2;
                Console.WriteLine(full);
                Console.ReadKey();
            }
        }
    }

    结果为:
    这里写图片描述
    这个类常用的就是Path.Combine,Path.GetFileName,Path.GetDirectoryName,Path.GetFileNameWithoutExtension。
    当然能在应用中也能获取临时文件的路径。这个类的使用常常伴随Directory类操作。

    Directory类

    Path是对文件的路径进行操作,Net提供了Directory类则是对文件的目录进行操作,能够对文件目录进行操作,如:获取文件目录下所有子文件、子文件夹、指定的文件夹等等。
    常用的就是:
    Directory.GetDirectories():读取指定目录下的所有子目录
    Directory.GetFiles(): 获取指定目录下的所有的子文件
    Directory.Exists():验证指定的文件是否存在
    Directory.CreateDirectory():创建指定目录

    string path = @"C:DRIVER";
      //读取指定目录下的所有子目录(文件夹)
      //string[] dirs = Directory.GetDirectories(path);
      //获取路径下含有"a"的文件夹,当然也可设置为"*.txt".
     string[] dirs = Directory.GetDirectories(path,"*a*",SearchOption.AllDirectories);
     //在获取目录基础上可以递归实现读取子目录下的子目录。
     foreach (var item in dirs)
       {
            Console.WriteLine(item);
       }
           Console.WriteLine("=============================");
        //获取指定目录下的所有的子文件
         string[] files = Directory.GetFiles(path,"*b*",SearchOption.AllDirectories);
      foreach (string item in files)
      {
         Console.WriteLine(item);
      }
         Console.ReadLine();

    获取路径

    前面两者提供了对文件目录和文件路径进行操作,那如何获取路径?
    1、通过对话框获取工作路径
    2、通过函数获取工作路径。

    • 通过对话框获取工作路径
    OpenFileDialog filename = new OpenFileDialog();
     filename.Filter ="文本文件(*.txt)|*.txt|所有文件 (*.*)|*.*";
    if (filename.ShowDialog() == DialogResult.OK)
     {
         textBox1.Text = filename.FileName.ToString();
     }
    • 通过函数获取工作路径

    1、取得控制台应用程序的根目录方法
    方法1、Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径
    方法2、AppDomain.CurrentDomain.BaseDirectory 获取基目录,它由程序集冲突解决程序用来探测程序集
    2、取得Web应用程序的根目录方法
    方法1、HttpRuntime.AppDomainAppPath.ToString();//获取承载在当前应用程序域中的应用程序的应用程序目录的物理驱动器路径。用于App_Data中获取
    方法2、Server.MapPath(“”) 或者 Server.MapPath(“~/”);//返回与Web服务器上的指定的虚拟路径相对的物理文件路径
    方法3、Request.ApplicationPath;//获取服务器上ASP.NET应用程序的虚拟应用程序根目录
    3、取得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中根据相对路径来获取绝对路径
    上面加粗就是常用获取工作路径的方法,工作路径就是exe或者dll的生成目录,来自属性>生成>输出目录的设置。
    例如:设置输出目录为
    这里写图片描述
    代码为:

    //获取和设置当前目录的完全限定目录(根据输出目录基础上获取的)
                string str = System.Environment.CurrentDirectory;
                //2.获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。
                //string str = System.Windows.Forms.Application.StartupPath;
                //Result: C:xxxxxx
                //3.获取新的 Process 组件并将其与当前活动的进程关联的主模块的完整路径,包含文件名。
                string str1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
    
                //4.获取当前 Thread 的当前应用程序域的基目录,它由程序集冲突解决程序用来探测程序集。
                string str4 = System.AppDomain.CurrentDomain.BaseDirectory;
    
                //5.获取应用程序的当前工作目录。
                string str5 = System.IO.Directory.GetCurrentDirectory();
    
                //6.获取和设置包含该应用程序的目录的名称。
                string str6 = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
    
                //7.获取当前进程的完整路径,包含文件名。
                //string str7 = this.GetType().Assembly.Location;
                //Result: C:xxxxxxxxx.exe
                //8.获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
                //string str8 = System.Windows.Forms.Application.ExecutablePath;
    
                Console.WriteLine(str);
                Console.WriteLine(str1);            
                Console.WriteLine(str4);
                Console.WriteLine(str5);
                Console.WriteLine(str6);
               // Console.WriteLine(str7);
                Console.ReadKey();

    这里写图片描述

  • 相关阅读:
    具有快表的地址变换机构
    npm更换淘宝镜像
    内存扩充技术
    内存管理的概念
    内存的基础知识
    102. 二叉树的层序遍历
    104. 二叉树的最大深度
    206. 反转链表
    mysql 多字段查询,全局搜素
    java 处理html转义字符
  • 原文地址:https://www.cnblogs.com/polly333/p/4498385.html
Copyright © 2020-2023  润新知