The destination folder is not set, so you can have problems with the starting directory or the working directory, as well as access rights, especially under Windows 10 even with an administrator user. This may raise an exception and immediately close the console.
You should use a folder in the user area as a target, dedicated for your application.
You can try that:
string pathCommon = @"C:\Users\" + Environment.UserName + @"\AppData\Local";
string pathSource = pathCommon + @"\DeadByDaylight\Saved\Logs\";
string pathDestination = pathCommon + @"\MyCompany-ChangeThisName\MyApp-ChangeThisName\";
string filenameSource = pathSource + "DeadByDaylight.log";
string filenameDestination = pathDestination + "DeadByDaylightCopy.log";
if ( !File.Exists(filenameSource) )
{
Console.WriteLine("File not found: " + filenameSource);
return;
}
else
{
Directory.CreateDirectory(pathDestination);
File.Copy(filenameSource, filenameDestination, true);
}
We created some vars, next we check if the source file exists or exit, then we create a destination folder to copy the file in.
For the user folder, a good practice is to use the assembly data if defined:
static public string UserDataFolderPath
{
get
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
+ Path.DirectorySeparatorChar
+ AssemblyCompany
+ Path.DirectorySeparatorChar
+ AssemblyTitle
+ Path.DirectorySeparatorChar;
Directory.CreateDirectory(path);
return path;
}
}
Having the AssemblyInfo.cs
like:
[assembly: AssemblyTitle("Name of the app")]
[assembly: AssemblyCompany("Any name as a root folder for your projects")]
Now you can write:
string pathDestination = UserDataFolderPath;
Also you can refactor your code and the tons of if
by using a dictionary and a loop on, like:
Dictionary<string, string> RefTable = new Dictionary<string, string>()
{
{ "TW", "Wraith" },
{ "TR", "Trapper" },
...
};
// search for the first killer
foreach ( string line in File.ReadAllLines(filenameDestination) )
{
foreach ( var item in RefTable )
if ( line.Contains("LogCustomization: --> " + item.Key) )
{
killer = item.Value;
break;
}
if (killer != "") break;
}
if ( killer == "" )
Console.WriteLine("ERROR: Make sure you're in the dedicated lobby");
else
Console.WriteLine(killer);
You can of course use the user temp folder instead of UserDataFolderPath
:
How to get temporary folder for current user
solved C# program closes immediately on friend’s PC [closed]