• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: how to delete the current user's directory?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to delete the current user's directory?


  • Subject: Re: how to delete the current user's directory?
  • From: Graff <email@hidden>
  • Date: Mon, 1 Sep 2008 13:29:43 -0400

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 (email@hidden)

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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: how to delete the current user's directory?
      • From: "Michael Ash" <email@hidden>
  • Prev by Date: Best 'native' formats for NSImage and NSSound?
  • Next by Date: NSArray as a static
  • Previous by thread: Re: how to delete the current user's directory?
  • Next by thread: Re: how to delete the current user's directory?
  • Index(es):
    • Date
    • Thread