I have Winforms application that must create write to certain configuration file during usage. Once i use debug mode this file can be create and written to it but once i create Setup Project and actually install the application i cannot access with following exception.
Configuration file is located the same directory as program ( at Program Files )
Code i’m using to read/write is.
public static string[] GetDefaultConfigFile(string path)
{
string[] res = {};
if (File.Exists(GetInternalFileName(path)))
{
using (StreamReader tr = new StreamReader(GetInternalFileName(path)))
{
res = tr.ReadToEnd().Split(';');
}
}
return res;
}
public static void SaveDefaultConfigFile(string fileName, string path)
{
using (var tw = new StreamWriter(GetInternalFileName(path)))
{
tw.Write(fileName);
tw.Close();
}
}
private static string GetInternalFileName(string path)
{
return path + "\\setup.config";
}
Maybe the account used to run this code doesn’t have sufficient privileges to write to the specified folder (
Program Files). If you are running under Windows 7 or Vista standard users are not authorized to write to this folder. In this case you could use the user specific folderc:\users\usernameto store configuration settings.Also I would simplify: