C # file Operations Encyclopedia
Last Update:2018-07-18
Source: Internet
Author: User
This article is from: http://space.itpub.net/23109131/viewspace-662112 1. Create a folder
Using System.IO;
Directory.CreateDirectory (%%1); 2. Create a file
Using System.IO;
File.create (%%1); 3. Delete Files
Using System.IO;
File.delete (%%1); 4. Delete Folder
Using System.IO;
Directory.delete (%%1); 5. Delete all folders in a directory
Using System.IO;
foreach (String dirstr in Directory.getdirectories (%%1))
{
DirectoryInfo dir = new DirectoryInfo (DIRSTR);
ArrayList folders=new ArrayList ();
filesysteminfo[] Filearr = dir. Getfilesysteminfos ();
for (int i = 0; i < folders. Count; i++)
{
FileInfo f = folders[i] as FileInfo;
if (f = null)
{
DirectoryInfo d = folders[i] as DirectoryInfo;
D.delete ();
}
}
} 6. Empty folder
Using System.IO;
Directory.delete (%%1,true);
Directory.CreateDirectory (%%1); 7. Read the file
Using System.IO;
StreamReader s = file.opentext (%%1);
string%%2 = null;
while ((%%2 = S.readline ())!= null) {
%%3
}
S.close (); 8. Write to File
Using System.IO;
FileInfo f = new FileInfo (%%1);
StreamWriter w = f.createtext ();
W.writeline (%%2);
W.close (); 9. Write Random file
Using System.IO;
byte[] DataArray = new Byte[100000];//new Random (). Nextbytes (DataArray);
using (FileStream FileStream = new FileStream (%%1, FileMode.Create)) {
Write the data to the file, in byte by byte.
for (int i = 0; i < dataarray.length; i++) {
Filestream.writebyte (Dataarray[i]);
}
Set the stream position to the beginning of the file.
Filestream.seek (0, Seekorigin.begin);
Read and verify the data.
for (int i = 0; i < filestream.length; i++) {
if (Dataarray[i]!= filestream.readbyte ()) {
Write Data error
Return
}
}
"Data Flow" +filestream.name+ "verified"
} 10. read File properties
Using System.IO;
FileInfo f = new FileInfo (%%1);//f.creationtime,f.fullname
if ((F.attributes & Fileattributes.readonly)!= 0) {
%%2
}
else{
%%3
} 11. Write properties
Using System.IO;
FileInfo f = new FileInfo (%%1);
Set read-only
F.attributes = Myfile.attributes | Fileattributes.readonly;
Set to write
F.attributes = myfile.attributes & ~fileattributes.readonly; 12. Enumerate all the folders in a folder
Using System.IO;
foreach (String%%2 in Directory.getdirectories (%%1)) {
%%3
}
/*
DirectoryInfo dir = new DirectoryInfo (%%1);
fileinfo[] files = dir. GetFiles ("*.*");
foreach (FileInfo%%2 in files) {
%%3
}
* * 13. Copy Folder
/*
Using System.IO;
Using System.Collections;
*/
String path = (%%2.lastindexof ("\") = =%%2.length-1)? %%2:%%2+ "\";
String parent = Path.getdirectoryname (%%1);
Directory.CreateDirectory (path + path.getfilename (%%1));
DirectoryInfo dir = new DirectoryInfo ((%%1.lastindexof ("\") = =%%1.length-1)?%%1:%%1 + "\");
filesysteminfo[] Filearr = dir. Getfilesysteminfos ();
queue<filesysteminfo> Folders = new Queue<filesysteminfo> (dir. Getfilesysteminfos ());
while (folders.count>0)
{
FileSystemInfo tmp = Folders.dequeue ();
FileInfo f = tmp as FileInfo;
if (f = null)
{
DirectoryInfo d = tmp as DirectoryInfo;
Directory.CreateDirectory (D.fullname.replace) (parent. LastIndexOf ("\") = = parent. Length-1)? Parent:parent + "\", path));
foreach (FileSystemInfo fi in D.getfilesysteminfos ())
{
Folders.enqueue (FI);
}
}
Else
{
F.copyto (f.fullname.replace (parent, path));
}
14. Copy all folders under the directory to another folder
/*
Using System.IO;
Using System.Collections;
*/
DirectoryInfo d = new DirectoryInfo (%%1);
foreach (DirectoryInfo dirs in D.getdirectories ())
{
Queue<filesysteminfo> al = new Queue<filesysteminfo> (dirs. Getfilesysteminfos ());
while (al. Count > 0)
{
FileSystemInfo temp = al. Dequeue ();
FileInfo file = temp as FileInfo;
if (file = = null)
{
DirectoryInfo directory = temp as DirectoryInfo;
Directory.CreateDirectory (path + directory). Name);
foreach (FileSystemInfo FSI in directory. Getfilesysteminfos ())
Al. Enqueue (FSI);
}
Else
File.Copy (file. FullName, path + file. Name);
}
} 15. Moving folders
/*
Using System.IO;
Using System.Collections;
*/
string filename = Path.getfilename (%%1);
String path= (%%2.lastindexof ("\") = =%%2.length-1)? %%2:%%2 + "\";
if (Path.getpathroot (%%1) = = Path.getpathroot (%%2))
Directory.move (%%1, path + filename);
Else
{
String parent = Path.getdirectoryname (%%1);
Directory.CreateDirectory (path + path.getfilename (%%1));
DirectoryInfo dir = new DirectoryInfo ((%%1.lastindexof ("\") = =%%1.length-1)?%%1:%%1 + "\");
filesysteminfo[] Filearr = dir. Getfilesysteminfos ();
queue<filesysteminfo> Folders = new Queue<filesysteminfo> (dir. Getfilesysteminfos ());
while (Folders.count > 0)
{
FileSystemInfo tmp = Folders.dequeue ();
FileInfo f = tmp as FileInfo;
if (f = null)
{
DirectoryInfo d = tmp as DirectoryInfo;
DirectoryInfo Dpath = new DirectoryInfo (D.fullname.replace (parent. LastIndexOf ("\") = = parent. Length-1)? Parent:parent + "\", path));
Dpath. Create ();
foreach (FileSystemInfo fi in D.getfilesysteminfos ())
{
Folders.enqueue (FI);
}
}
Else
{
F.moveto (f.fullname.replace (parent, path));
}
}
Directory.delete (%%1, true);
16. Move all folders under the directory to another directory
/*
Using System.IO;
Using System.Collections;
*/
string filename = Path.getfilename (%%1);
if (Path.getpathroot (%%1) = = Path.getpathroot (%%2))
foreach (String dir in Directory.getdirectories (%%1))
Directory.move (dir, Path.Combine (%%2,filename));
Else
{
foreach (String Dir2 in Directory.getdirectories (%%1))
{
String parent = Path.getdirectoryname (DIR2);
Directory.CreateDirectory (Path.Combine (%%2, Path.getfilename (DIR2)));
String dir = (Dir2. LastIndexOf ("\") = = Dir2. Length-1)? Dir2:dir2 + "\";
DirectoryInfo dirdir = new DirectoryInfo (dir);
filesysteminfo[] Filearr = Dirdir. Getfilesysteminfos ();
queue<filesysteminfo> Folders = new Queue<filesysteminfo> (dirdir. Getfilesysteminfos ());
while (Folders.count > 0)
{
FileSystemInfo tmp = Folders.dequeue ();
FileInfo f = tmp as FileInfo;
if (f = null)
{
DirectoryInfo d = tmp as DirectoryInfo;
DirectoryInfo Dpath = new DirectoryInfo (D.fullname.replace (parent. LastIndexOf ("\") = = parent. Length-1)? Parent:parent + "\",%%2);
Dpath. Create ();
foreach (FileSystemInfo fi in D.getfilesysteminfos ())
{
&nb