zoukankan      html  css  js  c++  java
  • C#文件操作

    文件操作

    System.IO空间

    File类:涉及到文件

    其中的重要的静态方法:

    Copy复制文件

           private void button3_Click(object sender, EventArgs e)
            {
                //写路径时,如果不想写转义符,就是用@
                string desPath = @"E:011.txt";
                bool exi = File.Exists(desPath);
                if(exi)
                {
                    //MessageBox 返回DialogResult类型
                    if(MessageBox.Show("文件已存在,是否要覆盖?", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                        == DialogResult.Yes)
                    {
                        File.Copy(@"E:1.txt", desPath, true);
                        MessageBox.Show("文件已被覆盖");
                    }                
                }
                else
                {
                    File.Copy(@"E:1.txt", desPath, true);
                    MessageBox.Show("文件复制成功");
                }
               
            }
    

    复制指定文件

            private void button4_Click(object sender, EventArgs e)
            {
                string sourceFile = null;
                string destFile = null;
    
                //可以打开的文件过滤器
                //两个一组,以竖线分割,前面是可以操作的文件,后面是文件类型
                //支持txt文件和docx文件
                openFileDialog1.Filter = "TXT文件|*.txt|WORD文件|*.docx";
                saveFileDialog1.Filter = "TXT文件|*.txt|WORD文件|*.docx";
    
                //显示打开文件对话框,返回DialogResult类型,如果为OK,则用户点击的是打开,否则为取消
                //openFileDialog1.ShowDialog();
                if(openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    //用户点击了打开按钮 
                    sourceFile = openFileDialog1.FileName;
    
                    //用户点击了保存按钮
                    if(saveFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        destFile = saveFileDialog1.FileName;
                        File.Copy(sourceFile, destFile, true);
                    }
                }
                
            }
    

    复制整个文件夹内容到指定目录

            private void button5_Click(object sender, EventArgs e)
            {
                string sDir, dDir;
                FolderBrowserDialog Folder = new FolderBrowserDialog();
                Folder.Description = "选择要复制的文件夹";
                if(Folder.ShowDialog() == DialogResult.OK)
                {
                    sDir = Folder.SelectedPath;
    
                    Folder.Description = "请选择要复制到的文件夹";
                    if(Folder.ShowDialog() == DialogResult.OK)
                    {
                        dDir = Folder.SelectedPath;
    
                        //得到源文件夹中所有文件
                        string[] files = Directory.GetFiles(sDir);
                        foreach(string filepath in files)
                        {
                            //根据路径名获得文件名
                            string dFileName = filepath.Substring(filepath.LastIndexOf("\") + 1);
                            File.Copy(filepath, dDir+"\"+dFileName, true); 
                        }
                          
                    }
                }
            }
    

      

  • 相关阅读:
    Visual Studio 2019 开发 Python 及
    Delphi 的环境配置
    Delphi 的 TMS 控件安装方法
    网络爬虫-书籍与框架
    建筑行业(项目管理) BI 数据可视化分析案例
    DeepNude(一健脱衣)的算法以及通用图像生成的理论与实践研究
    matlab生成HEX文件-任意信号 大于64K长度
    SDRAM总结
    function [ binary,decimal ] = num2binary16( number )
    任意时钟分频
  • 原文地址:https://www.cnblogs.com/my-cat/p/7273798.html
Copyright © 2011-2022 走看看