zoukankan      html  css  js  c++  java
  • C#备份收藏夹代码

    //添加系统引用

    using System.IO;

    private void 备份收藏夹ToolStripMenuItem_Click(object sender, EventArgs e)
            {

                string BackupFavoritePath;
                string SystemFavoritePath = GetSystemFavoritePath();
                FolderBrowserDialog myfolderBrowserDialog = new FolderBrowserDialog();
                myfolderBrowserDialog.RootFolder = Environment.SpecialFolder.Desktop;
                if (myfolderBrowserDialog.ShowDialog() == DialogResult.OK)
                {
                    if (myfolderBrowserDialog.SelectedPath != "")
                    {
                        BackupFavoritePath = myfolderBrowserDialog.SelectedPath;
                        CopyDir(SystemFavoritePath, BackupFavoritePath);

                     }
                    else
                    {
                        return;
                    }
                }
            }

            /// <summary>
            /// 从注册表获取收藏夹路径
            /// </summary>
            /// <returns></returns>
            private static string GetSystemFavoritePath()
            {
                string strSystemFavoritePath = "";
                RegistryKey hklm = Registry.CurrentUser;
                RegistryKey software = hklm.OpenSubKey(@"Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders", true);
                if (software == null)
                {
                    strSystemFavoritePath = "读取失败";
                }
                else
                {
                    strSystemFavoritePath = software.GetValue("Favorites").ToString();
                }
                return strSystemFavoritePath;
            }

            /// <summary>
            /// 循环递归拷贝目录文件函数
            /// </summary>
            /// <param name="sourcePath"></param>
            /// <param name="targetPath"></param>
            private void CopyDir(string sourcePath, string targetPath)
            {
                try
                {
                    // 检查目标目录是否以目录分割字符结束如果不是则添加之
                    if (targetPath[targetPath.Length - 1] != System.IO.Path.DirectorySeparatorChar)
                    {
                        targetPath += System.IO.Path.DirectorySeparatorChar;
                    }

                    // 判断目标目录是否存在如果不存在则新建之
                    if (!System.IO.Directory.Exists(targetPath))
                    {
                        System.IO.Directory.CreateDirectory(targetPath);
                    }

                    // 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
                    // 如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
                    // string[] fileList = Directory.GetFiles(sourcePath);
                    string[] fileList = System.IO.Directory.GetFileSystemEntries(sourcePath);

                    // 遍历所有的文件和目录
                    foreach (string file in fileList)
                    {
                        // 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
                        if (System.IO.Directory.Exists(file))
                        {
                            CopyDir(file, targetPath + System.IO.Path.GetFileName(file));
                        }

                        // 否则直接Copy文件
                        else
                        {
                            System.IO.File.Copy(file, targetPath + System.IO.Path.GetFileName(file), true);
                        }
                    }
                }

                catch (Exception e)
                {
                    throw;
                }
            }

  • 相关阅读:
    UVa 291 The House Of Santa Claus——回溯dfs
    (优先)队列简单总结
    POJ 2255 Tree Recovery——二叉树的前序遍历、后序遍历、中序遍历规则(递归)
    博弈论入门 Bash 、Nim 、Wythoff's Game结论及c++代码实现
    UVa 167(八皇后)、POJ2258 The Settlers of Catan——记两个简单回溯搜索
    欧几里得(辗转相除gcd)、扩欧(exgcd)、中国剩余定理(crt)、扩展中国剩余定理(excrt)简要介绍
    51nod 1135 原根 (数论)
    「学习笔记」扩展KMP (简)
    「解题报告」[luoguP6594]换寝室 (二分答案 树形DP)
    「解题报告」[luoguP6585]中子衰变 (交互题 分类讨论)
  • 原文地址:https://www.cnblogs.com/xqf222/p/3306810.html
Copyright © 2011-2022 走看看