Im using an older program to export some data to a csv. The program uses config.ini file to store some credentials. I've been using an c# console application for test purpose. The problem i have is that my program can't find any config.ini file.
I tried this block of code to receive the location of my project and the ini file but that doens't seem to work well.
string dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Console.WriteLine(dir);
string assname = Assembly.GetExecutingAssembly().Location;
Path.GetDirectoryName(assname);
string filename = Path.Combine(dir, "config.ini");
string ConfigFilePath = Path.Combine(dir, "config.ini");
This is my code for now:
static void CheckLogin()
{
string ConfigFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.ini");
if (File.Exists(ConfigFilePath))
{
Console.WriteLine("Options and Credentials are imported from config.ini ...");
StreamReader IniFile = new StreamReader(ConfigFilePath);
while (!IniFile.EndOfStream)
{
String Line = IniFile.ReadLine();
string[] LineValues = Line.Split(':');
string Value = string.Join(":", LineValues, 1, LineValues.Length - 1);
switch (LineValues[0])
{
case "DevId":
Properties.Settings.Default.Talk2MDevId = Value;
break;
case "APIToken":
Properties.Settings.Default.APIToken = Value;
Console.WriteLine(Value);
break;
case "DeleteData":
Properties.Settings.Default.DeleteData = Value.ToLower().StartsWith("y");
break;
case "CSVFilename":
Properties.Settings.Default.CSVFilename = Value;
break;
case "CSVTitle":
Properties.Settings.Default.CSVTitle = Value;
break;
case "CSVLineFormat":
Properties.Settings.Default.CSVLineFormat = Value;
break;
case "DateTimeFormat":
Properties.Settings.Default.DateTimeFormat = Value;
break;
case "CSVOutputDir":
Properties.Settings.Default.CSVOutputDir = Value;
break;
case "ForceRetrieveAll":
Properties.Settings.Default.TransactionId = "0";
break;
}
}
Properties.Settings.Default.Save();
IniFile.Close();
//Delete the config file not to store the credentials in a all users accessible directory
File.Delete(ConfigFilePath);
}
i've never worked with .ini files so if some can help me out here how to locate the .ini file? it will be very helpfull!
CodePudding user response:
you can list your files in application working directory like this :
var currentDir = Directory.GetCurrentDirectory();
var files =Directory.GetFiles(currentDir);
and you can find your config file with this :
var conf = files.FirstOrDefault(name => name.EndsWith(".ini"));
or
var conf = files.FirstOrDefault(name => name == "config.ini");