FAQ,good luck!
12.1.1 How do I read from a text file?
First, use a System.IO.FileStream object to open the file:FileStream fs = new FileStream( @"c:\test.txt", FileMode.Open, FileAccess.Read );FileStream inherits from Stream, so you can wrap the FileStream object with a StreamReader object. This provides a nice interface for processing the stream line by line:StreamReader sr = new StreamReader( fs );
string curLine;
while( (curLine = sr.ReadLine()) != null )
Console.WriteLine( curLine );Finally close the StreamReader object:sr.Close();Note that this will automatically call Close() on the underlying Stream object, so an explicit fs.Close() is not required. 12.1.2 How do I write to a text file?
Similar to the read example, except use StreamWriter instead of StreamReader. 12.1.3 How do I read/write binary files?
Similar to text files, except wrap the FileStream object with a BinaryReader/Writer object instead of a StreamReader/Writer object.
12.1.4 How do I delete a file?
Use the static Delete() method on the System.IO.File object:File.Delete( @"c:\test.txt" );
12.1.1 How do I read from a text file?
First, use a System.IO.FileStream object to open the file:FileStream fs = new FileStream( @"c:\test.txt", FileMode.Open, FileAccess.Read );FileStream inherits from Stream, so you can wrap the FileStream object with a StreamReader object. This provides a nice interface for processing the stream line by line:StreamReader sr = new StreamReader( fs );
string curLine;
while( (curLine = sr.ReadLine()) != null )
Console.WriteLine( curLine );Finally close the StreamReader object:sr.Close();Note that this will automatically call Close() on the underlying Stream object, so an explicit fs.Close() is not required. 12.1.2 How do I write to a text file?
Similar to the read example, except use StreamWriter instead of StreamReader. 12.1.3 How do I read/write binary files?
Similar to text files, except wrap the FileStream object with a BinaryReader/Writer object instead of a StreamReader/Writer object.
12.1.4 How do I delete a file?
Use the static Delete() method on the System.IO.File object:File.Delete( @"c:\test.txt" );
dd即为此目录的信息,
如删除此目录:dd.delete
返回此目录的文件夹信息Directory[] dsub=dd.GetDirectories();
返回此目录的文件信息 File[] fsub=dd.GetFilees();读取文件信息的类:File,这个类可对文件进行删除,复制等。
如:Name属性返回文件的名称,FullName返回文件的完整路径还有StreamWriter,用于向文件内写信息的类,如:
FileStream fs=new FileStream("test.txt",FileMode.OpenOrCreate);
StreamWriter sw=new StreamWriter(fs);
sw.WriteLine("Hello!!!");
sw.Close();StreamReader,读取文件信息的类,如:
FileStream fs=new FileStream("test.txt",FileMode.Open);
StreamReader sr=new StreamReader(fs);
读出一行的信息:
String ss=sr.ReadLine();
sr.Close();
我这些希望能给你一点帮助,其实最常用的也就这些了。其他详细的信息。SDK里很全的,你可以再看一看。
如够您希望操作一个文件可以使用System.IO.FileStream类。如下所示:FileStream fs = File.Create("CheckFile.cs");
fs.Close();如果您希望读取或设置一个文件的某些属性,下面的方法将对你有所帮助:File.GetCreationTime 读取创建时间
File.SetCreationTime 设置创建时间
File.GetLastAccessTime 读取最后访问时间
File.SetLastAccessTime 设置最后访问时间
File.GetLastWriteTime 读取最后修改时间
File.SetLastWriteTime 设置最后修改时间
File.GetAttributes 读取文件属性
File.SetAttributes 设置文件属性