152 lines
5.4 KiB
C#
152 lines
5.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Management;
|
|
using System.Runtime.InteropServices;
|
|
using System.Security.AccessControl;
|
|
using System.Security.Principal;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace Mip
|
|
{
|
|
static class classDiskOperations
|
|
{
|
|
public static string TempPath = Application.StartupPath + @"\MipTemp";
|
|
|
|
public static void CreatePath()
|
|
{
|
|
if (Directory.Exists(TempPath)) //adresár už existuje
|
|
{
|
|
RemovePathProtection(TempPath);
|
|
System.IO.DirectoryInfo adresar = new System.IO.DirectoryInfo(TempPath);
|
|
|
|
foreach (System.IO.FileInfo file in adresar.GetFiles()) file.Delete();
|
|
foreach (System.IO.DirectoryInfo subDirectory in adresar.GetDirectories()) subDirectory.Delete(true);
|
|
//AddPathProtection(TempPath);
|
|
}
|
|
|
|
// adresar neexistuje - vytvorenie
|
|
else
|
|
{
|
|
Directory.CreateDirectory(TempPath);
|
|
//AddPathProtection(TempPath);
|
|
}
|
|
}
|
|
|
|
public static void AddPathProtection(string _Directory)
|
|
{
|
|
string UserName = "";
|
|
|
|
SelectQuery query = new SelectQuery("Win32_UserAccount");
|
|
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
|
|
foreach (ManagementObject envVar in searcher.Get())
|
|
{
|
|
UserName = envVar["Domain"].ToString() + "\\" + envVar["Name"].ToString();
|
|
|
|
DirectorySecurity ds = Directory.GetAccessControl(_Directory);
|
|
FileSystemAccessRule fsa = new FileSystemAccessRule(UserName, FileSystemRights.FullControl, AccessControlType.Deny);
|
|
|
|
ds.AddAccessRule(fsa);
|
|
Directory.SetAccessControl(_Directory, ds);
|
|
|
|
}
|
|
}
|
|
|
|
public static void RemovePathProtection(string _Directory)
|
|
{
|
|
string UserName = "";
|
|
|
|
SelectQuery query = new SelectQuery("Win32_UserAccount");
|
|
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
|
|
foreach (ManagementObject envVar in searcher.Get())
|
|
{
|
|
UserName = envVar["Domain"].ToString() + "\\" + envVar["Name"].ToString();
|
|
|
|
DirectorySecurity ds = Directory.GetAccessControl(_Directory);
|
|
FileSystemAccessRule fsa = new FileSystemAccessRule(UserName, FileSystemRights.FullControl, AccessControlType.Deny);
|
|
|
|
ds.RemoveAccessRule(fsa);
|
|
Directory.SetAccessControl(_Directory, ds);
|
|
|
|
}
|
|
}
|
|
|
|
public static void RemoveCurrentUserProtecion(string _Directory)
|
|
{
|
|
string UserName = "";
|
|
|
|
UserName = Environment.UserDomainName + "\\" + Environment.UserName;
|
|
|
|
DirectorySecurity ds = Directory.GetAccessControl(_Directory);
|
|
FileSystemAccessRule fsa = new FileSystemAccessRule(UserName, FileSystemRights.FullControl, AccessControlType.Deny);
|
|
|
|
ds.RemoveAccessRule(fsa);
|
|
Directory.SetAccessControl(_Directory, ds);
|
|
|
|
}
|
|
|
|
public static void AddCurrentUserProtecion(string _Directory)
|
|
{
|
|
string UserName = "";
|
|
|
|
UserName = Environment.UserDomainName + "\\" + Environment.UserName;
|
|
|
|
DirectorySecurity ds = Directory.GetAccessControl(_Directory);
|
|
FileSystemAccessRule fsa = new FileSystemAccessRule(UserName, FileSystemRights.FullControl, AccessControlType.Deny);
|
|
|
|
ds.AddAccessRule(fsa);
|
|
Directory.SetAccessControl(_Directory, ds);
|
|
|
|
}
|
|
|
|
public static void RemovePath(string _Directory)
|
|
{
|
|
System.IO.DirectoryInfo adresar = new System.IO.DirectoryInfo(_Directory);
|
|
|
|
foreach (System.IO.FileInfo file in adresar.GetFiles()) file.Delete();
|
|
foreach (System.IO.DirectoryInfo subDirectory in adresar.GetDirectories()) subDirectory.Delete(true);
|
|
|
|
Directory.Delete(_Directory);
|
|
}
|
|
|
|
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
|
private static extern bool DefineDosDevice(int flags, string devname, string path);
|
|
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
|
|
private static extern int QueryDosDevice(string devname, StringBuilder buffer, int bufSize);
|
|
|
|
public static void MapDrive(char letter, string path)
|
|
{
|
|
if (!DefineDosDevice(0, devName(letter), path))
|
|
throw new Win32Exception();
|
|
}
|
|
public static void UnmapDrive(char letter)
|
|
{
|
|
if (!DefineDosDevice(2, devName(letter), null))
|
|
throw new Win32Exception();
|
|
}
|
|
public static string GetDriveMapping(char letter)
|
|
{
|
|
var sb = new StringBuilder(259);
|
|
if (QueryDosDevice(devName(letter), sb, sb.Capacity) == 0)
|
|
{
|
|
// Return empty string if the drive is not mapped
|
|
int err = Marshal.GetLastWin32Error();
|
|
if (err == 2) return "";
|
|
throw new Win32Exception();
|
|
}
|
|
return sb.ToString().Substring(4);
|
|
}
|
|
|
|
private static string devName(char letter)
|
|
{
|
|
return new string(char.ToUpper(letter), 1) + ":";
|
|
}
|
|
|
|
}
|
|
|
|
}
|