Skip to content

Commit

Permalink
add missing utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Valkirie committed Dec 27, 2021
1 parent d8e8109 commit d504414
Showing 1 changed file with 87 additions and 17 deletions.
104 changes: 87 additions & 17 deletions ControllerCommon/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Management;
using System.Net;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Text;

Expand Down Expand Up @@ -132,29 +133,40 @@ public static void OpenUrl(string url)
}
}

public static bool IsFileWritable(string filePath)
{
try
{
if (File.Exists(filePath))
{
using (var fs = new FileStream(filePath, FileMode.Open))
return fs.CanWrite;
}
else
{
bool CanWrite = false;
using (var fs = new FileStream(filePath, FileMode.Create))
CanWrite = fs.CanWrite;
File.Delete(filePath);
return CanWrite;
}
}
catch(Exception)
{
return false;
}
}

public static bool IsDirectoryWritable(string dirPath, bool throwIfFails = false)
public static bool IsDirectoryWritable(string dirPath)
{
try
{
using (FileStream fs = File.Create(
Path.Combine(
dirPath,
Path.GetRandomFileName()
),
1,
FileOptions.DeleteOnClose)
)
{ }
return true;
using (FileStream fs = File.Create(Path.Combine(dirPath,Path.GetRandomFileName()), 1, FileOptions.DeleteOnClose) )
return true;
}
catch (Exception ex)
catch (Exception)
{
Console.WriteLine(ex.Message);
if (throwIfFails)
throw;
else
return false;
return false;
}
}

Expand Down Expand Up @@ -182,5 +194,63 @@ public void SetFilterAttrs(double minCutoff, double beta)
axis1Filter.Beta = axis2Filter.Beta = axis3Filter.Beta = beta;
}
}

public static void SetDirectoryWritable(string processpath)
{
var rootDirectory = new DirectoryInfo(processpath);
var directorySecurity = rootDirectory.GetAccessControl();

SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
SecurityIdentifier adminitrators = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);

directorySecurity.AddAccessRule(
new FileSystemAccessRule(
everyone,
FileSystemRights.FullControl,
InheritanceFlags.None,
PropagationFlags.NoPropagateInherit,
AccessControlType.Allow));

directorySecurity.AddAccessRule(
new FileSystemAccessRule(
WindowsIdentity.GetCurrent().Name,
FileSystemRights.FullControl,
InheritanceFlags.None,
PropagationFlags.NoPropagateInherit,
AccessControlType.Allow));

directorySecurity.SetAccessRuleProtection(isProtected: true, preserveInheritance: false);

rootDirectory.SetAccessControl(directorySecurity);
}

public static void SetFileWritable(string processpath)
{
var rootFile = new FileInfo(processpath);
var fileSecurity = rootFile.GetAccessControl();

SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
SecurityIdentifier adminitrators = new SecurityIdentifier(WellKnownSidType.BuiltinAdministratorsSid, null);

fileSecurity.AddAccessRule(
new FileSystemAccessRule(
everyone,
FileSystemRights.FullControl,
InheritanceFlags.None,
PropagationFlags.NoPropagateInherit,
AccessControlType.Allow));

fileSecurity.AddAccessRule(
new FileSystemAccessRule(
WindowsIdentity.GetCurrent().Name,
FileSystemRights.FullControl,
InheritanceFlags.None,
PropagationFlags.NoPropagateInherit,
AccessControlType.Allow));

fileSecurity.SetAccessRuleProtection(isProtected: true, preserveInheritance: false);

rootFile.SetAccessControl(fileSecurity);
}
}
}

0 comments on commit d504414

Please sign in to comment.