C# File.Move的代码示例

File.Move方法的主要功能描述

将指定文件移动到新位置,并提供指定新文件名的选项。

通过代码示例来学习C# File.Move方法

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


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

File.Move的代码示例1 - TryBackupFiles()

    using System.IO;

        private bool TryBackupFiles(ITracer tracer, GVFSEnlistment enlistment, string backupRoot)
        {
            string backupSrc = Path.Combine(backupRoot, "src");
            string backupGit = Path.Combine(backupRoot, ".git");
            string backupGvfs = Path.Combine(backupRoot, GVFSPlatform.Instance.Constants.DotGVFSRoot);
            string backupDatabases = Path.Combine(backupGvfs, GVFSConstants.DotGVFS.Databases.Name);

            string errorMessage = string.Empty;
            if (!this.ShowStatusWhileRunning(
                () =>
                {
                    string ioError;
                    if (!this.TryIO(tracer, () => Directory.CreateDirectory(backupRoot), "Create backup directory", out ioError) ||
                        !this.TryIO(tracer, () => Directory.CreateDirectory(backupGit), "Create backup .git directory", out ioError) ||
                        !this.TryIO(tracer, () => Directory.CreateDirectory(backupGvfs), "Create backup .gvfs directory", out ioError) ||
                        !this.TryIO(tracer, () => Directory.CreateDirectory(backupDatabases), "Create backup .gvfs databases directory", out ioError))
                    {
                        errorMessage = "Failed to create backup folders at " + backupRoot + ": " + ioError;
                        return false;
                    }

                    // Move the current src folder to the backup location...
                    if (!this.TryIO(tracer, () => Directory.Move(enlistment.WorkingDirectoryBackingRoot, backupSrc), "Move the src folder", out ioError))
                    {
                        errorMessage = "Failed to move the src folder: " + ioError + Environment.NewLine;
                        errorMessage += "Make sure you have no open handles or running processes in the src folder";
                        return false;
                    }

                    // ... but move the .git folder back to the new src folder so we can preserve objects, refs, logs...
                    if (!this.TryIO(tracer, () => Directory.CreateDirectory(enlistment.WorkingDirectoryBackingRoot), "Create new src folder", out errorMessage) ||
                        !this.TryIO(tracer, () => Directory.Move(Path.Combine(backupSrc, ".git"), enlistment.DotGitRoot), "Keep existing .git folder", out errorMessage))
                    {
                        return false;
                    }

                    // ... backup the .gvfs hydration-related data structures...
                    string databasesFolder = Path.Combine(enlistment.DotGVFSRoot, GVFSConstants.DotGVFS.Databases.Name);
                    if (!this.TryBackupFilesInFolder(tracer, databasesFolder, backupDatabases, searchPattern: "*", filenamesToSkip: "RepoMetadata.dat"))
                    {
                        return false;
                    }

                    // ... backup everything related to the .git\index...
                    if (!this.TryIO(
                            tracer,
                            () => File.Move(
                                Path.Combine(enlistment.DotGitRoot, GVFSConstants.DotGit.IndexName),
                                Path.Combine(backupGit, GVFSConstants.DotGit.IndexName)),
                            "Backup the git index",
                            out errorMessage) ||
                        !this.TryIO(
                            tracer,
                            () => File.Move(
                                Path.Combine(enlistment.DotGVFSRoot, GitIndexProjection.ProjectionIndexBackupName),
                                Path.Combine(backupGvfs, GitIndexProjection.ProjectionIndexBackupName)),
                            "Backup GVFS_projection",
                            out errorMessage))
                    {
                        return false;
                    }

                    // ... backup all .git\*.lock files
                    if (!this.TryBackupFilesInFolder(tracer, enlistment.DotGitRoot, backupGit, searchPattern: "*.lock"))
                    {
                        return false;
                    }

                    return true;
                },
                "Backing up your files"))
            {
                this.Output.WriteLine();
                this.WriteMessage(tracer, "ERROR: " + errorMessage);

                return false;
            }

            return true;
        }
    

开发者ID: microsoft,   项目名称: VFSForGit,   代码行数: 82,   代码来源: DehydrateVerb.cs

在microsoft提供的TryBackupFiles()方法中,该源代码示例一共有82行, 其中使用了File.Move()4次, 并且小编将这些方法高亮显示出来了,希望对您了解File.Move()有帮助。 如果您觉得有帮助的话,请帮忙点赞或转发。
该代码示例的点赞次数为3, 点赞数越大, 从某种程度说明这个示例对了解File.Move()可能更有帮助。

File.Move的代码示例2 - TryRenameToBackupFile()

    using System.IO;

        protected bool TryRenameToBackupFile(string filePath, out string backupPath, List messages)
        {
            backupPath = filePath + BackupExtension;
            try
            {
                File.Move(filePath, backupPath);
                this.Tracer.RelatedEvent(EventLevel.Informational, "FileMoved", new EventMetadata { { "SourcePath", filePath }, { "DestinationPath", backupPath } });
            }
            catch (Exception e)
            {
                messages.Add("Failed to back up " + filePath + " to " + backupPath);
                this.Tracer.RelatedError("Exception while moving " + filePath + " to " + backupPath + ": " + e.ToString());
                return false;
            }

            return true;
        }
    

开发者ID: microsoft,   项目名称: VFSForGit,   代码行数: 19,   代码来源: RepairJob.cs

在microsoft提供的TryRenameToBackupFile()方法中,该源代码示例一共有19行, 其中使用了File.Move()1次, 并且小编将这些方法高亮显示出来了,希望对您了解File.Move()有帮助。 如果您觉得有帮助的话,请帮忙点赞或转发。
该代码示例的点赞次数为3, 点赞数越大, 从某种程度说明这个示例对了解File.Move()可能更有帮助。

File.Move的代码示例3 - EditInExternalProgram()

    using System.IO;
        private void EditInExternalProgram(bool UseDefaultEditor = true)
        {
            if (!ActiveTexture.CanEdit || !IsFinished)
                return;

            ImageProgramSettings settings = new ImageProgramSettings();
            settings.LoadImage(ActiveTexture);
            if (settings.ShowDialog() == DialogResult.OK)
            {
                UseDefaultEditor = !settings.OpenDefaultProgramSelection;

                string UseExtension = settings.GetSelectedExtension();
                FormatToChange = settings.GetSelectedImageFormat();

                string TemporaryName = Path.GetTempFileName();
                Utils.DeleteIfExists(Path.ChangeExtension(TemporaryName, UseExtension));
                File.Move(TemporaryName, Path.ChangeExtension(TemporaryName, UseExtension));
                TemporaryName = Path.ChangeExtension(TemporaryName, UseExtension);

                switch (UseExtension)
                {
                    case ".dds":
                        ActiveTexture.SaveDDS(TemporaryName, true, false, CurArrayDisplayLevel, CurMipDisplayLevel);
                        break;
                    case ".astc":
                        ActiveTexture.SaveASTC(TemporaryName, true, false, CurArrayDisplayLevel, CurMipDisplayLevel);
                        break;
                    case ".tga":
                        ActiveTexture.SaveTGA(TemporaryName, true, false, CurArrayDisplayLevel, CurMipDisplayLevel);
                        break;
                    default:
                        ActiveTexture.SaveBitMap(TemporaryName, true, false, CurArrayDisplayLevel, CurMipDisplayLevel);
                        break;
                }

                //Start watching for changes
                FileWatcher.EnableRaisingEvents = true;
                FileWatcher.Filter = Path.GetFileName(TemporaryName);

                if (UseDefaultEditor)
                    Process.Start(TemporaryName);
                else
                    ShowOpenWithDialog(TemporaryName);
            }
        }
    

开发者ID: KillzXGaming,   项目名称: Switch-Toolbox,   代码行数: 46,   代码来源: ImageEditorBase.cs

在KillzXGaming提供的EditInExternalProgram()方法中,该源代码示例一共有46行, 其中使用了File.Move()1次, 并且小编将这些方法高亮显示出来了,希望对您了解File.Move()有帮助。 如果您觉得有帮助的话,请帮忙点赞或转发。
该代码示例的点赞次数为3, 点赞数越大, 从某种程度说明这个示例对了解File.Move()可能更有帮助。

File.Move的代码示例4 - Install()

    using System.IO;
        static void Install()
        {
            Console.WriteLine("Installing...");
            foreach (string dir in Directory.GetDirectories("master/"))
            {
                SetAccessRule(folderDir);
                SetAccessRule(dir);

                string dirName = new DirectoryInfo(dir).Name;
                string destDir = Path.Combine(folderDir, dirName + @"\");

                //Skip hash directory
                if (dirName.Equals("Hashes", StringComparison.CurrentCultureIgnoreCase))
                    continue;

                if (Directory.Exists(destDir))
                {
                    Directory.Delete(destDir, true);
                }

                if (Directory.Exists(destDir))
                    Directory.Delete(destDir, true);

                Directory.Move(dir, destDir);
            }
            foreach (string file in Directory.GetFiles("master/"))
            {
                if (file.Contains("Updater.exe") || file.Contains("Updater.exe.config")
                    || file.Contains("Updater.pdb") || file.Contains("Octokit.dll"))
                    continue;

                SetAccessRule(file);
                SetAccessRule(folderDir);

                string destFile = Path.Combine(folderDir, Path.GetFileName(file));
                if (File.Exists(destFile))
                    File.Delete(destFile);

                File.Move(file, destFile);
            }
        }
    

开发者ID: KillzXGaming,   项目名称: Switch-Toolbox,   代码行数: 42,   代码来源: Program.cs

在KillzXGaming提供的Install()方法中,该源代码示例一共有42行, 其中使用了File.Move()2次, 并且小编将这些方法高亮显示出来了,希望对您了解File.Move()有帮助。 如果您觉得有帮助的话,请帮忙点赞或转发。
该代码示例的点赞次数为3, 点赞数越大, 从某种程度说明这个示例对了解File.Move()可能更有帮助。

File.Move的代码示例5 - SaveFileFormat()

    using System.IO;
        /// 
        /// Saves the  as a file from the given 
        /// 
        /// The format instance of the file being saved
        /// The name of the file
        /// The Alignment used for compression. Used for Yaz0 compression type. 
        /// Toggle for showing compression dialog
        /// 
        public static void SaveFileFormat(IFileFormat FileFormat, string FileName, bool EnableDialog = true, string DetailsLog = "")
        {
            //These always get created on loading a file,however not on creating a new file
            if (FileFormat.IFileInfo == null)
                throw new System.NotImplementedException("Make sure to impliment a IFileInfo instance if a format is being created!");

            Cursor.Current = Cursors.WaitCursor;
            FileFormat.FilePath = FileName;

            string compressionLog = "";
            if (FileFormat.IFileInfo.FileIsCompressed || FileFormat.IFileInfo.InArchive
                || Path.GetExtension(FileName) == ".szs" || Path.GetExtension(FileName) == ".sbfres"
                || Path.GetExtension(FileName) == ".mc")
            {
                //Todo find more optmial way to handle memory with files in archives
                //Also make compression require streams
                var mem = new System.IO.MemoryStream();
                FileFormat.Save(mem);
                mem =  new System.IO.MemoryStream(mem.ToArray());

                FileFormat.IFileInfo.DecompressedSize = (uint)mem.Length;

                var finalStream = CompressFileFormat(
                    FileFormat.IFileInfo.FileCompression,
                    mem,
                    FileFormat.IFileInfo.FileIsCompressed,
                    FileFormat.IFileInfo.Alignment,
                    FileName,
                    EnableDialog);

                compressionLog = finalStream.Item2;
                Stream compressionStream = finalStream.Item1;

                FileFormat.IFileInfo.CompressedSize = (uint)compressionStream.Length;
                compressionStream.ExportToFile(FileName);

                DetailsLog += "\n" + SatisfyFileTables(FileFormat, FileName, compressionStream,
                                    FileFormat.IFileInfo.DecompressedSize,
                                    FileFormat.IFileInfo.CompressedSize,
                                    FileFormat.IFileInfo.FileIsCompressed);

                compressionStream.Flush();
                compressionStream.Close();
            }
            else
            {
                //Check if a stream is active and the file is beinng saved to the same opened file
                if (FileFormat is ISaveOpenedFileStream && FileFormat.FilePath == FileName && File.Exists(FileName))
                {
                    string savedPath = Path.GetDirectoryName(FileName);
                    string tempPath = Path.Combine(savedPath, "tempST.bin");

                    //Save a temporary file first to not disturb the opened file
                    using (var fileStream = new FileStream(tempPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
                    {
                        FileFormat.Save(fileStream);
                        FileFormat.Unload();

                        //After saving is done remove the existing file
                        File.Delete(FileName);

                        //Now move and rename our temp file to the new file path
                        File.Move(tempPath, FileName);

                        FileFormat.Load(File.OpenRead(FileName));

                        var activeForm = LibraryGUI.GetActiveForm();
                        if (activeForm != null && activeForm is ObjectEditor)
                            ((ObjectEditor)activeForm).ReloadArchiveFile(FileFormat);
                    }
                }
                else
                {
                    using (var fileStream = new FileStream(FileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite))
                    {
                        FileFormat.Save(fileStream);
                    }
                }
            }

            if (EnableDialog)
            {
                if (compressionLog != string.Empty)
                    MessageBox.Show($"File has been saved to {FileName}. Compressed time: {compressionLog}", "Save Notification");
                else
                    MessageBox.Show($"File has been saved to {FileName}", "Save Notification");
            }

            //   STSaveLogDialog.Show($"File has been saved to {FileName}", "Save Notification", DetailsLog);
            Cursor.Current = Cursors.Default;
        }
    

开发者ID: KillzXGaming,   项目名称: Switch-Toolbox,   代码行数: 100,   代码来源: STFileSaver.cs

在KillzXGaming提供的SaveFileFormat()方法中,该源代码示例一共有100行, 其中使用了File.Move()1次, 并且小编将这些方法高亮显示出来了,希望对您了解File.Move()有帮助。 如果您觉得有帮助的话,请帮忙点赞或转发。
该代码示例的点赞次数为3, 点赞数越大, 从某种程度说明这个示例对了解File.Move()可能更有帮助。

File.Move的代码示例6 - GetExternalDictionaries()

    using System.IO;

        static byte[] GetExternalDictionaries()
        {
            byte[] dictionary = new byte[0];

            var userDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SwitchToolbox");
            if (!Directory.Exists(userDir))
                Directory.CreateDirectory(userDir);

            //Create folder for TOTK contents if it does not exist
            if (!Directory.Exists(Path.Combine(userDir, "TOTK")))
                Directory.CreateDirectory(Path.Combine(userDir, "TOTK"));

            string folder = Path.Combine(userDir, "TOTK", "ZstdDictionaries");

            if (!Directory.Exists(folder))
                Directory.CreateDirectory(folder);

            void TransferZDic(string path)
            {
                //Check if old directory contains the file and move it
                string fileOld = Path.Combine(Runtime.ExecutableDir, "Lib", "ZstdDictionaries", path);
                string fileNew = Path.Combine(folder, path);
                if (!File.Exists(fileNew) && File.Exists(fileOld))
                {
                    File.Move(fileOld, fileNew); 
                }
            }
            TransferZDic("bcett.byml.zsdic");
            TransferZDic("pack.zsdic");
            TransferZDic("zs.zsdic");

            if (Directory.Exists(folder))
            {
                void CheckZDic(string fileName, string expectedExtension)
                {
                    //Dictionary already set
                    if (dictionary.Length != 0) return;

                    string zDictPath = Path.Combine(folder, fileName);
                    //Then check if the input file uses the expected extension
                    if (File.Exists(zDictPath) && fileNameTemp.EndsWith(expectedExtension))
                        dictionary = File.ReadAllBytes(zDictPath);
                }

                //Order matters, zs must go last
                CheckZDic("bcett.byml.zsdic",  "bcett.byml.zs" );
                CheckZDic("pack.zsdic", "pack.zs" );
                CheckZDic("zs.zsdic", ".zs" );
            }
            return dictionary;
        }
    

开发者ID: KillzXGaming,   项目名称: Switch-Toolbox,   代码行数: 53,   代码来源: Zstb.cs

在KillzXGaming提供的GetExternalDictionaries()方法中,该源代码示例一共有53行, 其中使用了File.Move()1次, 并且小编将这些方法高亮显示出来了,希望对您了解File.Move()有帮助。 如果您觉得有帮助的话,请帮忙点赞或转发。
该代码示例的点赞次数为3, 点赞数越大, 从某种程度说明这个示例对了解File.Move()可能更有帮助。

File.Move()方法的常见问题及解答

如何使用C#将指定文件移动到新位置,并提供指定新文件名的选项。

在C#中,一般可以使用System.IO中的File.Move方法来实现; 本文中提供了7个File.Move的示例, 你看一下是否对你有帮助。

C#中File.Move的用法说明

File.Move主要用于将指定文件移动到新位置,并提供指定新文件名的选项。, 它是System.IO中的一个常见方法,您也可以参考官方帮助文档获取更详细的用法说明。

C#中File.Move()的常见错误类型及注意事项

File.Move的错误类型有很多, 这里就不一一阐述了,本文只列出一些常见的代码示例供参考,大家可以看一下代码中Catch语句中是否有常见的错误捕获及处理。

C#中File.Move()的构造函数有哪些

File.Move构造函数功能基本类似,只是参数不同; 目前主流的集成开发环境都已经带智能提醒了,如:Visual Studio; 大家可以非常轻松的通过Visual Studio中的智能提醒,了解对应构造函数的用法。

如何使用ChartGPT写一段File.Move的代码

你可以在ChartGPT中输入如下的指令:"提供一个如何使用File.Move的C#代码示例"
ChartGPT写出的代码和本文中的小编提供的代码的区别。 ChartGPT发展到现在已经非常聪明了,但需要使用这提供非常专业的问题,才可能有比较好的源代码示例; 而本文中, 小编已经帮您列出来基本所有类和所有方法的使用示例, 而且这些示例基本都是一些网络大佬提供的源码,可以更方便的供一些开发菜鸟或者资深开发参考和学习。

File.Move所在的类及名称空间

File.Move是System.IO下的方法。

什么是File.Move?

File.Move是System.IO下的一个方法, 一般用于将指定文件移动到新位置,并提供指定新文件名的选项。

File.Move怎么使用?

File.Move使用上比较简单,可以参考MSDN中的帮助文档,也参考本文中提供的7个使用示例。

File.Move菜鸟教程

对于菜鸟来说,本文中提供的7个File.Move写法都将非常直观的帮您掌握File.Move的用法,是一个不错的参考教程。

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