How to handle a corrupted plist programatically

I access my app’s plist on launch to read/write a preference setting. I’ve had several occasions where a user was unable to launch the app due to corrupted XML in the plist. It tell them where the file is and to remove it, which fixes the problem. But I wonder if I can do this programatically. That is, if I put the plist read in a try block and get an exception, can I have my app delete the plist file and replace it with the info.plist file( in the app’s bundle), renaming it com.mycompany.myapp.plist? Or will that cause problems?

Are you reading it manually ?

Yes, like this, using a shell to convert the plist to XML

sh.execute "plutil -convert xml1 " + f.ShellPath //converts binary plist to xml so we can read it.
ti = f.OpenAsTextFile
s = ti.ReadAll

then

xdoc = new XmlDocument(s)

But I’m not asking why the plist is sometimes corrupted, that can happen with any app (and it’s very rare with mine, but with thousands of users I do get the occasional report). I’m wondering if I can put the

new XMLDocument(s)

in a try-catch block and, if an exception is thrown, replace the problematic plist with a new copy of the info.plist in the app bundle.

Yes you can put the XML doc reading in a try catch
But you shouldn’t be doing this this way anyways
Use CFPreferences directly and it takes care of all that for you

Thanks. Do you mean use CFPreferences via Declares (i.e. using MacOSLib)?

Yes
It’s not so bad and because you use the official API it will already handle binary & text plists

Thank you for the help, I’ll give it a shot.

You want NSBundle for accessing the info.plist file within your application bundle.

Oh and don’t write to it, you’ll break the application code signature.

I wouldn’t write to info.plist. The most I’d do would be to make a copy to replace the app .plist.