目录操作
string[] drives = directory.getlogicaldrives(); //本地驱动器的名,如:c:/等
string path = directory.getcurrentdirectory();   //获取应用程序的当前工作目录
path.getfilename(@"c:/dir/file.txt");            //获取子目录的名字,result的结果是file.txt
directory.getfiles(路径及文件名)                 //获取指定目录中的文件名(文件列表)
directoryinfo di = new directoryinfo(@"f:/mydir"); //构造函数创建目录
directoryinfo di=directory.createdirectory(@"f:/bbs"); //创建对象并创建目录
if (di.exists == false) //检查是否存在此目录
di.create(); //创建目录
directoryinfo dis = di.createsubdirectory("subdir"); //以相对路径创建子目录
dis.delete(true); //删除刚创建的子目录
di.delete(true); //删除创建目录
文件操作
directory.delete(@"f:/bbs2", true); //删除目录及其子目录和内容(如为假不能删除有内容的目录包括子目录)
directory.getdirectories 方法 //获取指定目录中子目录的名称
string[] dirs = directory.getdirectories(@"f:/", "b*"); 
console.writeline("此目录中以b开头的子目录共{0}个!", dirs.length);
foreach (string dir in dirs) { console.writeline(dir); }
directory.getfilesystementries //获取指定目录中的目录及文件名
directory.getlogicaldrives //检索此计算机上格式为“<驱动器号>:/”的逻辑驱动器的名称,【语法同上】
directory.getparent //用于检索父目录的路径。
directoryinfo a = directory.getparent(path);
console.writeline(a.fullname);directory.move //移动目录及其在内的所有文件
directory.move(@"f:/bbs/1", @"f:/bbs/2"); //将文件夹1内的文件剪到文件夹2内 文件夹2是刚创建的
stream // 对字节的读写操作(包含对异步操作的支持) reading writing seeking
binaryreader和binarywriter // 从字符串或原始数据到各种流之间的读写操作
filestream类通过seek()方法进行对文件的随机访问,默认为同步
textreader和textwriter //用于gb2312字符的输入和输出
stringreader和stringwriter //在字符串中读写字符
streamreader和streamwriter //在流中读写字符
bufferedstream 为诸如网络流的其它流添加缓冲的一种流类型.
memorystream 无缓冲的流
networkstream 互联网络上的流
//编码转换
encoding e1 = encoding.default;               //取得本页默认代码 
byte[] bytes = e1.getbytes("中国人民解放军"); //转为二进制
string str = encoding.getencoding("utf-8").getstring(bytes); //转回utf-8编码
//文本文件操作:创建/读取/拷贝/删除
using system;
using system.io;
class test 
{
   string path = @"f:/t.txt";
   public static void main() 
    {       
      //创建并写入(将覆盖已有文件)
      if (!file.exists(path))
       {          
         using (streamwriter sw = file.createtext(path))
          {
             sw.writeline("hello");
          } 
       }
      //读取文件
      using (streamreader sr = file.opentext(path)) 
       {
         string s = "";
        while ((s = sr.readline()) != null) 
         {
            console.writeline(s);
         }
      }
     //删除/拷贝
     try 
      {
         file.delete(path);
         file.copy(path, @"f:/tt.txt");
      } 
     catch (exception e) 
      {
         console.writeline("the process failed: {0}", e.tostring());
      }
    }
}
//流文件操作
private const string name = "test.data";
public static void main(string[] args) 
{
     //打开文件()   ,或通过file创建立如:fs = file.create(path, 1024)
     filestream fs = new filestream(name, filemode.createnew);
     //转换为字节 写入数据(可写入中文)
     byte[] info = new utf8encoding(true).getbytes("this is some text in the file.");
    //字节数组,字节偏移量,最多写入的字节数
     fs.write(info, 0, info.length);
     w.close();
     fs.close();
    //打开文件
     fs = new filestream(name, filemode.open, fileaccess.read);
    //读取
     binaryreader r = new binaryreader(fs);
     for (int i = 0; i < 11; i++) 
     {
         console.writeline(r.readint32());
     }
     w.close();
     fs.close();
}
新闻热点
疑难解答