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!

4

2 回答 2

0

如果我理解正确,您需要检查文件是打开还是关闭,我认为这是在 File.Create 之后关闭文件

我会使用该类来检查文件是否打开并尝试关闭方法。

于 2013-06-13T17:01:13.333 回答
0

解决它。由于某种原因,外部函数没有关闭文件。我在上面的类中扩展了 IDisposable 并在阅读完所有设置后手动处理它。我真的很震惊,这些功能自己没有做到这一点。没关系,都整理好了。为帮助和提示干杯!

于 2013-06-16T08:10:09.547 回答