. NET Io:path, File, FileInfo, Directory, DirectoryInfo, DriveInfo, FileSystemWatcher

Source: Internet
Author: User

1. Managing the file system

Typically, applications have the need to save data and retrieve data.

1.1 Using the Path class to access file paths


"Path Common method": http://www.cnblogs.com/tangge/archive/2012/10/30/2746458.html#a3

1.2 Accessing files using the file and FileInfo classes 1.2.1

123456789101112131415161718 staticvoidMain(string[] args){    stringsourceFileName = @"F:\a.txt"; //源文件    stringdestFileName = @"c:\a.txt"; //目标文件    //如果源文件不存在    if(!File.Exists(sourceFileName))    {        File.Create(sourceFileName).Close();    }    //如果目标文件存在,先删除    if(File.Exists(destFileName))    {        File.Delete(destFileName);    }    File.Copy(sourceFileName, destFileName);    File.Delete(sourceFileName);}

Duplicate record

String sourcefilename = @ "F:\a.txt"; Source file            String destfilename = @ "C:\a.txt";//target file            StreamWriter SW = File.appendtext (destfilename);            Sw. WriteLine (String. Format ("{0} copy complete", DateTime.Now));            Sw. Flush ();            Sw. Close ();

1.2.2 FileInfo Class

Length

private static void Main (string[] args)        {            string path = @ "E:\ zhongtian it\ video \dvd-asp.net\dvd-Zhang Bo. Netc1001\io-Zhang Bo. Netc1001\1 Last Review _ homework explained. avi ";            FileInfo fi = new FileInfo (path);            Console.WriteLine (                String. Format ("This file is {0:#.00}m", fi.) Length/(1024x768 * 1024x768));        }

1.3 Accessing Directories 1.3.1 Directory classes using the directory and DirectoryInfo classes

string path = @ "F:\tt\aeg\www";            if (directory.exists (path))            {                directory.delete (path);            }            else            {                directory.createdirectory (path);            }

string path = @ "F:\tt\aeg";            GetFiles retrieving file list            string[] aa= directory.getfiles (path);            foreach (var a in aa)            {                Console.WriteLine (a);            }                        Console.WriteLine ("------------");            GetDirectories Retrieving the Folder List            foreach (var s in directory.getdirectories (path))            {                Console.WriteLine (s);            }            Console.WriteLine ("------------");            GetDirectories retrieving the folder and file list            foreach (var s in directory.getfilesystementries (path))            {                Console.WriteLine (s);            }

1.3.2 DirectoryInfo Class

1.4 Using the DriveInfo class to access the drive

Console.WriteLine ("Drive {0}, type {1},", Dr. Name,dr. DriveType);                if (Dr. IsReady)                //{                    Console.WriteLine ("Free space is {0}", Dr. Availablefreespace);                //}

Console.WriteLine ("Drive {0}, type {1},", Dr. Name, Dr. DriveType);                if (Dr. IsReady)//device is ready                {                    Console.WriteLine ("\ t available space is {0}g", Dr. availablefreespace/(1024*1024*1024)); 41G                    Console.WriteLine ("\ t partition format {0}\n", Dr. Driveformat);   NTFS                           }

1.5 FileSystemWatcher Class

2. Using a byte stream

3. Managing Application Data

4. Efficient operation of strings

(GO). NET Io:path, File, FileInfo, Directory, DirectoryInfo, DriveInfo, FileSystemWatcher

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.