温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

C#文件操作知识点(2)

发布时间:2020-04-14 20:29:01 来源:网络 阅读:366 作者:cashs2010 栏目:编程语言

C#文件操作知识点总结(2)

文件和目录操作

1.File类和Directory

Flile类的常用方法

序号

方法

说明

1

Exists(string Path)

用于检查指定文件是否存在,该方法返回一个布尔值

2

Copy(string SourceFilePath,string DestinationFilePath)

将指定路径的源文件中的内容复制到目标文件中,如果目标文件不存在,则在指定路径中新建一个文件

3

Move(string sourceFileName,string destFileName)

将指定文件移到一个新的路径

4

Delete(string path)

删除指定的文件,如果指定的文件不存在,则不引发异常

 

Directory类的常用方法

序号

方法

说明

1

Exists(string path)

用于坚持指定的文件夹在磁盘上是否存在

2

Move(string sourceDirName,string DestDirName)

用于将文件或目录及其内容移到新位置

3

Delete(string,bool)

删除指定目录,如果bool值为true,则删除子目录中的所有目录内容

 

 

例:

 

代码

 private void button1_Click(object senderEventArgs e)

        {

            openFileDialog1.Filter = "全部文件 *.*|*.*";

            openFileDialog1.FileName = "全部文件";

            openFileDialog1.ShowDialog();

            this.textBox1.Text = openFileDialog1.FileName;

        }

 

        private void button2_Click(object senderEventArgs e)

        {

            openFileDialog1.Filter = "全部文件 *.*|*.*";

            openFileDialog1.FileName = "全部文件";

            openFileDialog1.ShowDialog();

            this.textBox2.Text = openFileDialog1.FileName;

        }

 

        //复制文件

        private void button3_Click(object senderEventArgs e)

        {

            if (!File.Exists(this.textBox1.Text))

            {

                MessageBox.Show("文件不存在");

            }

            else

            {

                File.Copy(this.textBox1.Textthis.textBox2.Text);

                MessageBox.Show("拷贝成功");

            }

        }

 

        //移动文件

        private void button4_Click(object senderEventArgs e)

        {

            if (!File.Exists(this.textBox1.Text))

            {

                MessageBox.Show("文件不存在");

            }

            else

            {

                File.Move(this.textBox1.Textthis.textBox2.Text);

                MessageBox.Show("移动成功");

            }

        }

        //删除文件

        private void button5_Click(object senderEventArgs e)

        {

            if (!File.Exists(this.textBox1.Text))

            {

                MessageBox.Show("文件不存在");

            }

            else

            {

                File.Delete(this.textBox1.Text);

                MessageBox.Show("删除成功");

            }

        }

2.FileInfo类和DirectoryInfo

FileInfo类的属性和方法

属性

说明

Exists

用于检查指定文件是否存在,返回一个bool

Extension

获取表示文件扩展命名部分的字符串

Name

获取文件名

FullName

获取目录或文件的完整目录

方法

说明

CopyTo(string)

将现有文件复制到新文件,不允许覆盖现有文件

Delete()

永久删除文件

MoveTo(string)

将指定文件移到新位置(string)

 

例:

            DirectoryInfo di = new DirectoryInfo("D:\testDir");

            //返回当前目录的子目录

            DirectoryInfo[] subDir = di.GetDirectories();

            //返回当前目录的文件列表

            FileInfo[] fi = di.GetFiles();

 

序列化与反序列化

步骤:

1.引入命名空间:using System.Runtime.Serialization.Formatters.Binary;

2.SavingInfoRemind等类的头部加一个标记[Serializable],例如:

[Serializable]

Public class SavingInfo

{

//..............

}

3.编写Save()方法和Load()方法,例如:

//序列化方法

        public void Save()

        { 

            //定义文件流

            FileStream fs = new FileStream(@"files\save.bin"FileMode.Create);

            //二进制方式

            BinaryFormatter bf = new BinaryFormatter();

            //序列化存储对象

            bf.Serialize(this.listArrays);

            //关闭文件流

            fs.Close();

        }

 

        //反序列化方法

        public void Load()

        { 

            //省略判断文件是否存在

            FileStream fs = new FileStream(@"files\save.bin",FileMode.Open);

            BinaryFormatter bf=new BinaryFormatter();

            //反序列化

            this.lisArrays = (SavingInfo)bf.Deserialize(fs);

            fs.Close();

        }

注:Deserialize()方法将存储介质的数据文件流转换为object类型。

不想序列化的属性在其头部加上[NonSerialized]标记即可。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI