help
Root Question Message
class Program
{
[DllImport("shell32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern void SHChangeNotify(
int wEventId, int uFlags, IntPtr dwItem1, IntPtr dwItem2);
static void Main(string[] args)
{
const string iconFolder = "icons";
string workingDiretory = Directory.GetCurrentDirectory();
string[] folders = Directory.GetDirectories(workingDiretory);
string[] icons = Directory.GetFiles(Path.Combine(workingDiretory, iconFolder));
for (int i = 0; i < folders.Length && folders[i] != "icons"; i++)
{
string folderPath = folders[i];
string iconPath = icons[i];
string iconName = Path.GetFileName(iconPath);
string desktopIniPath = Path.Combine(folderPath, "desktop.ini");
string hiddenPath = Path.Combine(folderPath, ".hidden");
string[] desktopIniContent = new string[]
{
"[.ShellClassInfo]",
$"IconResource={iconName},0",
"[ViewState]",
"Mode=",
"Vid=",
"FolderType=Generic"
};
string[] hiddenConent = new string[]
{
"desktop.ini",
iconName
};
File.WriteAllLines(Path.Join(folderPath,"desktop.ini"), desktopIniContent, Encoding.UTF8);
File.Copy(iconPath, Path.Join(folderPath, iconName));
File.WriteAllLines(Path.Join(folderPath, ".hidden"), hiddenConent, Encoding.UTF8);
File.SetAttributes(desktopIniPath,
File.GetAttributes(desktopIniPath)
| FileAttributes.Hidden
| FileAttributes.ReadOnly);
File.SetAttributes(iconPath,
File.GetAttributes(iconPath)
| FileAttributes.Hidden
| FileAttributes.ReadOnly);
File.SetAttributes(hiddenPath,
File.GetAttributes(hiddenPath)
| FileAttributes.Hidden
| FileAttributes.ReadOnly);
File.SetAttributes(folderPath,
File.GetAttributes(folderPath)
| FileAttributes.ReadOnly);
SHChangeNotify(0x08000000, 0x0000, (IntPtr)null, (IntPtr)null);
}
}
}
Unhandled exception. System.UnauthorizedAccessException: Access to the path '....\desktop.ini' is denied.
at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
at System.IO.Strategies.FileStreamHelpers.ChooseStrategy(FileStream fileStream, String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, Int64 preallocationSize)
at System.IO.StreamWriter.ValidateArgsAndOpenPath(String path, Boolean append, Encoding encoding, Int32 bufferSize)
at System.IO.File.WriteAllLines(String path, IEnumerable`1 contents, Encoding encoding)
at SetFolderIcon.Program.Main(String[] args) in
Program.cs:line 46
File.WriteAllLines(Path.Join(folderPath,"desktop.ini"), desktopIniContent,Encoding.UTF8);
UnauthorizedAccessException
path specified a file that is read-only.
-or-
path specified a file that is hidden.
-or-
This operation is not supported on the current platform.
-or-
path is a directory.
-or-
The caller does not have the required permission.
/close
Normal 128
The file is a standard file that has no special attributes. This attribute is valid only if it is used alone. Normal is supported on Windows, Linux, and macOS.
DirectoryInfo awd = new(folderPath)
{
Attributes = FileAttributes.Directory
};
DirectoryInfo directory = new DirectoryInfo(@"C:\my\directory");
DirectorySecurity security = directory.GetAccessControl();
security.AddAccessRule(new FileSystemAccessRule(@"MYDOMAIN\JohnDoe",
FileSystemRights.Modify,
AccessControlType.Deny));
directory.SetAccessControl(security);
System.Security.AccessControl
.\username