回答
我这样做,通过以下方式:
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/MyFolder"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
我不喜欢 “[路径objectAtIndex:0]”,因为如果苹果增加了一个新的文件夹以 “A” 开始, “B” 奥得“C “,”文档“ - 文件夹不是目录中的第一个文件夹。
更好:
NSString *dataPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/MyFolder"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder
查看Tim的回答。使用NSSearchPathForDirectoryiesInDomains是检索用户Documents目录的标准方式。 – 2011-03-18 01:11:12
如果Apple决定将文档目录重命名为“我的文档”(或类似文档),那么您的方法也无效。 – Mehlyfication 2014-12-05 08:01:58
我没有足够的声誉对曼尼的回答发表评论,但[路径objectAtIndex:0]是获取应用程序的文件目录的标准方式
因为NSSearchPathForDirectoriesInDomains 功能原本是为 Mac OS X,其中可能有比这些目录中的每一个更多的 , 它比单个路径返回路径数组而不是 。在iOS中, 生成的数组应包含目录的单一路径 。清单 3-1显示了这个 函数的典型用法。
清单3-1获取路径 应用程序的Documents目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
我认为检索文档目录路径的最简单方法是:'NSString * path = [@“〜/ Documents”stringByExpandingTildeInPath];' – 2014-07-21 12:15:38
这工作对我很好,
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *appSupportDir = [fm URLsForDirectory:NSDocumentsDirectory inDomains:NSUserDomainMask];
NSURL* dirPath = [[appSupportDir objectAtIndex:0] URLByAppendingPathComponent:@"YourFolderName"];
NSError* theError = nil; //error setting
if (![fm createDirectoryAtURL:dirPath withIntermediateDirectories:YES
attributes:nil error:&theError])
{
NSLog(@"not created");
}
它应该是NSDocumentDirectory而不是NSDocumentsDirectory – 2017-09-30 05:34:11
下面的代码创建目录可能会有帮助:
-(void) createDirectory : (NSString *) dirName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Fetch path for document directory
dataPath = (NSMutableString *)[documentsDirectory stringByAppendingPathComponent:dirName];
NSError *error;
if (![[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]) {
NSLog(@"Couldn't create directory error: %@", error);
}
else {
NSLog(@"directory created!");
}
NSLog(@"dataPath : %@ ",dataPath); // Path of folder created
}
用法:
[self createDirectory:@"MyFolder"];
结果:
目录中创建!
数据路径:在/ var /移动/应用/ BD4B5566-1F11-4723-B54C-F1D0B23CBC /文档/ MyFolder中
夫特1.2和iOS 8
创建定制目录(名称= “MyCustomData” )只在目录不存在的情况下。
// path to documents directory
let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String
// create the custom folder path
let myCustomDataDirectoryPath = documentDirectoryPath.stringByAppendingPathComponent("/MyCustomData")
// check if directory does not exist
if NSFileManager.defaultManager().fileExistsAtPath(myCustomDataDirectoryPath) == false {
// create the directory
var createDirectoryError: NSError? = nil
NSFileManager.defaultManager().createDirectoryAtPath(myCustomDataDirectoryPath, withIntermediateDirectories: false, attributes: nil, error: &createDirectoryError)
// handle the error, you may call an exception
if createDirectoryError != nil {
println("Handle directory creation error...")
}
}
的夫特2溶液:
let documentDirectoryPath: String = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first!
if !NSFileManager.defaultManager().fileExistsAtPath(documentDirectoryPath) {
do {
try NSFileManager.defaultManager().createDirectoryAtPath(documentDirectoryPath, withIntermediateDirectories: false, attributes: nil)
} catch let createDirectoryError as NSError {
print("Error with creating directory at path: \(createDirectoryError.localizedDescription)")
}
}
夫特3解决方案:
private func createImagesFolder() {
// path to documents directory
let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
if let documentDirectoryPath = documentDirectoryPath {
// create the custom folder path
let imagesDirectoryPath = documentDirectoryPath.appending("/images")
let fileManager = FileManager.default
if !fileManager.fileExists(atPath: imagesDirectoryPath) {
do {
try fileManager.createDirectory(atPath: imagesDirectoryPath,
withIntermediateDirectories: false,
attributes: nil)
} catch {
print("Error creating images folder in documents dir: \(error)")
}
}
}
}
夫特3.0
var error: Error?
var paths: [Any] = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
var documentsDirectory: String = paths[0] as? String ?? ""
// Get documents folder
var dataPath: String = URL(fileURLWithPath: documentsDirectory).appendingPathComponent("/MyFolder").absoluteString
if !FileManager.default.fileExists(atPath: dataPath) {
try? FileManager.default.createDirectory(atPath: dataPath, withIntermediateDirectories: false, attributes: nil)
}
第
- 1. 创建文件到应用程序文档文件夹
- 2. iOS - 访问应用程序创建的文件和文件夹
- 3. PhoneGap - 如何在iOS文档文件夹中创建新文件
- 4. iPad应用程序文档文件夹
- 5. 在iOS中创建文件夹OneDrive中的文件夹
- 6. System.UnauthorizedAccessException在应用程序文件夹中创建文件时
- 7. 获取存储在应用程序文档文件夹中的文件内容
- 8. 文档文件夹中的文件应用程序的大小iOS
- 9. Sencha'a应用程序文件夹内的自定义文件夹
- 10. 在iOS的文档文件夹中创建多个目录
- 11. 在文档文件夹中创建一个子文件夹/文件
- 12. 在Google Drive中创建文件夹内的文件夹android
- 13. 的NSFileManager应用程序文件夹 - IOS
- 14. 在指定文件夹中创建文件夹在新文件夹中创建子文件夹
- 15. 无法创建文件夹,然后在文件夹内创建文件
- 16. Onedrive专用应用程序文件夹 - 创建一个新文件夹
- 17. 无法在文档文件夹中创建plist文件
- 18. ofstream不会在文档文件夹中创建文件
- 19. 访问包括应用程序文件夹中的文件夹。
- 20. 在Windows 8应用程序中显示文档库文件夹
- 21. 使用程序创建文件夹
- 22. iPhone中的应用程序的文档文件夹
- 23. iPhone应用程序的文档文件夹中的透明UIImage
- 24. 在应用程序安装过程中创建文件夹android
- 25. 使用支持iCloud的文件夹中的文档删除iOS应用程序
- 26. 课程文件文件夹未创建
- 27. 安装应用程序后在文档文件夹中的示例文件
- 28. 在应用程序的保存箱中创建文件夹
- 29. 文档文件夹中存在文件
- 30. iPad的文件夹内创建子文件夹
anks的回复, 它的作品,但它创建文件夹“MyFolder”旁边的应用程序的文档文件夹..不在里面。 不知道为什么 – 2009-11-19 12:22:43
对不起 - 有一个错字 - 必须在文档路径中追加@“/ MyFolder”,而不是@“MyFolder”。 – Vladimir 2009-11-19 12:26:08
是的,我发现,感谢您的帮助:) – 2009-11-19 12:28:41