-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
74 lines (57 loc) · 2.57 KB
/
Copy pathProgram.cs
File metadata and controls
74 lines (57 loc) · 2.57 KB
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
using System;
using System.IO;
using Newtonsoft.Json;
class Program
{
static void Main()
{
// JSON file path
string fileNamesPath = "FileNames.json";
// Target Folder
string targetDirectory = @"C:\Users\ ... set the target folder path";
// JSON file read
string jsonData = File.ReadAllText(fileNamesPath);
// JSON data to jsonConvert
List<string> dataList = JsonConvert.DeserializeObject<List<string>>(jsonData);
// C# data to HashSet
HashSet<string> hSet = new HashSet<string>(dataList);
// Display folder info and check file names
DisplayFolderInfo(targetDirectory, hSet);
}
static void DisplayFolderInfo(string directoryPath, HashSet<string> hSet)
{
try
{
DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
if (hSet.Contains(directoryInfo.Name))
{
// If the folder name exists in the HashSet, write the folder name and path to the console
Console.WriteLine("Folder Name: " + directoryInfo.Name);
Console.WriteLine("Folder Path: " + directoryInfo.FullName);
Console.WriteLine();
string currentDirectory = Environment.CurrentDirectory;
string iconPath = currentDirectory + @"\Ico\folder-" + directoryInfo.Name + ".ico";
// Create the desktop.ini file inside the folder
string desktopIniPath = Path.Combine(directoryInfo.FullName, "desktop.ini");
string iniContent = "[.ShellClassInfo]\r\nIconResource=" + iconPath + ",0";
File.WriteAllText(desktopIniPath, iniContent);
File.SetAttributes(desktopIniPath, File.GetAttributes(desktopIniPath) | FileAttributes.System | FileAttributes.Hidden);
File.SetAttributes(directoryInfo.FullName, File.GetAttributes(directoryInfo.FullName) | FileAttributes.System);
}
// Get all subdirectories and recursively call the method for each subdirectory
foreach (var subdirectory in directoryInfo.GetDirectories())
{
DisplayFolderInfo(subdirectory.FullName, hSet);
}
}
catch (UnauthorizedAccessException)
{
// Handle unauthorized access (e.g., when you don't have permission to access certain folders)
Console.WriteLine("Unauthorized Access: " + directoryPath);
}
catch (Exception ex)
{
Console.WriteLine("Error: " + ex.Message);
}
}
}