C#——文件操作

1.打开文件

  //打开XML文件,并识别一些其中的内容
          using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Filter = "配置文件|*.xml";
                ofd.DefaultExt = "*.xml";
                ofd.InitialDirectory = Application.StartupPath + "\\Cfg";
                if (ofd.ShowDialog() == DialogResult.OK)  //如果点击的是打开文件
                {
                    string fileName = ofd.FileName;  //获取全路径文件名

                    //MessageBox.Show("OK");
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(fileName);
                    XmlNode xml_CaseInfo = xmlDoc.SelectSingleNode("CaseInfo");
                    if (null == xml_CaseInfo)
                        return;

                    XmlNode xml_MaxPower = xml_CaseInfo.SelectSingleNode("NormalConfiguration");

                    XmlNodeList oList = xml_MaxPower.ChildNodes;
                    //XmlNode oCurrentNode;
                    stroutPutLevel = oList[0].Attributes.GetNamedItem("Value").Value;
                    strInputLevel = oList[1].Attributes.GetNamedItem("Value").Value;
                    strPMax = oList[2].Attributes.GetNamedItem("Value").Value;
                }
            }

2.文件另存为

方式一:移动文件

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "配置文件(*.xml)|*.xml";
            sfd.DefaultExt = "*.xml";
            sfd.InitialDirectory = Application.StartupPath + "\\Cfg\\Cellular";

            if (sfd.ShowDialog() == DialogResult.OK)
            {
                string localFilePath = Application.StartupPath + "\\Cfg\\Cellular\\Case.xml";
                string fileName = sfd.FileName;
                
                if (System.IO.File.Exists(fileName))
                {
                    if (MessageBox.Show("文件已经存在,确认覆盖吗?", "参数保存") == DialogResult.OK)
                    {
                        System.IO.File.Copy(localFilePath, fileName, true);
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    //System.IO.Directory.Move(localFilePath, fileName);//剪切文件或文件夹
                    System.IO.File.Copy(localFilePath, fileName, true);//只能移动文件
                    MessageBox.Show("另存成功", "另存");
                }
                return;
            }

方式二:递归移动文件夹
        public void CopyUpdateFile(string srcPath, string aimPath)
        {
            if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
                aimPath += Path.DirectorySeparatorChar;
            if (!Directory.Exists(aimPath))
                Directory.CreateDirectory(aimPath);
            if (!Directory.Exists(srcPath))
                return;
            string[] fileList = Directory.GetFileSystemEntries(srcPath);

            foreach (string file in fileList)
            {
                int k = file.LastIndexOf("\\");
                string filename = file.Substring(k + 1);
                if (filename.ToLower() != Path.GetFileName(Application.ExecutablePath).ToLower())
                {
                    if (Directory.Exists(file))
                        CopyUpdateFile(file, aimPath + Path.GetFileName(file));
                    else
                    {
                        try
                        {
                            File.Copy(file, aimPath + Path.GetFileName(file), true);
                            //File.Delete(file);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("复制文件失败:" + file + e.Message);
                            continue;
                        }
                    }
                }
            }
        }
方法三:批量移动文件到指定的文件夹
            
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件备份路径";
            string foldPath = "";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                foldPath = dialog.SelectedPath + @"\";//获取选择的路径
            }
            //通过某种方法获取要转移的文件的集合
            List<string> TestList = GetSelectFile();
            foreach (string File in FileList)
            {
                //使用方法二进行转移
            }

3.删除文件

 //System.IO.Directory.Delete(path);//删除文件
//System.IO.Directory.Delete(path, true); 删除文件夹


 //递归删除文件夹及文件夹内所有文件
        private void DeleteDir(string path)
        {
            if (path.Trim() == "" || !Directory.Exists(path))
                return;
            DirectoryInfo dirInfo = new DirectoryInfo(path);

            FileInfo[] fileInfos = dirInfo.GetFiles();
            if (fileInfos != null && fileInfos.Length > 0)
            {
                foreach (FileInfo fileInfo in fileInfos)
                {
                    //DateTime.Compare( fileInfo.LastWriteTime,DateTime.Now);
                    File.Delete(fileInfo.FullName); //删除文件
                }
            }

            DirectoryInfo[] dirInfos = dirInfo.GetDirectories();
            if (dirInfos != null && dirInfos.Length > 0)
            {
                foreach (DirectoryInfo childDirInfo in dirInfos)
                {
                    this.DeleteDir(childDirInfo.FullName); //递归
                }
            }
            Directory.Delete(dirInfo.FullName, true); //删除目录
        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱学习的man

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值