diff --git a/FileSystem/DirectoryIOLinux.cs b/FileSystem/DirectoryIOLinux.cs
index 2da2bb4..1d0a680 100644
--- a/FileSystem/DirectoryIOLinux.cs
+++ b/FileSystem/DirectoryIOLinux.cs
@@ -85,7 +85,7 @@ public void DeleteDirectory(string path)
{
if (Directory.Exists(path))
{
- AllowWritesOnDirectory(path);
+ TryAllowWritesOnDirectory(path);
Directory.Delete(path, true);
}
}
@@ -166,8 +166,8 @@ public void MoveDirectory(string source, string target, bool move = true, bool f
{
string sourcePath = source.TrimEnd('\\', ' ');
string targetPath = target.TrimEnd('\\', ' ');
- AllowWritesOnDirectory(sourcePath);
- AllowWritesOnDirectory(targetPath);
+ TryAllowWritesOnDirectory(sourcePath);
+ TryAllowWritesOnDirectory(targetPath);
var files = Directory.EnumerateFiles(sourcePath, "*", SearchOption.AllDirectories)
.GroupBy(s => Path.GetDirectoryName(s));
@@ -195,8 +195,8 @@ public void MoveDirectory(string source, string target, bool move = true, bool f
///
public void Unzip(string zipPath, string destinationDir)
{
- AllowWritesOnDirectory(zipPath);
- AllowWritesOnDirectory(destinationDir);
+ TryAllowWritesOnDirectory(zipPath);
+ TryAllowWritesOnDirectory(destinationDir);
ZipFile.ExtractToDirectory(zipPath, destinationDir);
}
@@ -233,6 +233,20 @@ public void AllowWritesOnDirectory(string path)
}
}
+ ///
+ public bool TryAllowWritesOnDirectory(string path)
+ {
+ try
+ {
+ AllowWritesOnDirectory(path);
+ return true;
+ }
+ catch
+ {
+ return false;
+ }
+ }
+
public IDirectoryInfoIO CreateDirectory(string path)
{
var dir = Directory.CreateDirectory(path);
diff --git a/FileSystem/DirectoryIOWin.cs b/FileSystem/DirectoryIOWin.cs
index a048730..e6fbb92 100644
--- a/FileSystem/DirectoryIOWin.cs
+++ b/FileSystem/DirectoryIOWin.cs
@@ -89,7 +89,7 @@ public void DeleteDirectory(string path)
{
if (Directory.Exists(path))
{
- AllowWritesOnDirectory(path);
+ TryAllowWritesOnDirectory(path);
Directory.Delete(path, true);
}
}
@@ -170,8 +170,8 @@ public void MoveDirectory(string source, string target, bool move = true, bool f
{
string sourcePath = source.TrimEnd('\\', ' ');
string targetPath = target.TrimEnd('\\', ' ');
- AllowWritesOnDirectory(sourcePath);
- AllowWritesOnDirectory(targetPath);
+ TryAllowWritesOnDirectory(sourcePath);
+ TryAllowWritesOnDirectory(targetPath);
var files = Directory.EnumerateFiles(sourcePath, "*", SearchOption.AllDirectories)
.GroupBy(s => Path.GetDirectoryName(s));
@@ -199,8 +199,8 @@ public void MoveDirectory(string source, string target, bool move = true, bool f
///
public void Unzip(string zipPath, string destinationDir)
{
- AllowWritesOnDirectory(zipPath);
- AllowWritesOnDirectory(destinationDir);
+ TryAllowWritesOnDirectory(zipPath);
+ TryAllowWritesOnDirectory(destinationDir);
ZipFile.ExtractToDirectory(zipPath, destinationDir);
}
@@ -245,6 +245,20 @@ public void AllowWritesOnDirectory(string path)
}
}
+ ///
+ public bool TryAllowWritesOnDirectory(string path)
+ {
+ try
+ {
+ AllowWritesOnDirectory(path);
+ return true;
+ }
+ catch
+ {
+ return false;
+ }
+ }
+
public IDirectoryInfoIO CreateDirectory(string path)
{
var dir = Directory.CreateDirectory(path);
diff --git a/FileSystem/FileIOLinux.cs b/FileSystem/FileIOLinux.cs
index 3f76e61..d7976c6 100644
--- a/FileSystem/FileIOLinux.cs
+++ b/FileSystem/FileIOLinux.cs
@@ -10,7 +10,7 @@ internal sealed class FileIOLinux : IFileIO
///
public void AppendAllText(string filePath, string fileContent)
{
- AllowWritesOnFile(filePath);
+ TryAllowWritesOnFile(filePath);
File.AppendAllText(filePath, fileContent);
}
@@ -19,7 +19,7 @@ public void DeleteFile(string path)
{
if (File.Exists(path))
{
- AllowWritesOnFile(path);
+ TryAllowWritesOnFile(path);
File.Delete(path);
}
}
@@ -72,7 +72,7 @@ public string GetParentDirectory(string relativePath)
public void MoveFile(string filePath, string sourceFolder, string targetFolder, bool forceReplace)
{
string targetFile = Path.Combine(targetFolder, Path.GetFileName(filePath));
- AllowWritesOnFile(sourceFolder);
+ TryAllowWritesOnFile(sourceFolder);
if (!File.Exists(targetFile))
{
@@ -135,7 +135,7 @@ public string ReadAllText(string filePath, System.Text.Encoding encoding)
///
public void WriteAllText(string filePath, string fileContent)
{
- AllowWritesOnFile(filePath);
+ TryAllowWritesOnFile(filePath);
File.WriteAllText(filePath, fileContent);
}
@@ -154,12 +154,21 @@ public FileStream Create(string path, int bufferSize, FileOptions options)
return File.Create(path, bufferSize, options);
}
- private static void AllowWritesOnFile(string path)
+ private static bool TryAllowWritesOnFile(string path)
{
- var file = new FileInfo(path);
- if (file.Exists)
+ try
{
- file.Attributes = FileAttributes.Normal;
+ var file = new FileInfo(path);
+ if (file.Exists)
+ {
+ file.Attributes = FileAttributes.Normal;
+ }
+
+ return true;
+ }
+ catch
+ {
+ return false;
}
}
}
diff --git a/FileSystem/FileIOWin.cs b/FileSystem/FileIOWin.cs
index 88cd974..033a81d 100644
--- a/FileSystem/FileIOWin.cs
+++ b/FileSystem/FileIOWin.cs
@@ -13,7 +13,7 @@ internal sealed class FileIOWin : IFileIO
///
public void AppendAllText(string filePath, string fileContent)
{
- AllowWritesOnFile(filePath);
+ TryAllowWritesOnFile(filePath);
File.AppendAllText(filePath, fileContent);
}
@@ -22,7 +22,7 @@ public void DeleteFile(string path)
{
if (File.Exists(path))
{
- AllowWritesOnFile(path);
+ TryAllowWritesOnFile(path);
File.Delete(path);
}
}
@@ -77,14 +77,14 @@ public void MoveFile(string filePath, string sourceFolder, string targetFolder,
if (!File.Exists(targetFile))
{
- AllowWritesOnFile(sourceFolder);
+ TryAllowWritesOnFile(sourceFolder);
File.Move(filePath, targetFile);
}
else
{
if (forceReplace)
{
- AllowWritesOnFile(sourceFolder);
+ TryAllowWritesOnFile(sourceFolder);
DeleteFile(targetFile);
File.Move(filePath, targetFile);
}
@@ -99,7 +99,7 @@ public void MoveFile(string filePath, string sourceFolder, string targetFolder,
///
public byte[] ReadAllBytes(string filePath)
{
- AllowWritesOnFile(filePath);
+ TryAllowWritesOnFile(filePath);
if (File.Exists(filePath))
{
return File.ReadAllBytes(filePath);
@@ -113,7 +113,7 @@ public byte[] ReadAllBytes(string filePath)
///
public string ReadAllText(string filePath)
{
- AllowWritesOnFile(filePath);
+ TryAllowWritesOnFile(filePath);
if (File.Exists(filePath))
{
return File.ReadAllText(filePath);
@@ -127,7 +127,7 @@ public string ReadAllText(string filePath)
///
public string ReadAllText(string filePath, System.Text.Encoding encoding)
{
- AllowWritesOnFile(filePath);
+ TryAllowWritesOnFile(filePath);
if (File.Exists(filePath))
{
return File.ReadAllText(filePath, encoding);
@@ -141,7 +141,7 @@ public string ReadAllText(string filePath, System.Text.Encoding encoding)
///
public void WriteAllText(string filePath, string fileContent)
{
- AllowWritesOnFile(filePath);
+ TryAllowWritesOnFile(filePath);
File.WriteAllText(filePath, fileContent);
}
@@ -160,22 +160,31 @@ public System.IO.FileStream Create(string path, int bufferSize, System.IO.FileOp
return File.Create(path, bufferSize, options);
}
- private static void AllowWritesOnFile(string path)
+ private static bool TryAllowWritesOnFile(string path)
{
- // DirectoryInfo can be created from a file path
- //FileSecurity fileSec = new FileSecurity(path,AccessControlSections.All);
- //if (AccessPermissions.HasWritePermissionOnDir(fileSec)) return;
- var file = new FileInfo(path);
+ try
+ {
+ // DirectoryInfo can be created from a file path
+ //FileSecurity fileSec = new FileSecurity(path,AccessControlSections.All);
+ //if (AccessPermissions.HasWritePermissionOnDir(fileSec)) return;
+ var file = new FileInfo(path);
+
+ if (file.Exists)
+ {
+ file.Attributes = System.IO.FileAttributes.Normal;
+ }
- if (file.Exists)
+ //if (!AccessPermissions.WaitOnWritePermission(fileSec, new TimeSpan(0, 0, 10)))
+ //{
+ // throw new TimeoutException("Could not get write access on: " + path);
+ //}
+
+ return true;
+ }
+ catch
{
- file.Attributes = System.IO.FileAttributes.Normal;
+ return false;
}
-
- //if (!AccessPermissions.WaitOnWritePermission(fileSec, new TimeSpan(0, 0, 10)))
- //{
- // throw new TimeoutException("Could not get write access on: " + path);
- //}
}
}
}
\ No newline at end of file
diff --git a/FileSystem/IDirectoryIO.cs b/FileSystem/IDirectoryIO.cs
index 9435042..a731cf1 100644
--- a/FileSystem/IDirectoryIO.cs
+++ b/FileSystem/IDirectoryIO.cs
@@ -218,6 +218,12 @@ public interface IDirectoryIO
/// Path to the directory
void AllowWritesOnDirectory(string path);
+ ///
+ /// Allow writes to be performed on the directory and its contents.
+ ///
+ /// Path to the directory
+ bool TryAllowWritesOnDirectory(string path);
+
/// Creates all directories and subdirectories in the specified path unless they already exist.
/// The directory to create.
/// The directory specified by is a file.