C#.net Disk Management and file operations

Source: Internet
Author: User

The original Published time: 2008-08-08--from my Baidu article [imported by moving tools]

Namespaces that need to be introduced:

Using System.IO;
Using System.Text;

Private DirectoryInfo di;
Private FileInfo fi;

Files and folders FileSystemInfo

File FileInfo

Folder DirectoryInfo

Create a file

FileInfo fi=new FileInfo (path);
if (!fi. Exists)
Fi. Create ();

Create a folder
DirectoryInfo di = new DirectoryInfo (path);
if (!di. Exists)
Di. Create ();

Delete Folder Di. Delete ();

Delete file fi. Delete ();

Move Folder di. MoveTo (NewPath);

Move file FI. MoveTo (Newfpath);

Rename Folder Di. MoveTo (Newfpath); "The original path is the same, but the file name is different"

Rename file fi. MoveTo (Newfpath); "The original path is the same, only the file name is different"

Copy file: FI. CopyTo (Newfpath);

To copy a folder:

protected void Dircopy (String oldpath,string newpath)
{
DI = new DirectoryInfo (OldPath);
foreach (FileSystemInfo fsi in Di. Getfilesysteminfos ())
{
If (FSI is FileInfo)
{
fi = (FileInfo) FSI;
if (! Directory.Exists (NewPath))
{
DirectoryInfo newdir= directory.createdirectory (NewPath);
Fi. CopyTo (newdir.fullname+ ");
}
Else
{
Fi. CopyTo (newpath+ ");
}
}
Else
{
DirectoryInfo child_di= (DirectoryInfo) FSI;
String Olddir=child_di. FullName;
String Dirname=child_di. Fullname.substring (Child_di. Fullname.lastindexof (");
String Newchildpath=path.combine (Newpath,dirname);
if (! Directory.Exists (Olddir))
Directory.CreateDirectory (Olddir);
Dircopy (Olddir,newchildpath);
}

}
}

Editing of files:

FileStream fs = new FileStream (fpath,filemode.create, FileAccess.Write);
StreamWriter SW = new StreamWriter (FS, Encoding.default);
Sw. WriteLine (information);
Sw. Close ();
Fs. Close ();

Read the file:

File.readalltext (Fpath,encoding.default);

Some properties of files and folders are obtained:

DI = new DirectoryInfo (Fpath);
foreach (FileSystemInfo fsi in Di. Getfilesysteminfos ())
{
If (FSI is FileInfo)
{
FileInfo fi = (FileInfo) FSI;
FName = fi. Name;
if (FI. Extension.length < 1)
Fexp = "";
Else
Fexp = fi. Extension.remove (0, 1);
Fsize = fi. Length.tostring ();
Ftime = fi. Lastwritetime.tostring ();
}
Else
{
Di = (DirectoryInfo) FSI;
fname = di. Name;
Fexp = "folder";
Fsize = "";
Ftime = di. Lastwritetime.tostring ();
}
......

}

C#.net Disk Management and file operations

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.