Reputation: 103
I have made a program in C# that reads text files (.nfo files) when I open the nfo file. (Associated .nfo files with my exe file).
The program checks for a config file, and if it's missing, it creates a new one.
This works fine if I run the exe file manualy, and then open an nfo file from inside the program. When I double click an nfo file, the program starts, but allso writes a config file in the same folder as the nfo file.
I want the program to ONLY check for config file in the same folder as the exe file. I want the exe file to be folder independent too.
Here's the code for the config check / write:
string configFil = "XnfoReader.exe.Config";
StreamWriter sw = null;
string configText = @"<?xml version=""1.0"" encoding=""utf-8"" ?>"
if (!File.Exists(configFil))
{
FileStream fs = File.Open(configFil, FileMode.CreateNew, FileAccess.Write);
sw = new StreamWriter(fs, System.Text.Encoding.UTF8);
sw.WriteLine(configText);
sw.Close();
sw = null;
}
Am I making sense here?
Upvotes: 0
Views: 441
Reputation: 6494
The solution is pretty simple -lucky to you- but not really obvious. Actually you only put the name of your config file, so the application add the current path to get the full path to the config file.
That is your problem, because as you previously open an nfo file in an other location, the current path became the path to the nfo file.
So the solution is to put directly the full path to your config path, to do that you can use :
string fullPathToConfigFile = Assembly.GetEntryAssembly().Location + ".config"
Upvotes: 1