Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
125 lines (106 loc) · 5 KB
/
Copy pathProgram.cs
File metadata and controls
125 lines (106 loc) · 5 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
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
125
using System;
using System.Diagnostics;
using System.IO;
using Microsoft.Win32;
class Program
{
// The name of the configuration file.
const string ConfigFileName = "config.txt";
static void Main()
{
// Get the directory where the launcher executable is located.
string launcherDirectory = AppDomain.CurrentDomain.BaseDirectory;
// Combine the directory with the config file name to get the full path.
string configFilePath = Path.Combine(launcherDirectory, ConfigFileName);
// Check if the config file exists. If not, inform the user and exit.
if (!File.Exists(configFilePath))
{
Console.WriteLine($"Error: The configuration file '{ConfigFileName}' was not found at '{configFilePath}'.");
Console.WriteLine("Please ensure the config.txt file is in the same directory as the launcher.");
Console.ReadKey(); // Wait for user input before closing.
return;
}
try
{
// Read all lines from the configuration file.
string[] configLines = File.ReadAllLines(configFilePath);
string gamePath = null;
string gpuValue = null;
// Parse the configuration lines to find the game path and GPU value.
foreach (string line in configLines)
{
// Clean up the line by trimming whitespace.
string trimmedLine = line.Trim();
// Skip empty or comment lines.
if (string.IsNullOrEmpty(trimmedLine) || trimmedLine.StartsWith("#"))
{
continue;
}
// Split the line at the first '=' to separate the key and value.
int separatorIndex = trimmedLine.IndexOf('=');
if (separatorIndex > 0)
{
string key = trimmedLine.Substring(0, separatorIndex).Trim();
string value = trimmedLine.Substring(separatorIndex + 1).Trim();
if (key.Equals("path", StringComparison.OrdinalIgnoreCase))
{
gamePath = value;
}
else if (key.Equals("gpu", StringComparison.OrdinalIgnoreCase))
{
gpuValue = value;
}
}
}
// Check if both the game path and GPU value were found.
if (string.IsNullOrEmpty(gamePath) || string.IsNullOrEmpty(gpuValue))
{
Console.WriteLine("Error: The config.txt file is missing 'path' or 'gpu' values.");
Console.ReadKey();
return;
}
// Construct the full path to the game executable.
string fullGamePath = Path.Combine(launcherDirectory, gamePath);
// Check if the game executable exists.
if (!File.Exists(fullGamePath))
{
Console.WriteLine($"Error: The game executable '{fullGamePath}' was not found.");
Console.WriteLine("Please check the 'path' value in your config.txt file.");
Console.ReadKey();
return;
}
// This is the correct registry key for all GPU preferences.
string registryKey = @"Software\Microsoft\DirectX\UserGpuPreferences";
// This is the correct value name, which is the full path to the executable.
string registryValueName = fullGamePath;
// This is the correct value data string.
string registryValueData = $"GpuPreference={gpuValue};";
// Use the correct key and value to set the preference.
// CreateSubKey will create the key if it does not exist.
using (RegistryKey userKey = Registry.CurrentUser.CreateSubKey(registryKey, true))
{
// Set the value with the full path as the name and the preference string as the data.
userKey.SetValue(registryValueName, registryValueData, RegistryValueKind.String);
}
Console.WriteLine($"Successfully set GPU preference for '{fullGamePath}'.");
// Start the game process.
Process.Start(fullGamePath);
}
catch (FileNotFoundException ex)
{
Console.WriteLine($"Error: File not found. Details: {ex.Message}");
Console.ReadKey();
}
catch (FormatException ex)
{
Console.WriteLine($"Error: The 'gpu' value in config.txt is not a valid number. Details: {ex.Message}");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine($"An unexpected error occurred: {ex.Message}");
Console.WriteLine("Please ensure you have the necessary permissions to modify the Windows Registry.");
Console.ReadKey();
}
}
}