Nexus.Client.Util.FileUtil.ForceDelete C# (CSharp) Method

ForceDelete() public static method

Forces deletion of the given path.
This method is recursive if the given path is a directory. This method will clear read only/system attributes if required to delete the path.
public static ForceDelete ( string p_strPath ) : void
p_strPath string The path to delete.
return void
		public static void ForceDelete(string p_strPath)
		{
			for (Int32 i = 0; i < 5; i++)
			{
				try
				{
					if (File.Exists(p_strPath))
						File.Delete(p_strPath);
					else if (Directory.Exists(p_strPath))
						Directory.Delete(p_strPath, true);
					return;
				}
				catch (Exception e)
				{
					if (!(e is IOException || e is UnauthorizedAccessException || e is DirectoryNotFoundException))
						throw;
					try
					{
						ClearAttributes(p_strPath, true);
					}
					catch (Exception ex)
					{
						if (!(ex is ArgumentException || ex is DirectoryNotFoundException))
							throw;
						//we couldn't clear the attributes
					}
				}
			}
		}

Usage Example

Example #1
0
 /// <summary>
 /// Ends a read-only transaction.
 /// </summary>
 /// <remarks>
 /// This takes the archive out of read-only mode, and releases any used resources.
 /// </remarks>
 public void EndReadOnlyTransaction()
 {
     if (m_szeReadOnlyExtractor != null)
     {
         m_szeReadOnlyExtractor.Dispose();
     }
     m_szeReadOnlyExtractor = null;
     if (!String.IsNullOrEmpty(m_strReadOnlyTempDirectory))
     {
         FileUtil.ForceDelete(m_strReadOnlyTempDirectory);
     }
     m_strReadOnlyTempDirectory = null;
 }