开发者

Why is WritePrivateProfileString returning 0?

In my C# application, I wish to update the values in an INI file. I used the interop services and wish to use the function WritePrivateProfileString. So I imported the DLL like this:

[DllImport("KERNEL32.DLL", EntryPoint = "WritePrivateProfileStringW",
           SetLastError = true,
           CharSet = CharSet.Unicode, ExactSpelling = true,
           CallingConvention = CallingConvention.StdCall)]
private static extern int WritePrivateProfileString(
            string lpAppName,
            string lpKeyName,
            string lpString,
   开发者_开发百科         string lpFilename);`

And used it in my function like this:

int result = WritePrivateProfileString(category, key, value, iniFile);

But the result is 0 and the INI file is not updated. How can I check the error?


The Marshal.GetLastWin32Error should help you.


Call the .NET equivalents to Win32 API functions GetLastError and FormatMessage, and the system will tell you why it's not letting you do this.

Cody is probably right in guessing that it is related to UAC, but without knowledge of where you are trying to write this file, we can only guess.

Update

As has been explained to me in the comments, Marshal.GetLastWin32Error is the .NET way to get hold of the error code rather than calling GetLastError directly. Calling GetLastError directly will return the error code of the latest call to the Win32 API, which is likely to have been from a .NET runtime call to Win32 rather than your P/Invoke.


Application data/parameter files should be saved to an application-specific folder within the system's LocalApplicationData special folder either for a specific user or for all users.

Your app will have proper permissions to write here. This can be a per-user special folder, so ini settings will be unique for each user.

You can retrieve the LocalApplicationData special folder using:

string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData);

Create a folder here for your application, and store your ini file(s) within it.

On the file system the LocalApplicationData special folder by default maps to:

(XP/2k3) C:\Documents and Settings\user_account\Local Settings\Application Data\

(Vista/2k8/7/8) C:\Users\user_account\AppData\Local\

Comments regarding "ini files are not recommended" are imho wrong.

If you don't like the ini file format and API for PrivateProfile strings, then use XML files for your app settings, or roll your own settings data file format.

Just keep your application data in files in the LAD special folder and not in the registry.

I recommend that all third-party software apps use the LAD folder for per user initialization data/parameters and not the Registry. The system registry tends to become filled with left over junk as many software applications do not clean up and remove their registry keys when they perform upgrades or uninstalls.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜