zoukankan      html  css  js  c++  java
  • 驱动器,文件夹和文件

      Directory和DirctioryInfo类是用于创建,移动和枚举目录和子目录的静态方法,如复制,移动,重命名,创建和删除目录。也可用于获取和设置与目录的创建访问及写入操作相关的DateTime信息。

      由于所有的Directory方法都是静态的,因此如果只想执行一个操作,那么使用Directory方法的效率比使用相应的DirectoryInfo实例方法可能更高。大多数Directory方法要求当前操作目录的路径

      Directory类的静态方法对所有方法都执行安全检查。如果打算多次重用某个对象,可以考虑改用DirectoryInfo的相应实例方法,因为并不总是需要安全检查

      注意,在接受路径作为输入字符串的成员中,路径必须是格式良好的,否则将引发异常。例如,如果路径以空格开头,不会自动删除空格,因此空格开头的格式不正确,否则将引发异常。同样,路径或路径的组合不能被完全限制两次,例如,C:Temp C:Windows在大多数情况下也将引发异常。

      下面的代码示例确定的目录是否存在,如果存在,则删除该目录;如果不存在,则创建该目录,然后移动此目录,在其中创建一个文件并对文件进行计数。

    string path=@"....MyDif";//声明要操作的目录

    string target=@"....TestDir";

    try

    {

      if(!Directory.Exists(path))    //确定目录是否存在

      {  

        Directory.CreateDirectory(path);  //如果源目录不存在则创建目录

      }

      if(Directory.Exists(target))

      {

        Directory.Delete(target,true);  //如果目标目录存在则删除目录

      }

      Directory.Move(path,target);//移动源目录到目标目录

      File.CreateText(target+@"myfile.txt");    //在目录中产生文件

      //Directory.GetFile(target).Length;  //获取目录target的文件数

      Console.WriteLine("The number of files in {0} is{1}",target,Directory.GetFiles(target).Length);

    }

    catch(Exception e)

    {

      Console.WriteLine("The process failed:{0}",e.ToString());

    }

    finally{}

      程序运行后的项目文件夹中产正一个新文件夹TestDir,并在TestDir中产生一个空文本文件myfile.txt。屏幕输出如下:

    The number of files in ....TestDir is 1

    使用I/O类创建目录中具有exe拓展名的所有文件列表

    string path=".";

    if(args.Length>0)

    {

      if(Directory.Exists(args[0]))

        path=args[0];

      else

        Console.WriteLine("{0}not found;using current directory:",args[0]);

    }

    DirectoryInfo dir=new DirectoryInfo(path);

    foreach(FileInfo f in dir.GetFiles("*.exe"))

    {

      string name=f.name;

      long size=f.Length;

      DateTime creationTime=f.CreationTime;

      Console.WriteLine("{0,-12:NO}{-1,-20:g}{2}",size,creationTime,name);

    }

    Console.ReadLine();

    在本例中,DirectoryInfo是当前目录,用(.)表示,代码列出了当前目录中具有exe拓展名的所有文件,同时还列出了这些文件的大小,创建时间和名称。假设程序名FileExample,此代码的输出可能如下:

    5,120          2014/3/9 10:10     FileExample.exe

    11,584    2014/3/9 10:11     FileExample.vshost.exe

      如果需要另一个目录(如C:WINDOWSsystem32)中的exe文件列表,用Windows系统的"运行"指令,将目录C:WINDOWSsystem32作为参数放在程序名后而运行:

    FileExample C:WINDOWSsystem32

    下面的示例演示如何计算目录大小

    public static double DirSize(DirectoryInfo d)

    {

      double Size=0;

      FileInfo[]fis=d.GetFiles();//GetFiles返回当前目录的文件列表

      foreach(FileInfo fi in fis)

        Size+=fi.Length;

      DirectoryInfo[]dis=d.GetDirectories();//GetDirectories返回当前目录的子目录

      foreach(DirectoryInfo di in dis)

        Size+=DirSize(di);

      return(Size);

    }

    public static void Main(string []args)

    {

      if(args.Length!=1)

        Console.WriteLine("You mush provide a directory argument at the command line.");

      else

      {

        DirectoryInfo d=new DirectoryInfo(args[0]);

        Console.WriteLine("The size of{0} and its subdirectories is{1} bytes.",d,DirSize(d));

      }

      Console.ReadLine();

    }

    使用Windows系统的“运行”指令运行此程序,例如

    GetDirectroySize.exe c:WINDOWSsystem

    输出如下:

    The size of c:WINDOWSsystem and its subdirectoryies is 700380 bytes

    下面的示例获取系统特殊文件路径

    Console.WriteLine()

    //指向系统文件夹C:WINNTSystem32

    Console.WriteLine("GetFolderPath:{0}",Environment.GetFolderPath(Environment.SpecialFolder.System));

    //指向“我的文档”文件夹

    Console.WriteLine("GetFolderPaht:{0}",Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));

    输出如下:

    GetFolderPath:c:WINNTSystem32

    GetFolderPath:C:Documents and SettingsAdministratorMy Documents

      Path类对包含文件或目录路径信息的String实例执行操作,这些操作时以跨平台的方法执行的。.Net Framework不支持通过由设备名称构成的路径(如\.PHYSICALDRIVEO)直接访问物理硬盘。路径可以包含绝对或相对位置信息。绝对路径完整地指定一个位置。若要确定当前目录,则调用Directory.GetCurrentDirectory.

      下面的代码实例演示Path类的某些主要成员:

      string path1=@"c: empMyTest.txt";

      string path2=@"c: empMyTest";

      string path3=@"temp";

    //确定路径是否包含文件拓展名

    if(Path.HasExtension(path1))

      Console.WriteLine("{0}has an extension",path1);

    if(!Path.HasExtension(path2))

      Console.WriteLine("{0}has no extension.",path2);

    //获取一个值,指示指定的路径字符串是包含绝对路径信息还是包含相对路径信息

    if(!Path.IsPathRooted(path3))

      Console.WriteLine("The string{0} contains no root information.",path3);

    //返回制定路径字符串的绝对路径

    Console.WriteLine("The full path of{0} is {1}",path3,Path.GetFullPath(path3));

    //返回当前系统的临时文件夹路径

    Console.WriteLine("{0} is the location for temporary files.",Path.GetTempPath());

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

    Console.WriteLine("{0} is a file available for use.",Path.GetTempFileName());

    输出如下:

    C: empMytest.txt has an extension

    C: empMytest has no extension.

    The string temp contaions no root informaion.

    The full path of temp is

    C:C#1010.210.2.2PathMemberPathMemberinDebug emp

  • 相关阅读:
    deb app install under terminal
    修改代理
    virtualenv install &usage
    what a pietty
    晚上想睡觉不要喝可乐
    ubuntu server 调教
    修改代理
    修改网关为静态IP
    infobright install tips
    i like fujishu laptop more than mac
  • 原文地址:https://www.cnblogs.com/yk1992/p/3588749.html
Copyright © 2011-2022 走看看