C#File类常用的文件操作方法(创建、移动、删除、复制等)

File类,是一个静态类,主要是来提供一些函数库用的。静态实用类,提供了很多静态的方法,支持对文件的基本操作,包括创建,拷贝,移动,删除和 打开一个文件。

File类方法的参量很多时候都是路径path。File的一些方法可以返回FileStream和StreamWriter的对象。可以 和他们配套使用。System.IO.File类和System.IO.FileInfo类

主要提供有关文件的各种操作,在使用时需要引用System.IO命名空间。

一、File类常用的操作方法

1、创建文件方法

//参数1:要创建的文件路径

File.Create(@"D:\Test\Debug1\测试.txt")

2、打开文件方法

 //参数1:要打开的文件路径,参数2:打开的文件方式

File.Open(@"D:\Test\Debug1\测试.txt",FileMode.Append)

3、追加文件方法

 //参数1:要追加的文件路径,参数2:追加的内容

File.AppendAllText(@"D:\Test\Debug1\测试.txt","哈哈");

4、复制文件方法

 //参数1:要复制的源文件路径,参数2:复制后的目标文件路径,参数3:是否覆盖相同文件名
 File.Copy(@"D:\Test\Debug1\测试.txt", @"D:\Test\Debug2\测试1.txt", true);

5、移动文件方法

 //参数1:要移动的源文件路径,参数2:移动后的目标文件路径
File.Move(@"D:\Test\Debug1\测试.txt", @"D:\Test\Debug3\测试2.txt");

6、删除文件方法

 //参数1:要删除的文件路径
 File.Delete(@"D:\Test\Debug1\测试.txt");

7、设置文件属性方法

//参数1:要设置属性的文件路径,参数2:设置的属性类型(只读、隐藏等)
File.SetAttributes(@"D:\Test\Debug1\测试.txt", FileAttributes.Hidden);

 

二、界面和源码例子:

1、界面布局

2、源码例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.IO;
using  System.Linq;
using  System.Text;
using  System.Threading.Tasks;
using  System.Windows.Forms;
 
namespace  FileHandleTest
{
     public  partial  class  Form1 : Form
     {
         public  Form1()
         {
             InitializeComponent();
         }
 
         #region Files类的文件操作方法(创建、复制、删除、移动、追加、打开、设置属性等)
       
         static  string  path =  @"D:\Test\Debug1\测试.txt" ;     //源文件路径
         static  string  path1 =  @"D:\Test\Debug2\测试1.txt" ;   //文件复制路径
         static  string  path2 =  @"D:\Test\Debug3\测试2.txt" ;   //文件移动路径
         static  string  path3 =  @"C:\测试3.txt" ;     //跨盘符存放路径(测试)
 
         /// <summary>
         /// 1、创建文件方法
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private  void  btncreate_Click( object  sender, EventArgs e)
         {
             //参数1:指定要判断的文件路径
             if  (!File.Exists(path))
             {
                 //参数1:要创建的文件路径,包含文件名称、后缀等
                 FileStream fs = File.Create(path);
                 fs.Close();
                 MessageBox.Show( "文件创建成功!" );
             }
             else  {
                 MessageBox.Show( "文件已经存在!" );
             }
         }
 
         /// <summary>
         ///2、 打开文件的方法
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private  void  btnopen_Click( object  sender, EventArgs e)
         {
             if  (File.Exists(path))
             {
                 //参数1:要打开的文件路径,参数2:打开的文件方式
                 FileStream fs = File.Open(path, FileMode.Append);
                 //字节数组
                 byte [] bytes = { ( byte ) 'h' , ( byte ) 'e' , ( byte ) 'l' , ( byte ) 'l' , ( byte ) 'o'  };
                 //通过字符流写入文件
                 fs.Write(bytes, 0, bytes.Length);
                 fs.Close();
                 MessageBox.Show( "打开并追加Hello成功!" );
             }
             else
             {
                 MessageBox.Show( "文件不存在!" );
             }
         }
 
         /// <summary>
         /// 3、追加文件内容方法
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private  void  btnappend_Click( object  sender, EventArgs e)
         {
             string  appendtext =  this .txtContent.Text;
             if  (File.Exists(path))
             {
                 //参数1:要追加的文件路径,参数2:追加的内容
                 File.AppendAllText(path, appendtext);
                 MessageBox.Show( "文件追加内容成功!" );
             }
             else
             {
                 MessageBox.Show( "文件不存在!" );
             }
         }
 
        
         /// <summary>
         /// 4、复制文件方法(只能在同个盘符进行操作)
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private  void  btncopy_Click( object  sender, EventArgs e)
         {
             if  (File.Exists(path))
             {
                 //参数1:要复制的源文件路径,参数2:复制后的目标文件路径,参数3:是否覆盖相同文件名
                 File.Copy(path, path1,  true );
                 MessageBox.Show( "复制文件成功!" );
             }
             else  {
                 MessageBox.Show( "文件不存在!" );
             }
         }
 
         /// <summary>
         /// 5、移动文件方法(只能在同个盘符进行操作)
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private  void  btnmove_Click( object  sender, EventArgs e)
         {
             if  (File.Exists(path))
             {
                 //参数1:要移动的源文件路径,参数2:移动后的目标文件路径
                 File.Move(path, path2);
                 MessageBox.Show( "移动文件成功!" );
             }
             else  {
                 MessageBox.Show( "文件不存在!" );
             }
         }
 
         /// <summary>
         /// 6、删除文件方法
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private  void  btndelete_Click( object  sender, EventArgs e)
         {
             if  (File.Exists(path))
             {
                 //参数1:要删除的文件路径
                 File.Delete(path);
                 MessageBox.Show( "文件删除成功!" );
             }
             else
             {
                 MessageBox.Show( "文件不存在!" );
             }
         }
 
         /// <summary>
         ////7、设置文件属性方法
         /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private  void  btnset_Click( object  sender, EventArgs e)
         {
             if  (File.Exists(path))
             {
                 //参数1:要设置属性的文件路径,参数2:设置的属性类型(只读、隐藏等)
                 File.SetAttributes(path, FileAttributes.Hidden);
                 MessageBox.Show( "设置文件属性为隐藏成功!" );
             }
             else
             {
                 MessageBox.Show( "文件不存在!" );
             }
         }
 
         #endregion
     }
        
}

猜你喜欢

转载自blog.csdn.net/u010753302/article/details/80909208