views:

85

answers:

2

Hi everyone,

I have put a config ini file "config.ini" for reading and writing to from my C# program, the thing is, if the user has UAC enabled then for some weird reasons the program doesn't read or write to the file but it managed to create the file but cannot read or write to it.

How can I get this to work.

this file is saved into DOCUMENTSFOLDER\ProductName\config.ini

Ini class file: http://www.sinvise.net/so/Ini.cs

Code Snippet of config.ini creation: http://www.sinvise.net/so/creation.txt

A: 

You should have better luck writing to the path: Environment.SpecialFolder.LocalApplicationData. Your app should have permission to write to that with UAC on or off, or even on machines where the app/user has very limited permissions.

Andy Jacobs
I just tried it with:string iniPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\Product\\config.ini";And it's a no go, it's not reading the data.
Sandeep Bansal
Did the program successfully write the file there (if I wasn't clear, this is not the DOCUMENTSFOLDER, but is a better folder to use for INI files)?
Andy Jacobs
Yes it did with all data, but it's not reading from it, I'm using the Ini Class to get the values from that file. The class source is in my questions.I had already used the DOCUMENTSFOLDER and previously the common applications data folder, still a no go, I believe it could be something with the Ini class
Sandeep Bansal
Also, I forgot if the LocalApplicationData ends with a backslash or not, which may be the problem. In either case, it's better to use Path.Combine which handles both cases. E.g., Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Product\\config.ini");
Andy Jacobs
Can you be more specific about how the reading is failing. E.g., is it throwing an exception (and if so, which exception), or not complaining, but not seeing any data in the file, or claiming the file doesn't exist, etc.?
Andy Jacobs
Well I have if(GetInival(....) == "1") { checkboxName.checked = true; } --- The value is 1 in the config.ini and everything works with UAC off but the checkbox isn't checked in when UAC is on.
Sandeep Bansal
A: 

Quick suggestion before going further, you should check out nini which is a neat INI handler and takes care of the reading/writing the INI files.

Sample usage of the code that reads from the ini file.

using Nini;
using Nini.Config;

namespace niniDemo{
   public class niniDemoClass{
       public bool LoadIni(){
            string configFileName = "demo.ini";
            IniConfigSource configSource = new IniConfigSource(configFileName);

            IConfig demoConfigSection = configSource.Configs["Demo"];
            string demoVal = demoConfigSection.Get("demoVal", string.Empty);
       }
   }

}

Try that and take it from there...

Hope this helps, Best regards, Tom.

tommieb75
I'm recoding my project to nini, hopefully it'll fix my problems.
Sandeep Bansal
Worked Great! Thanks a lot, you saved me a lot of head bashing!
Sandeep Bansal
@Sandeep: Great stuff! Glad to be of help! :)
tommieb75