2011年5月10日 星期二

foundation Framework-檔案存取 CH16-3 NSFileManager用於目錄的方法

//

// main.m

// CH16-3 NSFileManager - Directory Path

//

// Created by jason on 2011/5/9.

// Copyright 2011 __MyCompanyName__. All rights reserved.

//


#import

#import

#import

#import


//#import


int main(int argc, char *argv[])

{

NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];

NSString *dirName= @"testdir1";

NSString *path;

NSFileManager *fm;

fm=[NSFileManager defaultManager];

//Get Current directory 取得目前目錄

path=[fm currentDirectoryPath];

NSLog(@"Current directory path is %@", path);

//Create a new directory 於目錄中建立新的目錄

if([fm createDirectoryAtPath: dirName attributes: nil]==NO){

NSLog(@"Could not create directory!");

return 1;

}

// Rename the new directory 更改目錄名,也可以用來移動整個目錄包含其全部內容

if ([fm movePath:dirName toPath: @"Newdir" handler: nil]==NO){

NSLog(@"Directory rename failed!");

return 2;

}

// Change directory into the new directory 將剛建立的目錄newdir設為目前的目錄 (current directory

if([fm changeCurrentDirectoryPath:@"newdir"]==NO){

NSLog(@"Change directory failed!");

return 3;

}

//Now get and display current working directory 驗證看看是不是改過來了

path = [fm currentDirectoryPath];

NSLog(@"Current directory is: %@",path);

NSLog(@"All operations were successsful!");

[pool drain];

return 0;

// return NSApplicationMain(argc, (const char **) argv);

}

沒有留言: