This repository has been archived by the owner on Jun 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommonApplicationData.cs
109 lines (100 loc) · 4.87 KB
/
CommonApplicationData.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//Author: DaveyM69
//Date: 22 May 2010
//Link: http://www.codeproject.com/Tips/61987/Allow-write-modify-access-to-CommonApplicationData
using System;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;
namespace FileHasher
{
internal class CommonApplicationData
{
private static readonly string directory =
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
private readonly string applicationFolder;
private readonly string companyFolder;
/// <summary>
/// Creates a new instance of this class creating the specified company and application folders
/// if they don't already exist and optionally allows write/modify to all users.
/// </summary>
/// <param name="companyFolder">The name of the company's folder (normally the company name).</param>
/// <param name="applicationFolder">The name of the application's folder (normally the application name).</param>
/// <remarks>If the application folder already exists then permissions if requested are NOT altered.</remarks>
public CommonApplicationData(string companyFolder, string applicationFolder)
: this(companyFolder, applicationFolder, false)
{
}
/// <summary>
/// Creates a new instance of this class creating the specified company and application folders
/// if they don't already exist and optionally allows write/modify to all users.
/// </summary>
/// <param name="companyFolder">The name of the company's folder (normally the company name).</param>
/// <param name="applicationFolder">The name of the application's folder (normally the application name).</param>
/// <param name="allUsers">true to allow write/modify to all users; otherwise, false.</param>
/// <remarks>If the application folder already exists then permissions if requested are NOT altered.</remarks>
public CommonApplicationData(string companyFolder, string applicationFolder, bool allUsers)
{
this.applicationFolder = applicationFolder;
this.companyFolder = companyFolder;
CreateFolders(allUsers);
}
/// <summary>
/// Gets the path of the application's data folder.
/// </summary>
public string ApplicationFolderPath => Path.Combine(CompanyFolderPath, applicationFolder);
/// <summary>
/// Gets the path of the company's data folder.
/// </summary>
public string CompanyFolderPath => Path.Combine(directory, companyFolder);
private void CreateFolders(bool allUsers)
{
DirectoryInfo directoryInfo;
DirectorySecurity directorySecurity;
AccessRule rule;
var securityIdentifier = new SecurityIdentifier
(WellKnownSidType.BuiltinUsersSid, null);
if (!Directory.Exists(CompanyFolderPath))
{
directoryInfo = Directory.CreateDirectory(CompanyFolderPath);
bool modified;
directorySecurity = directoryInfo.GetAccessControl();
rule = new FileSystemAccessRule(
securityIdentifier,
FileSystemRights.Write |
FileSystemRights.ReadAndExecute |
FileSystemRights.Modify,
AccessControlType.Allow);
directorySecurity.ModifyAccessRule(AccessControlModification.Add, rule, out modified);
directoryInfo.SetAccessControl(directorySecurity);
}
if (!Directory.Exists(ApplicationFolderPath))
{
directoryInfo = Directory.CreateDirectory(ApplicationFolderPath);
if (allUsers)
{
bool modified;
directorySecurity = directoryInfo.GetAccessControl();
rule = new FileSystemAccessRule(
securityIdentifier,
FileSystemRights.Write |
FileSystemRights.ReadAndExecute |
FileSystemRights.Modify,
InheritanceFlags.ContainerInherit |
InheritanceFlags.ObjectInherit,
PropagationFlags.InheritOnly,
AccessControlType.Allow);
directorySecurity.ModifyAccessRule(AccessControlModification.Add, rule, out modified);
directoryInfo.SetAccessControl(directorySecurity);
}
}
}
/// <summary>
/// Returns the path of the application's data folder.
/// </summary>
/// <returns>The path of the application's data folder.</returns>
public override string ToString()
{
return ApplicationFolderPath;
}
}
}