My application starts with the system, I did it by:
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rkApp.SetValue("Monitor", BaseDir+"\\Monitor.exe");
Then I want to load a file that is located under the path:
string configPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\settings.jsn";
The problem is that the variable configPath
on startup is empty, if I turn off the application and manually, the path appears not know why it does not work when the application starts automatically.
UPDATE: This is my Class
public partial class MainWindow : Window
{
class MySettings : AppSettings<MySettings>
{
public string filePath = null;
public string interval = "0";
}
public class AppSettings<T> where T : new()
{
private static readonly string DEFAULT_FILENAME = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\settings.jsn";
public void Save(string fileName = null)
{
File.WriteAllText(DEFAULT_FILENAME, (new JavaScriptSerializer()).Serialize(this));
}
public static void Save(T pSettings, string fileName = null)
{
File.WriteAllText(DEFAULT_FILENAME, (new JavaScriptSerializer()).Serialize(pSettings));
}
public static T Load(string fileName = null)
{
T t = new T();
if (File.Exists(DEFAULT_FILENAME))
t = (new JavaScriptSerializer()).Deserialize<T>(File.ReadAllText(DEFAULT_FILENAME));
return t;
}
}
MySettings settings = MySettings.Load();
public MainWindow()
{
try
{
RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
rkApp.SetValue("Monitor", BaseDir+"\\Monitor.exe");
string configPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\settings.jsn";
string configPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\settings.jsn"
statusLabel.Content = configPath;
Console.WriteLine("Path: " + configPath); //is empty
InitializeComponent();
int inter = settings.interval; // is empty
}catch(Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
}
}
This is my code to read settings. Settings are read when uruchomy program manually if you start automatically each time the system are not loaded, and the path where the file is empty.