How to access a command line application's bundle in C
I am on a Mac, programming with Xcode. I have a command line application that will read/write to files in it's bundle and I was wondering how to access these files.
Thanks, Mr. Man
EDIT: Would it work开发者_开发问答 better if I just made a folder in the user's library folder?
Assuming you actually have a bundle, you can use the CoreFoundation CFBundle API. You can use CFBundleGetMainBundle()
to find your app's bundle and the CFBundleCopy*URL()
functions to get the URL of the bundle and its various components.
However, applications should not write to their bundles. There are a number of things that can go wrong with that, such as the current user not having write permission or a signed bundle getting broken. Generally, your application should put files it creates that aren't meant to be user-editable* in the ~/Library/Application Support folder. The normal place for user documents is the Documents folder.
(* To clarify "user editable," I mean files that the user is normally expected to navigate to and open himself rather than simply being a private store for data that's solely accessed through your app's interface. So for example TextEdit documents go in Documents while Stickies notes go in Application Support.)
Not sure if I understand you question, but if you want the path of one of your resources you can use:
NSString *path = (NSString*)[[NSBundle mainBundle] pathForResource:@"sample" ofType:@"png"];
If you just want the app directory you could use:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [[paths objectAtIndex:0] retain];
or
NSArray *resPaths = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES);
NSString *resourceDirectory = [[resPaths objectAtIndex:0] retain];
I'm not at my Mac right now, so I'm just pasting from our framework.
精彩评论