当前位置: 首页>>代码示例>>C#>>正文


C# FileSystemInfo.DeleteFile方法代码示例

本文整理汇总了C#中System.IO.FileSystemInfo.DeleteFile方法的典型用法代码示例。如果您正苦于以下问题:C# FileSystemInfo.DeleteFile方法的具体用法?C# FileSystemInfo.DeleteFile怎么用?C# FileSystemInfo.DeleteFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.FileSystemInfo的用法示例。


在下文中一共展示了FileSystemInfo.DeleteFile方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: recursiveDelete

        private static bool recursiveDelete(string testName, FileSystemInfo fs, bool silent,  bool failOnError)
        {
            Debug.Assert(!(silent && failOnError));

            if (fs.IsFile())
            {
                fs.DeleteFile();
                return silent;
            }

            var dir = new DirectoryInfo(fs.FullName);
            if (!dir.Exists) return silent;

            try
            {
                FileSystemInfo[] ls = dir.GetFileSystemInfos();

                foreach (FileSystemInfo e in ls)
                {
                    silent = recursiveDelete(testName, e, silent, failOnError);
                }

                dir.Delete();
            }
            catch (IOException e)
            {
                ReportDeleteFailure(testName, failOnError, fs, e.Message);
            }

            return silent;
        }
开发者ID:stschake,项目名称:GitSharp,代码行数:31,代码来源:LocalDiskRepositoryTestCase.cs

示例2: recursiveDelete

        /// <summary>
        /// Utility method to delete a directory recursively. It is
        /// also used internally. If a file or directory cannot be removed
        /// it throws an AssertionFailure.
        /// </summary>
        /// <param name="fs"></param>
        /// <param name="silent"></param>
        /// <param name="name"></param>
        /// <param name="failOnError"></param>
        /// <returns></returns>
        protected static bool recursiveDelete(FileSystemInfo fs, bool silent, string name, bool failOnError)
        {
            Debug.Assert(!(silent && failOnError));

            if (fs.IsFile())
            {
                fs.DeleteFile();
                return silent;
            }

            var dir = new DirectoryInfo(fs.FullName);
            if (!dir.Exists) return silent;

            try
            {
                FileSystemInfo[] ls = dir.GetFileSystemInfos();

                foreach (FileSystemInfo e in ls)
                {
                    silent = recursiveDelete(e, silent, name, failOnError);
                }

                dir.Delete();
            }
            catch (IOException e)
            {
                //ReportDeleteFailure(name, failOnError, fs);
                Console.WriteLine(name + ": " + e.Message);
            }

            return silent;
        }
开发者ID:Squelch,项目名称:GitSharp,代码行数:42,代码来源:RepositoryTestCase.cs

示例3: recursiveDelete

        private static void recursiveDelete(string testName, FileSystemInfo fs,  bool failOnError)
        {
            if (fs.IsFile())
            {
                if (!fs.DeleteFile())
                {
                    throw new IOException("Unable to delete file: " + fs.FullName);
                }

                // Deleting a file only marks it for deletion, following code blocks until
                // the file is actually deleted. For a more thorough explanation see the
                // comment below @ the directory.Delete()
                while (File.Exists(fs.FullName))
                {
                    Thread.Sleep(0);
                }
                return;
            }

            var dir = new DirectoryInfo(fs.FullName);
            if (!Directory.Exists(dir.FullName)) return;

            try
            {
                FileSystemInfo[] ls = dir.GetFileSystemInfos();

                foreach (FileSystemInfo e in ls)
                {
                    recursiveDelete(testName, e, failOnError);
                }
                
                dir.Delete();

                // dir.Delete() only marks the directory for deletion (see also: http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/a2fcc569-1835-471f-b731-3fe9c6bcd2d9),
                // so it creates a race condition between the actual deletion of this directory, and the 
                // deletion of the parent directory. If this directory is not deleted when the parent directory
                // is tried to be deleted, an IOException ("Directory not empty") will be thrown.
                // The following code blocks untill the directory is actually deleted.
                // (btw, dir.Exists keeps returning true on my (Windows 7) machine, even if the Explorer and Command
                // say otherwise)
                while (Directory.Exists(dir.FullName))
                {
                    Thread.Sleep(0);
                }
            }
            catch (IOException ex)
            {
                ReportDeleteFailure(testName, failOnError, fs, ex.ToString());
            }
        }
开发者ID:yolanother,项目名称:GitSharp,代码行数:50,代码来源:LocalDiskRepositoryTestCase.cs


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