c#学习笔记(操作文件和注册表)

System.IO命名空间下包含了文件输入输出相关的类
BinaryReader/BingarWriter:二进制形式读写
BufferedStream:给另一流上的读写添加缓冲层,无法继承此类
Directory/DirectoryInfo/DirectoryNotFoundException:提供目录操作的静态/实例方法
DriveInfo/DriveNotFoundException:提供驱动器信息
EndOfStreamException:读操作试图超出流的末尾
ErrorEventArgs:
File/FileInfo/FileNotFoundException:提供文件操作的静态/实例方法
FileLoadException:当找到托管程序集却不能加载它时引发的异常
FileStream:以文件为主的Stream,支持同步或异步读写操作。通过Lock和Unlock方法实现单通道操作。
FileSystemEventArgs:提供目录事件Changed、Created、Deleted的数据
FileSystemInfo:为FileInfo和DirectoryInfo对象提供基类
FileSystemWatcher:侦听文件系统更改通知
InternalBufferOverflowException:内部缓冲区溢出时引发的异常
InvalidDataException:数据流格式无效时的异常
IODescriptionAttribute:设置可视化设计器在引用事件、扩展程序或属性时可显示的说明
IOException:发生I/O错误
MemoryStream:创建支持存储区为内存的流
Path/pathTooLongException:对文件或目录的路径信息操作
RenamedEventArgs
Stream:提供字节序列的一般视图
StreamReader/StreamWriter:实现一个TextReader/TextWriter,从字节流读写
StringReader/StringWriter:TextReader/TextWriter,从字符串读写
TextReader/TextWriter:连续有序字符系列的读写器
UnmanagedMemoryStream:提供从托管代码访问非托管代码内存块的能力

 

FileStream类读写文件

 

StreamWrite/StreamReader类读写文件比FileStream更为方便,无需额外的数据类型转换操作 

StreamWriter/StreamReader读写文件

 

BinaryWriter/BinaryReader以二进制将基元类型写入流,并支持用特定的编码写入字符串。输出文件需用查看16进制的阅读器才能正确阅读 

BinaryWriter/BinaryReader类读写文件

 

FileSystemWatcher可用于监视文件的操作

 

FileSystemWatcher监视文件变化

 

异步IO:
应用异步IO适用于对大型文件的读写操作,它的步骤是:
a.自定义一个无返回值的方法,此方法须接受一个IAsyncResult类型的接口做参数
b.声明一个AsynCallBack代理类型,以上面自定义的方法名称为参数
c.引用方法BeginRead读取文件或BeginWrite写操作
d.在自定义方法中使用方法EndRead取得BeginRead所读取的字节数组
e.BeginRead和BeginWrite的第四参数指定异步IO完成时调用的方法,最后一个参数为状态对象,设为null即可

异步写操作
class  AsynchronousIO
    {
        FileStream myFileStream;
        
double  lngNumber  =   100000000 ;

        
static   void  Main( string [] args)
        {
            AsynchronousIO myAsynchronousIO 
=   new  AsynchronousIO();
            myAsynchronousIO.longProcessWrite();
            Console.Read();
        }

        
public   void  longProcessWrite()
        {
            
byte [] b  =   new   byte [ 100000000 ];
            
for  ( int  i  =   0 ; i  <  lngNumber; i ++ ) { b[i]  =  ( byte )i; }
            Console.WriteLine(
" 写入大量数据.... " );
            AsyncCallback myAsnycCallback 
=   new  AsyncCallback(WriteEnd);
            myFileStream 
=   new  FileStream( @" c:\tnt.txt " , FileMode.Create);
            
// 执行到BeginWrite后,不会等待,而是继续往下执行。
            
// 当异步IO完成后,委托实例myAsnycCallback所关联的方法WriteEnd被调用
            myFileStream.BeginWrite(b,  0 , b.Length, myAsnycCallback,  null );
            Console.WriteLine(
" 正在将大量数据写入 "   +   @" c:\tnt.txt ! " );

        }
        
public   void  WriteEnd(IAsyncResult asyncResult)
        {
            Console.WriteLine(
" 文件写入完毕! " );
        }
    }

 

 隔离存储:
允许根据不同用户、组件或安全级别存放数据

 

隔离存储
class  StorageMaintain
    {
        
static   void  Main( string [] args)
        {
            IsolatedStorageFile myIsoStore 
=  IsolatedStorageFile.GetStore
                (IsolatedStorageScope.User 
|  IsolatedStorageScope.Assembly,  null null );
            IsolatedStorageFileStream isFile1 
=   new  IsolatedStorageFileStream
                (
" isFile1.bin " , FileMode.Create, myIsoStore);
            IsolatedStorageFileStream isFile2 
=   new  IsolatedStorageFileStream
                (
" isFile2.txt " , FileMode.Create, myIsoStore);
            IsolatedStorageFileStream isFile3 
=   new  IsolatedStorageFileStream
                (
" isFile3.txt " , FileMode.Create, myIsoStore);

            myIsoStore.CreateDirectory(
" ISDRoot " );
            myIsoStore.CreateDirectory(
" ISDRoot2 " );
            myIsoStore.CreateDirectory(
" ISDRoot/subISDRoot " );
            myIsoStore.CreateDirectory(
" ISDRootSecond " );

            IsolatedStorageFileStream isFile4 
=   new  IsolatedStorageFileStream
                (
" ISDRoot/rootFile1.txt " , FileMode.Create, myIsoStore);
            IsolatedStorageFileStream isFile5 
=   new  IsolatedStorageFileStream
                (
" ISDRoot/subISDRoot/rootFile2.txt " , FileMode.Create, myIsoStore);
            IsolatedStorageFileStream isFile6 
=   new  IsolatedStorageFileStream
                (
" ISDRoot/subISDRoot/rootFile3.txt " , FileMode.Create, myIsoStore);
            IsolatedStorageFileStream isFile7 
=   new  IsolatedStorageFileStream
                (
" ISDRootSecond/rootFile4.txt " , FileMode.Create, myIsoStore);

           
            isFile1.Close(); isFile2.Close(); isFile3.Close(); 
            isFile4.Close(); isFile5.Close(); isFile6.Close(); 
            isFile7.Close();
            
// 在隔离存储区读写操作
            IsolatedStorageFileStream isFile2_W  =   new  IsolatedStorageFileStream
                (
" isFile2.txt " , FileMode.Open, myIsoStore);
            StreamWriter myWriter 
=   new  StreamWriter(isFile2_W);
            myWriter.WriteLine(
" This is a test " );
            myWriter.Close();
            isFile2_W.Close();

            IsolatedStorageFileStream isFile2_R 
=   new  IsolatedStorageFileStream
               (
" isFile2.txt " , FileMode.Open, myIsoStore);
            StreamReader myReader 
=   new  StreamReader(isFile2_R);
            Console.WriteLine(myReader.ReadLine());
            isFile2_R.Close();
            myReader.Close();
            myIsoStore.Close();

            myIsoStore 
=  IsolatedStorageFile.GetStore
                (IsolatedStorageScope.User
| IsolatedStorageScope.Assembly, null , null );
            Console.WriteLine(
" 列出根目录下扩展名为bin的文件! " );

            
foreach  ( string   filename  in  myIsoStore.GetFileNames( " *.bin " ))
            {
                Console.WriteLine(filename);
            }
            Console.WriteLine(
" ==================== " );
            Console.WriteLine(
" 列出根目录下所有文件! " );
            
foreach  ( string   filename  in  myIsoStore.GetFileNames ( " * " ))
            {
                Console.WriteLine(filename);
            }
            Console.WriteLine(
" ==================== " );
            Console.WriteLine(
" 列出根目录及子目录下的所有文件! " );
            
foreach  ( string   directory  in  myIsoStore.GetDirectoryNames ( " * " ))
            {
                Console.WriteLine(directory);
                
foreach  ( string   filename  in  myIsoStore.GetFileNames (directory + " /* " ))
                {
                    Console.WriteLine(
" \t " + filename);
                }
                
foreach  ( string  sdirectory  in  myIsoStore.GetDirectoryNames (directory + " /* " ))
                {
                    Console.WriteLine(directory
+ " / " + sdirectory);
                    
foreach  ( string  filename  in  myIsoStore.GetFileNames(directory  + " / " + sdirectory + " /* " ))
                    {
                        Console.WriteLine(
" \t " + filename);
                    }
                }
            }
            myIsoStore.DeleteFile(
" isFile1.bin " );
            myIsoStore.DeleteDirectory(
" ISDRoot2 " );
            myIsoStore.Close ();            
            Console.Read();
        }
    }

 

 

 

 

 

你可能感兴趣的:(学习笔记)