On Mon, 1 Sep 2008 18:46:41 +0800, XiaoGang Li wrote:

hi, list, I have an uninstaller application , which is used to delete the files installed; and also i have a file which located in the current user's directory, like the ~/Library/LaunchAgents/com.****.plist. and how
can i delete this kind of file, i have tried the
AuthorizationExecuteWithPrivileges
API, it seems not workable, do i give a wrong flag value or something else?
here is my code piece, any comments will be appreciated.


It would probably be a lot easier to use the NSFileManager method removeItemAtPath:error:

        NSFileManager *defaultManager = [NSFileManager defaultManager];
        NSArray *searchPaths;
        searchPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory,
                                                                                
                          NSUserDomainMask,
                                                                                
                          TRUE);
        NSString *libraryPath = [searchPaths lastObject];
        NSString *itemDirectory;
        NSString *itemPath;
        
        itemDirectory = [libraryPath stringByAppendingPathComponent:
                                         @"LaunchAgents"];
        itemPath = [itemDirectory stringByAppendingPathComponent:
                                @"com.hp.printerAgent.plist"];
        
        if([defaultManager fileExistsAtPath:itemPath]){
                NSError *theError;
                [defaultManager removeItemAtPath:itemPath error:&theError];
        }

Note that I avoided hard-coded paths by using NSSearchPathForDirectoriesInDomains. This is important because hard- coded paths can break in many ways, such as when the user is on a non- english system. By using the proper function for finding standardized directories you avoid as much hard-coded paths as possible.

In general I try to avoid using terminal commands in an application when there is a Cocoa or Carbon method that does the same thing. By using the library methods you are likely to have a solution which is more portable and less fragile than by using terminal commands.

- Ken Bruno
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to