.Net文件操作

文件操作

File类,FileInfo类.using System.IO命名空间
(一)创建

方法一:

1         private string path = @"F:\Text\aaa.txt";
2         private void CreateFile_Click(object sender, EventArgs e)
3         {
4             FileStream fs = File.Create(path);
5             fs.Close();//返回FileStream必须释放流
6         }

 

方法二:

1         private string path = @"F:\Text\aaa.txt";
2         private void CreateFile_Click(object sender, EventArgs e)
3         {            
4             FileInfo CreateFile = new FileInfo(path);
5             FileStream stream = CreateFile.Create();
6             stream.Close();//返回FileStream类型必须释放流
7         }

 

(二)删除

方法一:

1         private string path = @"F:\Text\aaa.txt";
2         private void DeleteFile_Click(object sender, EventArgs e)
3         {
4             File.Delete(path);
5         }

 

方法二:

1         private string path = @"F:\Text\aaa.txt";
2         private void DeleteFile_Click(object sender, EventArgs e)
3         {           
4             FileInfo DeleteFile = new FileInfo(path);
5             DeleteFile.Delete();
6         }

 

(三)是否存在

方法一:

 1         private string path = @"F:\Text\aaa.txt";
 2         private void ExistFile_Click(object sender, EventArgs e)
 3         {
 4              bool ex = File.Exists(path);            
 5              if (ex)
 6              {
 7                  MessageBox.Show("存在");
 8              }
 9              else
10              {
11                  MessageBox.Show("不存在");
12              }
13         }

 

方法二:

 1         private string path = @"F:\Text\aaa.txt";
 2         private void ExistFile_Click(object sender, EventArgs e)
 3         {            
 4              FileInfo ExistFile = new FileInfo(path);
 5              bool ex = ExistFile.Exists;
 6              if (ex)
 7              {
 8                  MessageBox.Show("存在");
 9              }
10              else
11              {
12                  MessageBox.Show("不存在");
13              }
14         }

 

(四)复制---不能跨盘符

方法一:

1         private string path = @"F:\Text\aaa.txt";
2         private void CopyFile_Click(object sender, EventArgs e)
3         {
4             File.Copy(path, @"F:\Text\bbb.txt");//复制的同时里面的内容也和复制
5         }

 

方法二:

1  private void CopyFile_Click(object sender, EventArgs e)
2         {          
3             FileInfo CopyFile = new FileInfo(path);
4             CopyFile.CopyTo(@"F:\Text\bbb.txt"); //复制的同时里面的内容也和复制
5         }

 

(五)移动(改名)---不能跨盘符

 

注:Move()或MoveTo(): 1.路径不同名字相同(相当于--剪切),

 

          2.路径不同名字不同(相当于--剪切+重命名),

 

          3.路径相同名字不同(相当于--重命名).

 

 且移动是同时将文件夹内的所有对象,移动.

 

方法一:

1         private string path = @"F:\Text\aaa.txt";
2         private void MoveFile_Click(object sender, EventArgs e)
3         {
4             File.Move(path, @"F:\Test\bbb.txt");            
5         }

 

方法二:

1         private string path = @"F:\Text\aaa.txt";
2         private void MoveFile_Click(object sender, EventArgs e)
3         {           
4             FileInfo MoveFIle = new FileInfo(path);
5             MoveFIle.MoveTo(@"F:\Test\ccc.txt");
6         }

 

(六)获得文件名

注:只有FileInfo()类一种方法

  1.只获取文件名(貌似没什么用--下面show()的内容就是aaa.txt)

     

1         private string path = @"F:\Text\aaa.txt";
2         private void FileName_Click(object sender, EventArgs e)
3         {
4             FileInfo FileName = new FileInfo(path);
5             string name = FileName.Name;
6             MessageBox.Show(name);
7         }

 

  2.或取文件全名(包括路径)

1         private string path = @"F:\Text\aaa.txt";
2         private void FileName_Click(object sender, EventArgs e)
3         {            
4             FileInfo FileName = new FileInfo(path);
5             string name = FileName.FullName;
6             MessageBox.Show(name);
7         }

 

(七)获得扩展名

注:只有FileInfo()类一种方法

1         private string path = @"F:\Text\aaa.txt";
2         private void ExtensionName_Click(object sender, EventArgs e)
3         {
4             FileInfo ExtensionName = new FileInfo(path);
5             string exname = ExtensionName.Extension;
6             MessageBox.Show(exname);
7         }

 

 

(八)获取辅助属性

注:只有File()类一种方法

1         private string path = @"F:\Text\aaa.txt";
2         private void GetAttribute_Click(object sender, EventArgs e)
3         {
4             FileAttributes Attributes = File.GetAttributes(path);
5             DateTime CreateTime = File.GetCreationTime(path);
6             DateTime LastAccessTime = File.GetLastAccessTime(path);
7             DateTime GetLastWriteTime =  File.GetLastWriteTime(path);
8             MessageBox.Show(Attributes.ToString()+"\n"+CreateTime.ToString()+"\n"+LastAccessTime.ToString()+"\n"+GetLastWriteTime.ToString());
9         }

 

(九)修改辅助属性

注:只有File()类一种方法

        private string path = @"F:\Text\aaa.txt";
        private void button1_Click(object sender, EventArgs e)
        {
            FileAttributes Attributes = new FileAttributes();
            Attributes = FileAttributes.Hidden;
            File.SetAttributes(path, Attributes);
            File.SetCreationTime(path, DateTime.Now);
            File.SetLastAccessTime(path, DateTime.Now);
            File.SetLastWriteTime(path, DateTime.Now);
        }

 

(十)打开保存

  文件读写

注:所有打开new一个新类之后先写 类名.close()

  FileStream类:比较通用。

    打开

 1         private string path = @"F:\Text\aaa.txt";
 2         private void Open_Stream_Click(object sender, EventArgs e)
 3         {
 4             FileStream stream = new FileStream(path, FileMode.Open);//读出的数据是以二进制代码形式存储
 5             byte[] nr = new byte[stream.Length];
 6             stream.Read(nr,0,nr.Length);
 7             stream.Close();
 8             //把byte[]的内容变成字符串放在文本框中。
 9             string s = System.Text.Encoding.Default.GetString(nr);
10             textBox1.Text = s;
11         }

    保存

 1         private string path = @"F:\Text\aaa.txt";
 2         private void Save_Stream_Click(object sender, EventArgs e)
 3         {
 4             //把文本框的字符串变成二进制数组
 5             byte[] nr = System.Text.Encoding.Default.GetBytes(textBox1.Text);
 6             //送到文件中去
 7             FileStream stream = new FileStream(path, FileMode.OpenOrCreate);
 8             stream.Write(nr, 0, nr.Length);
 9             stream.Close();
10         }

 

  StreamReader类:

    打开

1         private string path = @"F:\Text\aaa.txt";
2         private void Open_Reader_Click(object sender, EventArgs e)
3         {
4             StreamReader reader = new StreamReader(path, Encoding.Default);
5             string s = reader.ReadToEnd();
6             reader.Close();
7             textBox1.Text = "";
8             textBox1.Text = s;
9         }

 1         private string path = @"F:\Text\aaa.txt";
 2         private void Open_Writer_Click(object sender, EventArgs e)
 3         {
 4             FileStream stream = new FileStream(path, FileMode.Open);
 5             StreamReader reader = new StreamReader(stream, Encoding.Default);
 6             string s = reader.ReadToEnd();
 7             reader.Close();
 8             stream.Close();
 9             textBox1.Text = s;
10         }

 

 

  StreamWriter类:

 

1         private string path = @"F:\Text\aaa.txt";
2         private void Save_Writer_Click(object sender, EventArgs e)
3         {
4             StreamWriter writer = new StreamWriter(path, false, Encoding.Default);
5             writer.WriteLine(textBox1.Text);
6             writer.Close();
7         }

 1        private string path = @"F:\Text\aaa.txt";
 2        private void Save_Writer_Click(object sender, EventArgs e)
 3        {
 4            FileStream stream = new FileStream(path, FileMode.OpenOrCreate);
 5            StreamWriter writer = new StreamWriter(stream);
 6            writer.WriteLine(textBox1.Text);
 7            writer.Close();
 8            stream.Close();
 9        }
10     

 

 

posted @ 2015-07-24 09:12  北极熊爱吃鱼  阅读(185)  评论(0编辑  收藏  举报