我有这个代码,没关系。
NSString *pathName = [@"/Users/" stringByAppendingString:NSUserName()];
pathName = [pathName stringByAppendingString:@"/Library/Application Support/AddressBook/Configuration.plist"];
[[NSFileManager defaultManager] removeItemAtPath:pathName error:nil];
我想使用这个目录,但是有一个 cbk4yc7r.default 的文件夹。 cbk4yc7r为每个用户更改。如何修改代码以使用此目录?
"/Library/Application Support/Firefox/Profiles/cbk4yc7r.default/places.sqlite"
我尝试了“/ Library / Application Support / Firefox / Profiles / * .default / places.sqlite”但在objective-c中不起作用。
你能帮帮我吗? 感谢答案 0 :(得分:0)
假设您知道每个用户的cbk4yc7是什么,您可以这样做:
[NSString stringWithFormat:@"/Library/Application Support/Firefox/Profiles/%@.default/places.sqlite", user.dirString];
其中user.dirstring
相当于每个用户的cbk4yc7r。
答案 1 :(得分:0)
首先你应该使用
NSString *pathName = [@"~/Library/Application Support/Firefox/Profiles/" stringByExpandingTildeInPath];
获取正确的父目录,其次,该目录中的配置文件由〜/ Library / Application Support / Firefox / profiles.ini文件定义,例如: “Path = Profiles / cbk4yc7r.default”,因此您需要解析profiles.ini或使用
[[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathName error:&error];
答案 2 :(得分:0)
最好的解决方案是使用一些Firefox API(如果存在)来确定配置文件ID。但我不知道Firefox内部是否足以知道这是否可行。我做知道单个用户可以拥有多个配置文件,这是值得考虑的事情。
此外,您(以及坦白地说,其他响应者)正在对目录结构进行大量假设,但这些假设并不能保证。您还假设一切正常,当然不保证磁盘操作。考虑到这些因素,我提交了以下内容,它充分利用了Foundation提供的抽象。它还在适当的地方使用了更现代的NSURL:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *appDirErr;
NSURL *appSupportDir = [fileManager URLForDirectory:NSApplicationSupportDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:NO
error:&appDirErr];
if (appSupportDir) {
NSURL *firefoxDir = [appSupportDir URLByAppendingPathComponent:@"Firefox/Profiles"
isDirectory:YES];
NSError *profileErr;
NSArray *profileURLs = [fileManager contentsOfDirectoryAtURL:firefoxDir
includingPropertiesForKeys:nil
options:0
error:&profileErr];
if (profileURLs) {
for (NSURL *currentProfileURL in profileURLs) {
NSURL *removalURL = [currentProfileURL URLByAppendingPathComponent:@"places.sqlite"
isDirectory:NO];
NSError *removalErr;
if (! [fileManager removeItemAtURL:removalURL error:&removalErr]) {
NSLog(@"Error! %@", [removalErr localizedDescription]);
}
}
}
else {
NSLog(@"Error! %@", [profileErr localizedDescription]);
}
}
else {
NSLog(@"Error! %@", [appDirErr localizedDescription]);
}