C# Windows窗体——文件操作:File和FileInfo

File类支持对文件的基本操作,包括对文件创建、复制、删除、移动和打开灯静态方法,并协助创建FileStream对象。其为静态类,它对应的这个文件系统进行操作,方法均为静态方法。

属性:DerictoryName绝对路径名、CreationTime创建时间、LastAccessTime最近访问时间、LastWriteTime最近修改时间以及Length文件长度。

方法:AppendAllText将制指定的字符串追加到文件中,如果文件不存在,则创建该文件。AppendText将文本追加到现有文件中。Create在指定路径中创建文件。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "源文件名";         //获取或设置文件对话框标题
            if (openFileDialog1.ShowDialog() == DialogResult.OK)    //指定标识符以指示对话框的返回值
            {
                textBox1.Text = openFileDialog1.FileName;           //获取或设置一个包含在文件对话框中选定的文件名单字符串
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)       //显示一个对话框,提示用户选择一个文件夹
            {
                textBox2.Text = folderBrowserDialog1.SelectedPath;          //获取或设置用户选定的路径
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            string filename = textBox2.Text + "\\" + textBox3.Text;         //设置文件路径
            FileInfo myfile = new FileInfo(@textBox1.Text);
            myfile.Create();    //创建文件
            //File.Create(filename);      //在指定路径中创建或覆盖文件
        }
        private void button4_Click(object sender, EventArgs e)
        {
            FileInfo filename = new FileInfo(@textBox3.Text);
            if(filename.Exists)
                filename.CopyTo(textBox2.Text+"\\"+textBox3.Text);
            /*if (File.Exists(textBox1.Text))
            {
               
                //确定指定的文件是否存在
                File.Copy(@textBox1.Text, textBox2.Text = "\\" + textBox3.Text, true);      //将现有文件复制到指定的文件夹。允许覆盖同名文件(bool overwrite)
            }*/
            else
                MessageBox.Show("源文件不存在,无法复制");
        }
        private void button5_Click(object sender, EventArgs e)
        {
            FileInfo filename=new FileInfo(@textBox1.Text);
            if (filename.Exists)
            {
                filename.MoveTo(textBox2.Text + "\\" + textBox3.Text);
            }
            /*if (File.Exists(textBox1.Text))
            {
                File.Move(@textBox1.Text, textBox2.Text + "\\" + textBox3.Text);
            }*/
            else
                MessageBox.Show("源文件不存在,无法移动");
        }
        private void button6_Click(object sender, EventArgs e)
        {
            FileInfo filename = new FileInfo(@textBox3.Text);
            if (filename.Exists) filename.Delete();
            /*if (File.Exists(textBox1.Text))
                File.Delete(textBox1.Text);*/
            else
                MessageBox.Show("源文件不存在,无法删除");
        }
        private void button7_Click(object sender, EventArgs e)
        {
            FileInfo filename=new FileInfo(@textBox1.Text);
            if (filename.Exists)
                filename.MoveTo(textBox3.Text);
            /*if (File.Exists(textBox1.Text))
                File.Move(@textBox1.Text, textBox3.Text);*/
            else
                MessageBox.Show("源文件不存在,无法改名");
        }
    }
}


FileInfo类是一个密封类,它可以用来创建、复制、删除、移动和打开文件的实例方法。它的操作可能是File类中对应的静态方法。

如果应用程序在文件上执行几种操作,则使用FileInfo类更好一些,因为创建对象时,已经引用了正确的文件,而静态类每次都要寻找文件。如果进行单一的方法调用,则建议用File类,不必实例化对象。


摘自:《C#程序设计基础》张世明 电子工业出版社


  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值