C# 目录操作(新建,删除),获取目录下文件列表

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Linq;

namespace DirectoryApp
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.WriteLine("***** Fun with Directory(Info) *****\n");
      ShowWindowsDirectoryInfo();
      Console.ReadLine();

      DisplayImageFiles();
      Console.ReadLine();

      ModifyAppDirectory();
      Console.ReadLine();

      FunWithDirectoryType();
      Console.ReadLine();
    }

    #region Show basic directory info
    static void ShowWindowsDirectoryInfo()
    {
      DirectoryInfo dir = new DirectoryInfo(@"C:\Windows");

      // Dump directory information.
      Console.WriteLine("***** Directory Info *****");
      Console.WriteLine("FullName: {0}", dir.FullName);
      Console.WriteLine("Name: {0}", dir.Name);
      Console.WriteLine("Parent: {0}", dir.Parent);
      Console.WriteLine("Creation: {0}", dir.CreationTime);
      Console.WriteLine("Attributes: {0}", dir.Attributes);
      Console.WriteLine("Root: {0}", dir.Root);
      Console.WriteLine("**************************\n");
    }
    #endregion

    #region Show info on image types
    static void DisplayImageFiles()
    {
      DirectoryInfo dir = new DirectoryInfo(@"C:\Windows\Web\Wallpaper");

      // Get all files with a *.bmp extension.
      FileInfo[] imageFiles = dir.GetFiles("*.jpg");

      // How many were found?
      Console.WriteLine("Found {0} *.jpg files\n", imageFiles.Length);

      // Now print out info for each file.
      foreach (FileInfo f in imageFiles)
      {
        Console.WriteLine("***************************");
        Console.WriteLine("File name: {0}", f.Name);
        Console.WriteLine("File size: {0}", f.Length);
        Console.WriteLine("Creation: {0}", f.CreationTime);
        Console.WriteLine("Attributes: {0}", f.Attributes);
        Console.WriteLine("***************************\n");
      }
    }
    #endregion

    #region Modify directory structure
    static void ModifyAppDirectory()
    {
      DirectoryInfo dir = new DirectoryInfo(".");

      // Create \MyFolder off initial directory.
      dir.CreateSubdirectory("MyFolder");

      // Capture returned DirectoryInfo object.
      DirectoryInfo myDataFolder = dir.CreateSubdirectory(@"MyFolder2\Data");

      // Prints path to ..\MyFolder2\Data.
      Console.WriteLine("New Folder is: {0}", myDataFolder);
    }
    #endregion

    #region Using Directory type
    static void FunWithDirectoryType()
    {
      // List all drives on current computer.
      string[] drives = Directory.GetLogicalDrives();
      Console.WriteLine("Here are your drives:");
      foreach (string s in drives)
        Console.WriteLine("--> {0} ", s);

      // Delete what was created.
      Console.WriteLine("Press Enter to delete directories");
      Console.ReadLine();
      try
      {
        Directory.Delete(string.Format(@"{0}\MyFolder",
          Environment.CurrentDirectory));

        // The second parameter specifies if you
        // wish to destroy any subdirectories.
        Directory.Delete(string.Format(@"{0}\MyFolder2",
          Environment.CurrentDirectory), true);
      }
      catch (IOException e)
      {
        Console.WriteLine(e.Message);
      }
    }
    #endregion
  }
}

转载于:https://my.oschina.net/duluo180/blog/8135

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值