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