C# winForm——文件另存为功能(满足任意格式文件)

目录

一、前言

二、具体实现

2.1 预期效果

2.2 核心设计与代码

2.2.1 主窗体布局

2.2.2 核心代码

三、小结


一、前言

今天项目中遇到一个需求,要将winForm程序中bin/Debug 相对路径下的文件保存到用户自定义选择的位置,任意格式文件,就是PC程序的“另存为Save as ...” 功能,因为所有的格式的文件都要支持,所以只能在代码层面实现一个“复制-粘贴”的功能,具体是怎样实现的呢,来看看吧。

二、具体实现

2.1 预期效果

2.2 核心设计与代码

2.2.1 主窗体布局

2.2.2 核心代码

btn_OpenFile打开文件按钮,使用openFileDialog,选中需要另存为的原文件

      private void btn_OpenFile_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                // 显示被打开文件的路径和名称
                this.label1.Text = "原来文件路径是: "+ openFileDialog1.FileName;
                originPath = openFileDialog1.FileName;
                // 允许 "文件另存为..."按钮
                btn_SaveAs.Enabled = true;
                btn_OpenFile.Enabled = false;
            }
        }

btn_SaveAs另存为按钮,源文件路径添加到ArrayList中, 使用folderBrowserDialog指定targetPath,调用save函数

   private void btn_SaveAs_Click(object sender, EventArgs e)
        {

            string fileName = Path.GetFileName(originPath);
             try
             {
                 al.Clear();
                 al.Add(originPath);
             }
             catch (Exception ex)
             {
                 MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
             }
             try
             {
                 string targetPath = null;
                 if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                 {
                     targetPath = folderBrowserDialog1.SelectedPath.ToString();
                     this.label1.Text = "副本文件路径是: "+ folderBrowserDialog1.SelectedPath + "\\" + fileName;
                 }
                 targetPath = targetPath.TrimEnd('\\') + "\\";
                 save(targetPath);
                 MessageBox.Show("保存成功");
                 btn_SaveAs.Enabled = false;
                 btn_OpenFile.Enabled = true;
             }
             catch (Exception)
             {
                 MessageBox.Show("保存失败");
                 btn_SaveAs.Enabled = false;
                 btn_OpenFile.Enabled = true;
             }

        }

save()函数,核心,将ArrayList对象al指定的文件“复制-粘贴”到targetPath

 private void save(string targetPath)
        {
                try
                {
                    for (int i = 0; i < al.Count; i++)
                    {
                        string name1 = al[i].ToString().Substring(al[i].ToString().LastIndexOf("\\") + 1, 
                            al[i].ToString().Length - al[i].ToString().LastIndexOf("\\") - 1);
             
                        string paths = targetPath  + name1;
                        if (File.Exists(al[i].ToString()))
                        {
                            if (al[i].ToString() != paths)
                            {
                                File.Copy(al[i].ToString(), paths, true);
                            }
                        }
                        if (Directory.Exists(al[i].ToString()))
                        {
                            bc.Files_Copy(paths, al[i].ToString());
                        }
                    }
                }
                catch { }
           
        }

Files_Copy()函数,核心函数,实现文件与文件夹的“复制-粘贴”

  public void Files_Copy(string Ddir, string Sdir)
        {
            DirectoryInfo dir = new DirectoryInfo(Sdir);
            string SbuDir = Ddir;
            try
            {
                if (!dir.Exists)//判断所指的文件或文件夹是否存在
                {
                    return;
                }
                DirectoryInfo dirD = dir as DirectoryInfo;//如果给定参数不是文件夹则退出
                string UpDir = UpAndDown_Dir(Ddir);
                if (dirD == null)//判断文件夹是否为空
                {
                    Directory.CreateDirectory(UpDir + "\\" + dirD.Name);//如果为空,创建文件夹并退出
                    return;
                }
                else
                {
                    Directory.CreateDirectory(UpDir + "\\" + dirD.Name);
                }
                SbuDir = UpDir + "\\" + dirD.Name + "\\";
                FileSystemInfo[] files = dirD.GetFileSystemInfos();//获取文件夹中所有文件和文件夹
                //对单个FileSystemInfo进行判断,如果是文件夹则进行递归操作
                foreach (FileSystemInfo FSys in files)
                {
                    FileInfo file = FSys as FileInfo;
                    if (file != null)//如果是文件的话,进行文件的复制操作
                    {
                        FileInfo SFInfo = new FileInfo(file.DirectoryName + "\\" + file.Name);//获取文件所在的原始路径
                        SFInfo.CopyTo(SbuDir + "\\" + file.Name, true);//将文件复制到指定的路径中
                    }
                    else
                    {
                        string pp = FSys.Name;//获取当前搜索到的文件夹名称
                        Files_Copy(SbuDir + FSys.ToString(), Sdir + "\\" + FSys.ToString());//如果是文件,则进行递归调用
                    }
                }
            }
            catch
            {
                MessageBox.Show("文件制复失败。");
                return;
            }
        }

三、小结

在代码层面使用“复制-粘贴”文件的办法实现了本地磁盘上的文件的另存为功能,这种方法适合任意格式的文件哦!!!

天天打码,天天进步!

C# winForm工程文件:https://download.csdn.net/download/qq_36963950/12249460

发布了190 篇原创文章 · 获赞 71 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/qq_36963950/article/details/101080371