C# FileInfo.CopyTo的代码示例

通过代码示例来学习C# FileInfo.CopyTo方法

通过代码示例来学习编程是非常高效的。
1. 代码示例提供了一个具体而直观的学习环境,使初学者能够立即看到编程概念和语法的实际应用。
2. 通过分析和模仿现有的代码实例,初学者可以更好地理解编程逻辑和算法的工作原理。
3. 代码实例往往涵盖了多种编程技巧和最佳实践,通过学习和模仿这些实例,学习者可以逐步掌握如何编写高效、可读性强和可维护的代码。这对于初学者来说,是一种快速提升编程水平的有效途径。


FileInfo.CopyTo是C#的System.IO命名空间下中的一个方法, 小编为大家找了一些网络大拿们常见的代码示例,源码中的FileInfo.CopyTo() 已经帮大家高亮显示了,大家可以重点学习FileInfo.CopyTo() 方法的写法,从而快速掌握该方法的应用。

FileInfo.CopyTo的代码示例1 - CombineFiles()

    using System.IO;

        public static void CombineFiles(string srcFilePathPattern, string destFilePath, int bufferSize = 4096, string EOFDelimiter = null)
        {
            ChoGuard.ArgumentNotNullOrEmpty(srcFilePathPattern, "srcFilePathPattern");
            ChoGuard.ArgumentNotNullOrEmpty(destFilePath, "destFilePath");

            if (EOFDelimiter == null)
                EOFDelimiter = Environment.NewLine;

            using (var outputStream = File.OpenWrite(destFilePath))
            {
                foreach (string fp in ChoDirectory.GetFilesBeginWith(srcFilePathPattern).OrderBy(f => f, StringComparer.CurrentCultureIgnoreCase))
                {
                    if (new FileInfo(fp).Length == 0) continue;
                    using (var inputStream = File.OpenRead(fp))
                    {
                        if (outputStream.Position != 0)
                            outputStream.Write(EOFDelimiter);

                        // Buffer size can be passed as the second argument.
                        inputStream.CopyTo(outputStream, bufferSize);
                    }
                }
            }
        }
    

开发者ID:Cinchoo,项目名称:ChoETL,代码行数:26,代码来源:ChoFile.cs

在CombineFiles()方法中,FileInfo的代码示例类中的CopyTo的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞

FileInfo.CopyTo的代码示例2 - SetupProfileForTestRun()

    using System.IO;
        public static bool SetupProfileForTestRun(State state)
        {
            try
            {
                // Backup any saved state file because some add operations update it
                var fiSavedState = GetDefaultSavedStateInfo();

                state.BackupFileInfo = null;
                state.WasNoState = !fiSavedState.Exists;
                if (fiSavedState.Exists)
                {
                    state.BackupFileInfo = new FileInfo(fiSavedState.Directory.FullName + @"\SavedState.xml.Backup");
                    fiSavedState.CopyTo(state.BackupFileInfo.FullName, true);
                }

                // Move in our own saved state so that results are predictable
                var fiOurs = new FileInfo(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "SavedState.xml"));
                fiOurs.CopyTo(fiSavedState.FullName, true);

                return true;
            }
            catch (Exception ex)
            {
                state.AddReport(String.Format("Profile setup failed with unexpected exception '{0}'", ex.Message));
                return false;
            }
        }
    

开发者ID:Dijji,项目名称:FileMeta,代码行数:28,代码来源:Common.cs

在SetupProfileForTestRun()方法中,FileInfo的代码示例类中的CopyTo的代码示例方法一共出现了2次, 见黄色背景高亮显示的地方,欢迎大家点赞

FileInfo.CopyTo的代码示例3 - CopyAll()

    using System.IO;

        /// 
        /// Copies the folder and all of its files and folders 
        /// including subfolders to the target location
        /// 
        /// 
        /// 
        public static void CopyAll(this string sourcePath, string targetPath)
        {
            // Get the subdirectories for the specified directory.
            DirectoryInfo dir = new DirectoryInfo(sourcePath);

            if (!dir.Exists)
            {
                throw new DirectoryNotFoundException(
                    "Source directory does not exist or could not be found: "
                    + sourcePath);
            }

            try
            {
                DirectoryInfo[] dirs = dir.GetDirectories();
                // If the destination directory doesn't exist, create it.
                if (!Directory.Exists(targetPath))
                {
                    Directory.CreateDirectory(targetPath);
                }

                // Get the files in the directory and copy them to the new location.
                FileInfo[] files = dir.GetFiles();
                foreach (FileInfo file in files)
                {
                    string temppath = Path.Combine(targetPath, file.Name);
                    file.CopyTo(temppath, false);
                }

                // Recursively copy subdirectories by calling itself on each subdirectory until there are no more to copy
                foreach (DirectoryInfo subdir in dirs)
                {
                    string temppath = Path.Combine(targetPath, subdir.Name);
                    CopyAll(subdir.FullName, temppath);
                }
            }
            catch (Exception)
            {
#if DEBUG
                throw;
#else
                MessageBox.Show(string.Format("Copying path {0} has failed, it will now be deleted for consistency", targetPath));
                RemoveFolderIfExists(targetPath);
#endif
            }

        }
    

开发者ID:Flow-Launcher,项目名称:Flow.Launcher,代码行数:55,代码来源:FilesFolders.cs

在CopyAll()方法中,FileInfo的代码示例类中的CopyTo的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞

FileInfo.CopyTo的代码示例4 - AddFileFromTemplate()

    using System.IO;

        /// 
        /// Called to add a file to the project from a template.
        /// Override to do it yourself if you want to customize the file
        /// 
        /// Full path of template file
        /// Full path of file once added to the project
        public virtual void AddFileFromTemplate(string source, string target)
        {
            if (String.IsNullOrEmpty(source))
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (String.IsNullOrEmpty(target))
            {
                throw new ArgumentNullException(nameof(target));
            }

            try
            {
                string directory = Path.GetDirectoryName(target);
                if (!String.IsNullOrEmpty(directory) && !Directory.Exists(directory))
                {
                    Directory.CreateDirectory(directory);
                }

                FileInfo fiOrg = new FileInfo(source);
                FileInfo fiNew = fiOrg.CopyTo(target, true);

                fiNew.Attributes = FileAttributes.Normal; // remove any read only attributes.
            }
            catch (IOException e)
            {
                Trace.WriteLine("Exception : " + e.Message);
            }
            catch (UnauthorizedAccessException e)
            {
                Trace.WriteLine("Exception : " + e.Message);
            }
            catch (ArgumentException e)
            {
                Trace.WriteLine("Exception : " + e.Message);
            }
            catch (NotSupportedException e)
            {
                Trace.WriteLine("Exception : " + e.Message);
            }
        }
    

开发者ID:EWSoftware,项目名称:SHFB,代码行数:49,代码来源:ProjectNode.cs

在AddFileFromTemplate()方法中,FileInfo的代码示例类中的CopyTo的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞

FileInfo.CopyTo的代码示例5 - RecursivelyCopyDirectory()

    using System.IO;

        /// 
        /// Copy a directory recursively to the specified non-existing directory
        /// 
        /// Directory to copy from
        /// Directory to copy to
        public static void RecursivelyCopyDirectory(string source, string target)
        {
            // !EFW - Make sure we aren't copying a parent folder to one of it's child sub-folder
            string sourcePath, destPath;

            // Either one may or may not have the trailing backslash so remove it from both if present
            if(target.EndsWith("\\", StringComparison.Ordinal))
                sourcePath = Path.GetDirectoryName(source);
            else
                sourcePath = source;

            if(target.EndsWith("\\", StringComparison.Ordinal))
                destPath = Path.GetDirectoryName(target);
            else
                destPath = target;

            if(destPath.StartsWith(sourcePath, StringComparison.OrdinalIgnoreCase))
                throw new InvalidOperationException("Cannot copy a parent folder to one of its child sub-folder");

            // Make sure it doesn't already exist
            if(Directory.Exists(target))
                throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, SR.GetString(SR.FileOrFolderAlreadyExists, CultureInfo.CurrentUICulture), target));

            Directory.CreateDirectory(target);
            DirectoryInfo directory = new DirectoryInfo(source);

            // Copy files
            foreach(FileInfo file in directory.GetFiles())
            {
                file.CopyTo(Path.Combine(target, file.Name));
            }

            // Now recurse to child directories
            foreach(DirectoryInfo child in directory.GetDirectories())
            {
                RecursivelyCopyDirectory(child.FullName, Path.Combine(target, child.Name));
            }
        }
    

开发者ID:EWSoftware,项目名称:SHFB,代码行数:45,代码来源:Utilities.cs

在RecursivelyCopyDirectory()方法中,FileInfo的代码示例类中的CopyTo的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞

FileInfo.CopyTo的代码示例6 - CopyAll()

    using System.IO;

        public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
        {
            if (!Directory.Exists(target.FullName))
            {
                Directory.CreateDirectory(target.FullName);
            }

            foreach (FileInfo fi in source.GetFiles())
            {
                fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
            }

            foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
            {
                DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
                CopyAll(diSourceSubDir, nextTargetSubDir);
            }
        }
    

开发者ID:ShareX,项目名称:ShareX,代码行数:20,代码来源:FileHelpers.cs

在CopyAll()方法中,FileInfo的代码示例类中的CopyTo的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞

FileInfo.CopyTo的代码示例7 - CopyAll()

    using System.IO;

        public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
        {
            if (!Directory.Exists(target.FullName))
            {
                Directory.CreateDirectory(target.FullName);
            }

            foreach (FileInfo fi in source.GetFiles())
            {
                fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
            }

            foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
            {
                DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
                CopyAll(diSourceSubDir, nextTargetSubDir);
            }
        }
    

开发者ID:ShareX,项目名称:ShareX,代码行数:20,代码来源:Helpers.cs

在CopyAll()方法中,FileInfo的代码示例类中的CopyTo的代码示例方法一共出现了1次, 见黄色背景高亮显示的地方,欢迎大家点赞

本文中的FileInfo.CopyTo方法示例由csref.cn整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。