2

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.

Jac Mos
  • 7,746
  • 4
  • 15
  • 21
lukassz
  • 3,135
  • 7
  • 32
  • 72
  • What "the variable"? Do you mean that `Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData` returns an empty string when your application is started through Run? – CodeCaster Jul 16 '15 at 08:25
  • Variable `configPath` is empty when program start with system. – lukassz Jul 16 '15 at 08:26
  • When / where / what event is your code firing on? See http://stackoverflow.com/questions/14280202/force-application-launch-on-startup – Paul Zahra Jul 16 '15 at 08:26
  • 1
    AppData is a user specific folder. Check that your application runs under user credentials and not local system. – Fabjan Jul 16 '15 at 08:26
  • "if I turn off the application and manually" = "if I turn on the application manually" ? – Paul Zahra Jul 16 '15 at 08:27
  • 2
    @lukassz that is impossible, you add `\settings.jsn` to it. It can't be empty. What is _really_ happening and how do you notice that? Can you show the code around this line and explain properly what exactly happens? – CodeCaster Jul 16 '15 at 08:27
  • And yet, the variable does not show any value. It was only when I run the program manually. – lukassz Jul 16 '15 at 08:29
  • 1
    Variables don't show anything. How do you obtain the variable's value? How do you determine that it is empty? Are you sure that the code you show even runs? – CodeCaster Jul 16 '15 at 08:30
  • 1
    @lukassz you sure that application starts at all ? – Fabjan Jul 16 '15 at 08:30
  • @CodeCaster I just prints its value in textbox, Fabjan yes I'm sure. – lukassz Jul 16 '15 at 08:40
  • You have a `try` and removed the relevant `catch`. Show all code. – CodeCaster Jul 16 '15 at 08:41
  • Look at your code. The statement `string configPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\settings.jsn"` **cannot** let `configPath` be empty afterwards if no exception occurs. There's something you're not telling or something you're not showing. We can't debug this for you. – CodeCaster Jul 16 '15 at 08:45
  • Ok, my mistake, I corrected the post. – lukassz Jul 16 '15 at 08:49
  • You didn't correct it, now you have the same line twice. If I didn't make this clear: **you** need to go debugging. Your problem description is unclear at best and at least incomplete, we can't analyze this for you. – CodeCaster Jul 16 '15 at 08:59
  • How do I debug an application as it runs with the system? – lukassz Jul 16 '15 at 09:00
  • 1
    Debugging does not necessarily mean _"By attaching the debugger"_, you can also debug offline by inspecting a log file, for example. Or you can put a `Debugger.Break()` in your code, or put a `Console.ReadKey()`, attach the debugger yourself and then continue. – CodeCaster Jul 16 '15 at 09:05

1 Answers1

1

I found this page: https://msdn.microsoft.com/en-us/library/system.environment.specialfolder(v=vs.110).aspx

which says

ApplicationData: The directory that serves as a common repository for application-specific data for the current roaming user. A roaming user works on more than one computer on a network. A roaming user's profile is kept on a server on the network and is loaded onto a system when the user logs on.

My guess is since no user is logged in when your application starts at system startup it cannot get this folder. That would also explain why you cannot reproduce the problem because you're already logged in.

Maybe trying a different system folder would solve it.

Volkan Paksoy
  • 6,727
  • 5
  • 29
  • 40