[Mono-list] reading and writing plists
GARRISON, TRAVIS J.
garrisot at otc.edu
Tue Mar 22 13:21:51 EDT 2011
Thanks to Geoff Norton in pointing me to NSDictonary and Baker, GJ for the example phonebook program, I was finally able to read a plist from OSX. It may not be pretty but I will port the code below in case anyone else needs help. I wasn't totally sure about the NSApplication.Main. I commented it out to keep the program from opening up a window. This is strictly a console app to help us manage our macs better. What is the correct way to do this?
Travis
static string GetPlistValue(string filepath, string key)
{
//Clean up the pool errors
using (var ns = new NSAutoreleasePool())
{
//Connect to Apple framework
NSApplication.Init ();
//Commented out cause this is a console app
//NSApplication.Main (args);
//Create a new dictonary to hold our plist
NSDictionary plist = new NSDictionary();
//Check to see if the plist exists
if (NSFileManager.DefaultManager.FileExists(filepath))
{
//Check to see if the plist has anything in it
plist = NSDictionary.FromFile(filepath);
if (plist != null)
{
//return the value for our key
NSString valueKey = new NSString(key);
return plist.ValueForKey(valueKey).ToString();
}else{
//The plist is empty
return "Empty plist";
}
}
//Can't find the plist, correct path?
return "No plist found";
}
}
From: mono-osx-bounces at lists.ximian.com [mailto:mono-osx-bounces at lists.ximian.com] On Behalf Of GARRISON, TRAVIS J.
Sent: Thursday, March 17, 2011 3:09 PM
To: mono-osx at lists.ximian.com
Subject: [Mono-osx] reading and writing plists
We are looking for examples on how to read and write to the native plists included with Mac OSX. Most of the plists are in the binary format, which we can convert to the xml format if needed. We are trying to code a few tools to help us manage our macs better.
Thanks
Travis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20110322/24bb2297/attachment.html
More information about the Mono-list
mailing list