1

I've got this class:

    public class CIni
{
    public string path;

    [DllImport("kernel32")]
    private static extern long WritePrivateProfileString(string section,
        string key, string val, string filePath);
    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section,
        string key, string def, StringBuilder retVal, int size, string filePath);

    public CIni(string INIPath)
    {
        path = INIPath;
    }

    public void IniWriteValue(string Section, string Key, string Value)
    {
        WritePrivateProfileString(Section, Key, Value, this.path);
    }

    public string IniReadValue(string Section, string Key)
    {
        StringBuilder temp = new StringBuilder(255);
        int i = GetPrivateProfileString(Section, Key, "", temp,
            255, this.path);

        // finally return the value found
        return temp.ToString();
    }
}

Which I use to read out settings from a .ini file. I've also got an auto-updater which I wrote. Everything works fine until the auto-updater attempts to over write the .ini file (with new settings etc). I can't figure out why and the only thing I can think of is that for some reason the dll import keeps the file open?

Seeing as I've not actually got a handle to the file, how would I check to see if it's open/closed? Or does the file get closed automatically after the read/write? In which case, what can I do? Possibly dispose the object?

Thanks in advance!

laminatefish
  • 5,197
  • 5
  • 38
  • 70

2 Answers2

0

If I am understanding it correctly you need to check to see if the file is open or closed, I think this was answered here Closing a file after File.Create

I would use that class to check if the file is open and try the close method regardless.

Community
  • 1
  • 1
NoviceDeveloper
  • 1,270
  • 3
  • 15
  • 41
  • I'll have a look at that. The trouble is, the ini file is already created and I never actually open it myself. I'm using the kernel32.dll functions to do this for me, as and when I need to read/write values. I'm just not sure if the file is ever released. I'm assuming it is as it's a core os dll, but I can't find anything else that would explain why I can't overwrite the file, when I can overwrite everything else. – laminatefish Jun 13 '13 at 17:05
0

Fixed it. For some reason the external functions weren't closing the file. I extended IDisposable in the above class and manually dispose of it once I'm done reading all of the settings. I'm really quite shocked that these functions didn't do this themselves. Nevermind, all sorted. Cheers for the help and tips!

laminatefish
  • 5,197
  • 5
  • 38
  • 70