C# File.Delete的代码示例

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

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


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

File.Delete的代码示例1 - RunDlssTweaksDllCleanup()

    using System.IO;

        string RunDlssTweaksDllCleanup(string prompt, string[] dllsToCleanup, string dllBasePath)
        {
            var choices = new List();
            var keepFileName = "";
            foreach (var dll in dllsToCleanup)
            {
                var dllFilename = Path.GetFileName(dll);
                var fileVersion = "";
                try
                {
                    var fileVersionInfo = FileVersionInfo.GetVersionInfo(dll);
                    fileVersion = $" (v{fileVersionInfo.FileVersion})";
                }
                catch { }
                choices.Add(dllFilename + fileVersion);
                keepFileName = dllFilename; // use last DLL as the default keeper
            }

            // User has multiple DLSSTweaks DLLs in the current folder, le sigh
            var result = Utility.MultipleChoice(prompt + "\r\n" +
                "To let ConfigTool clear these DLLs, enter the filename of the DLL you wish to keep.\r\n" +
                "The rest will then be removed, alternatively press Cancel to ignore this.\r\n\r\n" +
                "Detected DLLs:", "Multiple DLSSTweaks DLLs!", choices.ToArray(), "\r\n  ", ref keepFileName);
            if (result != DialogResult.OK || string.IsNullOrEmpty(keepFileName))
                return null;

            var keepFilePath = Path.Combine(dllBasePath, keepFileName);

            if (!File.Exists(keepFilePath))
            {
                MessageBox.Show($"File {keepFileName} not found, aborting cleanup.", "File cleanup failed");
                return null;
            }

            var filesToDelete = new List();
            foreach (var dll in dllsToCleanup)
                if (!string.Equals(Path.GetFileName(dll), keepFileName, StringComparison.OrdinalIgnoreCase))
                    filesToDelete.Add(dll);

            if (MessageBox.Show("The following files will be deleted:\n\n" +
                string.Join("\n", filesToDelete) + "\n\n" +
                "The following file will be kept:\n\n" +
                keepFilePath + "\n\n" +
                "Is this OK?", "Confirm File Deletion", MessageBoxButtons.OKCancel) != DialogResult.OK)
            {
                if (File.Exists(keepFilePath))
                    return keepFileName;

                return null;
            }

            foreach (var dll in filesToDelete)
            {
                if (!string.Equals(Path.GetFileName(dll), keepFileName, StringComparison.OrdinalIgnoreCase))
                    try
                    {
                        File.Delete(dll);
                    }
                    catch
                    {
                    }
            }

            if (File.Exists(keepFilePath))
                return keepFileName;

            return null;
        }
    

开发者ID:emoose,项目名称:DLSSTweaks,代码行数:70,代码来源:Main.cs

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

File.Delete的代码示例2 - NewFileAttributesAreUpdated()

    using System.IO;

        [TestCaseSource(typeof(FileRunnersAndFolders), nameof(FileRunnersAndFolders.Folders))]
        public void NewFileAttributesAreUpdated(string parentFolder)
        {
            string filename = Path.Combine(parentFolder, "FileAttributesAreUpdated");
            FileSystemRunner fileSystem = FileSystemRunner.DefaultRunner;

            string virtualFile = this.Enlistment.GetVirtualPathTo(filename);
            virtualFile.ShouldNotExistOnDisk(fileSystem);

            File.Create(virtualFile).Dispose();
            virtualFile.ShouldBeAFile(fileSystem);

            // Update defaults. FileInfo is not batched, so each of these will create a separate Open-Update-Close set.
            FileInfo before = new FileInfo(virtualFile);
            DateTime testValue = DateTime.Now + TimeSpan.FromDays(1);
            before.CreationTime = testValue;
            before.LastAccessTime = testValue;
            before.LastWriteTime = testValue;
            before.Attributes = FileAttributes.Hidden;

            // FileInfo caches information. We can refresh, but just to be absolutely sure...
            virtualFile.ShouldBeAFile(fileSystem).WithInfo(testValue, testValue, testValue, FileAttributes.Hidden);

            File.Delete(virtualFile);
            virtualFile.ShouldNotExistOnDisk(fileSystem);
        }
    

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

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

File.Delete的代码示例3 - HandleAllFileDeleteOperations()

    using System.IO;

        private void HandleAllFileDeleteOperations()
        {
            string path;
            while (this.diff.FileDeleteOperations.TryDequeue(out path))
            {
                if (this.HasFailures)
                {
                    return;
                }

                try
                {
                    if (File.Exists(path))
                    {
                        File.Delete(path);
                    }

                    Interlocked.Increment(ref this.fileDeleteCount);
                }
                catch (Exception ex)
                {
                    EventMetadata metadata = new EventMetadata();
                    metadata.Add("Operation", "DeleteFile");
                    metadata.Add("Path", path);
                    this.tracer.RelatedError(metadata, ex.Message);
                    this.HasFailures = true;
                }
            }
        }
    

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

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

File.Delete的代码示例4 - TryDeleteFile()

    using System.IO;

        protected bool TryDeleteFile(ITracer tracer, string fileName)
        {
            try
            {
                File.Delete(fileName);
            }
            catch (Exception e)
            {
                tracer.RelatedError("Failed to delete file {0}: {1}", fileName, e.ToString());
                return true;
            }

            return true;
        }
    

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

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

File.Delete的代码示例5 - CanFetchAndCheckoutAfterDeletingIndex()

    using System.IO;

        [TestCase]
        public void CanFetchAndCheckoutAfterDeletingIndex()
        {
            this.RunFastFetch("--checkout -b " + Settings.Default.Commitish);

            File.Delete(Path.Combine(this.fastFetchRepoRoot, ".git", "index"));
            this.RunFastFetch("--checkout -b " + Settings.Default.Commitish);

            this.CurrentBranchShouldEqual(Settings.Default.Commitish);

            this.fastFetchRepoRoot.ShouldBeADirectory(FileSystemRunner.DefaultRunner)
                .WithDeepStructure(FileSystemRunner.DefaultRunner, this.fastFetchControlRoot);
        }
    

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

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

File.Delete的代码示例6 - SavePersistedValue()

    using System.IO;

        private static void SavePersistedValue(string dotGVFSRoot, string key, string value)
        {
            string metadataPath = Path.Combine(dotGVFSRoot, RepoMetadataName);

            Dictionary repoMetadata = new Dictionary();
            string json;
            using (FileStream fs = new FileStream(metadataPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete))
            using (StreamReader reader = new StreamReader(fs))
            {
                while (!reader.EndOfStream)
                {
                    json = reader.ReadLine();
                    json.Substring(0, 2).ShouldEqual("A ");

                    KeyValuePair kvp = JsonConvert.DeserializeObject>(json.Substring(2));
                    repoMetadata.Add(kvp.Key, kvp.Value);
                }
            }

            repoMetadata[key] = value;

            string newRepoMetadataContents = string.Empty;

            foreach (KeyValuePair kvp in repoMetadata)
            {
                newRepoMetadataContents += "A " + JsonConvert.SerializeObject(kvp).Trim() + "\r\n";
            }

            File.WriteAllText(metadataPath, newRepoMetadataContents);
        }
    

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

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

File.Delete的代码示例7 - Dispose()

    using System.IO;

        /// >
        public void Dispose()
        {
            if (this.lockFilePath == null)
            {
                return;
            }

            if (this.lockFileStream == null)
            {
                throw new ObjectDisposedException(nameof(IndexLock));
            }

            this.lockFileStream.Dispose();
            this.lockFileStream = null;

            File.Delete(this.lockFilePath);
            this.lockFilePath = null;
        }
    

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

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

File.Delete的代码示例8 - MountingARepositoryThatRequiresPlaceholderUpdatesWorks()

    using System.IO;

        [TestCase]
        public void MountingARepositoryThatRequiresPlaceholderUpdatesWorks()
        {
            string placeholderRelativePath = Path.Combine("EnumerateAndReadTestFiles", "a.txt");
            string placeholderPath = this.Enlistment.GetVirtualPathTo(placeholderRelativePath);

            // Ensure the placeholder is on disk and hydrated
            placeholderPath.ShouldBeAFile(this.fileSystem).WithContents();

            this.Enlistment.UnmountGVFS();

            File.Delete(placeholderPath);
            GVFSHelpers.DeletePlaceholder(
                Path.Combine(this.Enlistment.DotGVFSRoot, TestConstants.Databases.VFSForGit),
                placeholderRelativePath);
            GVFSHelpers.SetPlaceholderUpdatesRequired(this.Enlistment.DotGVFSRoot, true);

            this.Enlistment.MountGVFS();
        }
    

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

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

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