none
Delete User Application Settings RRS feed

  • Question

  • Probably not the right place to ask, but I have no idea where a better place would be.

     

    So I read about and experienced an issue where the user.config XML file used in User Application settings to save persistent data is prone to corruption. (corruption maybe a bit too strong a word for this, if the XML slightly different from what the parser expects, it will fail)

    I looked into saving that data into registry, and learned that it had it's own share of problems that was even more intimidating, so I want to try catching the exception of corrupt user.config and handle that by deleting the user.config file.

     

    I'm looking for pointers regarding how to get the path to the file within a client's filesystem!

    Thursday, April 7, 2011 11:42 PM

Answers

  • Hello Kit,

    You can use something like this to get the location:

     

    private void ThisAddIn_Startup(object sender, System.EventArgs

    e) {

     

    MessageBox.Show("Location: " + Assembly.GetExecutingAssembly().Location + "\nCodebase: " + Assembly

    .GetExecutingAssembly().CodeBase);

    }

    personally I gave up storing settings in app.config - always had problems when upgrading the AddIn or Outlook. Especially when deployed using ClickOnce.

    I'm using the SQL CompactEdition to store data locally.
    I store the DB or other data in the user AppData-Path.

    This is how you can get the place where a User can store data:

    ///<summary>
    /// Returns the path to the local app data folder.
    /// Works for every windows version.
    /// The only safe place where the user can write logs and app settings.
    ///</summary>
    ///<returns>The path as string.</returns>
    public static string ReadLocalAppDataPath() {
    string localAppPath = string.Empty;
    try {
    using (RegistryKey regKey =
    Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", false)) {
    localAppPath = regKey.GetValue("Local AppData").ToString();
    regKey.Close();
    }
    } catch (System.Exception ex) {
    // log the error always
    Trace.TraceError("{0}: [class]:{1} [method]:{2}\n[message]:{3}\n[Stack]:\n{4}",
    DateTime.Now, // when was the error happened
    MethodInfo.GetCurrentMethod().DeclaringType.Name, // the class name
    MethodInfo.GetCurrentMethod().Name, // the method name
    ex.Message, // the error message
    ex.StackTrace // the stack trace information
     );
    }
    return localAppPath;
    }
     
    Greets - Helmut

    Helmut Obertanner [http://www.x4u.de] [http://www.outlooksharp.de]
    • Marked as answer by Kit C Sunday, April 10, 2011 10:11 PM
    Friday, April 8, 2011 5:39 AM
    Answerer

All replies

  • Hello Kit,

    You can use something like this to get the location:

     

    private void ThisAddIn_Startup(object sender, System.EventArgs

    e) {

     

    MessageBox.Show("Location: " + Assembly.GetExecutingAssembly().Location + "\nCodebase: " + Assembly

    .GetExecutingAssembly().CodeBase);

    }

    personally I gave up storing settings in app.config - always had problems when upgrading the AddIn or Outlook. Especially when deployed using ClickOnce.

    I'm using the SQL CompactEdition to store data locally.
    I store the DB or other data in the user AppData-Path.

    This is how you can get the place where a User can store data:

    ///<summary>
    /// Returns the path to the local app data folder.
    /// Works for every windows version.
    /// The only safe place where the user can write logs and app settings.
    ///</summary>
    ///<returns>The path as string.</returns>
    public static string ReadLocalAppDataPath() {
    string localAppPath = string.Empty;
    try {
    using (RegistryKey regKey =
    Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders", false)) {
    localAppPath = regKey.GetValue("Local AppData").ToString();
    regKey.Close();
    }
    } catch (System.Exception ex) {
    // log the error always
    Trace.TraceError("{0}: [class]:{1} [method]:{2}\n[message]:{3}\n[Stack]:\n{4}",
    DateTime.Now, // when was the error happened
    MethodInfo.GetCurrentMethod().DeclaringType.Name, // the class name
    MethodInfo.GetCurrentMethod().Name, // the method name
    ex.Message, // the error message
    ex.StackTrace // the stack trace information
     );
    }
    return localAppPath;
    }
     
    Greets - Helmut

    Helmut Obertanner [http://www.x4u.de] [http://www.outlooksharp.de]
    • Marked as answer by Kit C Sunday, April 10, 2011 10:11 PM
    Friday, April 8, 2011 5:39 AM
    Answerer
  • Thanks for your answer! :)

     

    I'm not familiar with file-writing libraries, if you could toss in an example like how I would create a settings.inf in Users\(User)\AppData\(Addin)\ with just one short string, that'd be great.

     

    Wouldn't that registry setting method be version-specific? I see a 'CurrentVersion' thing in there.

    Sunday, April 10, 2011 10:11 PM