當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。