-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
126 lines (116 loc) · 5.31 KB
/
Program.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System.Runtime.InteropServices;
using System.IO.Compression;
namespace FontsInstaller
{
class Program
{
public static int fontsInstalled = 0;
static void Main()
{
[System.Runtime.InteropServices.DllImport("gdi32.dll", EntryPoint = "AddFontResourceW", SetLastError = true)]
static extern int AddFontResource([In][MarshalAs(UnmanagedType.LPWStr)]
string lpFileName);
Console.WriteLine("Paste fonts directory path: ");
string folderPath = Console.ReadLine();
try
{
DirectorySearch(folderPath);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
finally
{
DisplayInstalledFontsNumber(fontsInstalled);
}
static void DirectorySearch(string dir)
{
try
{
foreach (string file in Directory.GetFiles(dir))
{
if (Path.GetExtension(file) == ".zip")
{
try
{
string[] fontFolderParts = { dir, Path.GetFileNameWithoutExtension(file) };
var fontFolderPath = Path.Combine(fontFolderParts);
Console.WriteLine(fontFolderPath);
ZipFile.ExtractToDirectory(file, fontFolderPath);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Archive extraction error: " + ex.Message);
Console.ResetColor();
}
}
if (Path.GetExtension(file) == ".ttf" || Path.GetExtension(file) == ".otf")
{
string fontsFolder = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);
string fontName = Path.GetFileName(file);
var newPath = Path.Combine(fontsFolder, fontName);
Console.WriteLine(newPath);
try
{
File.Copy(file, newPath);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Move file error: " + ex.Message);
Console.ResetColor();
}
try
{
var result = AddFontResource(newPath);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Successfully added: " + fontName);
Console.ResetColor();
}
catch (Exception ex)
{
Console.WriteLine("Add font error: " + ex.Message);
}
try
{
//Add font's record to the registry
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.LocalMachine.
CreateSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts");
key.SetValue(Path.GetFileNameWithoutExtension(fontName) + " (TrueType)", fontName);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(fontName + " Successfully added to the registry.");
Console.ResetColor();
fontsInstalled++;
key.Close();
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Add registry item exception: " + ex.Message);
Console.ResetColor();
}
}
}
foreach (string directory in Directory.GetDirectories(dir))
{
DirectorySearch(directory);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
static void DisplayInstalledFontsNumber(int numberofFontsInstaled)
{
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = numberofFontsInstaled > 0 ? ConsoleColor.DarkGreen : ConsoleColor.Red;
Console.WriteLine(numberofFontsInstaled + " fonts installed.");
Console.ResetColor();
}
Console.ReadLine();
}
}
}