从文档目录中删除指定的文件
我想从我的应用程序文档目录中删除一个图像。 我写的删除图像的代码是:
-(void)removeImage:(NSString *)fileName { fileManager = [NSFileManager defaultManager]; paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); documentsPath = [paths objectAtIndex:0]; filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@", fileName]]; [fileManager removeItemAtPath:filePath error:NULL]; UIAlertView *removeSuccessFulAlert=[[UIAlertView alloc]initWithTitle:@"Congratulation:" message:@"Successfully removed" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil]; [removeSuccessFulAlert show]; }
它的部分工作。 此代码从目录中删除文件,但是当我检查目录中的内容时,它仍然在那里显示图像名称。 我想从目录中彻底删除该文件。 我应该在代码中做些什么改变? 谢谢
我检查了你的代码。 它为我工作。 使用下面的修改后的代码检查您所得到的任何错误
- (void)removeImage:(NSString *)filename { NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *filePath = [documentsPath stringByAppendingPathComponent:filename]; NSError *error; BOOL success = [fileManager removeItemAtPath:filePath error:&error]; if (success) { UIAlertView *removedSuccessFullyAlert = [[UIAlertView alloc] initWithTitle:@"Congratulations:" message:@"Successfully removed" delegate:self cancelButtonTitle:@"Close" otherButtonTitles:nil]; [removedSuccessFullyAlert show]; } else { NSLog(@"Could not delete file -:%@ ",[error localizedDescription]); } }
Swift 3.0:
func removeImage(itemName:String, fileExtension: String) { let fileManager = FileManager.default let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true) guard let dirPath = paths.first else { return } let filePath = "\(dirPath)/\(itemName).\(fileExtension)" do { try fileManager.removeItem(atPath: filePath) } catch let error as NSError { print(error.debugDescription) }}
感谢@Anil Varghese,我在swift 2.0中编写了非常类似的代码:
static func removeImage(itemName:String, fileExtension: String) { let fileManager = NSFileManager.defaultManager() let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true) guard let dirPath = paths.first else { return } let filePath = "\(dirPath)/\(itemName).\(fileExtension)" do { try fileManager.removeItemAtPath(filePath) } catch let error as NSError { print(error.debugDescription) } }
Swift 2.0:
func removeOldFileIfExist() { let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true) if paths.count > 0 { let dirPath = paths[0] let fileName = "someFileName" let filePath = NSString(format:"%@/%@.png", dirPath, fileName) as String if NSFileManager.defaultManager().fileExistsAtPath(filePath) { do { try NSFileManager.defaultManager().removeItemAtPath(filePath) print("old image has been removed") } catch { print("an error during a removing") } } } }
将错误设置为NULL,而不是将其设置为
NSError *error; [fileManager removeItemAtPath:filePath error:&error]; if (error){ NSLog(@"%@", error); }
这会告诉你它是否真的删除了这个文件
NSError *error; [[NSFileManager defaultManager] removeItemAtPath:new_file_path_str error:&error]; if (error){ NSLog(@"%@", error); }
我想从文档目录中删除我的sqlite数据库。我通过下面的答案成功地删除了sqlite数据库
NSString *strFileName = @"sqlite"; NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSArray *contents = [fileManager contentsOfDirectoryAtPath:documentsDirectory error:NULL]; NSEnumerator *enumerator = [contents objectEnumerator]; NSString *filename; while ((filename = [enumerator nextObject])) { NSLog(@"The file name is - %@",[filename pathExtension]); if ([[filename pathExtension] isEqualToString:strFileName]) { [fileManager removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:filename] error:NULL]; NSLog(@"The sqlite is deleted successfully"); } }
你可以用NSFileManager.defaultManager()来保护你的文件删除。isDeletableFileAtPath(PathName)现在你必须使用do {} catch {},因为旧的错误方法不再有效。 isDeletableFileAtPath()不是“抛出”(即“公共func removeItemAtPath(path:string)抛出”),所以它不需要做… catch
let killFile = NSFileManager.defaultManager() if (killFile.isDeletableFileAtPath(PathName)){ do { try killFile.removeItemAtPath(arrayDictionaryFilePath) } catch let error as NSError { error.description } }