获取文件路径 文件打开对话框可可?
我的应用程序中有一个文件打开对话框可以从中选择文件,但是当用户单击框中的“选择”按钮时,它显然不会执行任何操作。如何从所选文件中提取文件路径?我需要文件路径,以便我可以获取要加密的文件内容。最初,我将要使用的文件硬编码到我的应用程序中,但这仅用于测试目的。这是我用于文件打开对话框的内容:
int i;
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
[openDlg setCanChooseFiles:YES];
[openDlg setCanChooseDirectories:YES];
[openDlg setPrompt:@"Select"];
NSString *fileName = [pathAsNSString lastPathComponent];
[fileName stringByDeletingPathExtension];
if ([openDlg runModalForDirectory:nil file:nil] == NSOKButton )
{
NSArray* files = [openDlg filenames];
for( i = 0; i < [files count]; i++ )
{
[files objectAtIndex:i];
}
}
非常感谢您的帮助。
I have a File Open Dialog box in my application to select files from, but when the user clicks the 'Select' button in the box, it obviously won't do anything. How do I extract the filepath from the selected file? I need the filepath so I can get the contents of the file to encrypt. Initially, I hard coded the file I would use into my application, but that was only for testing purposes. Here is what I am using for the File Open Dialog Box:
int i;
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
[openDlg setCanChooseFiles:YES];
[openDlg setCanChooseDirectories:YES];
[openDlg setPrompt:@"Select"];
NSString *fileName = [pathAsNSString lastPathComponent];
[fileName stringByDeletingPathExtension];
if ([openDlg runModalForDirectory:nil file:nil] == NSOKButton )
{
NSArray* files = [openDlg filenames];
for( i = 0; i < [files count]; i++ )
{
[files objectAtIndex:i];
}
}
Thanks so much for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用
- (NSArray *)URLs
方法而不是filenames
。Use
- (NSArray *)URLs
method instead offilenames
.您的代码已经在处理用户选择的文件,您只是没有对它们执行任何操作。
从
-filenames
方法返回的数组包含用户选择为NSString
对象的文件的路径。如果他们只选择了一个文件,则数组中将只有一个对象。如果他们没有选择任何文件,则数组将为空。如果您只希望用户能够选择单个文件,请在配置面板时调用
[openPanel setAllowsMultipleSelection:NO]
。这样,filenames
数组中最多只有一个条目。正如 @VenoMKO 指出的,
-filenames
方法现已弃用,您应该使用-URLs
方法。这将返回一个文件NSURL
对象数组,而不是一个NSString
数组。由于 Snow Leopard 中几乎所有文件处理 API 都经过修改以采用 URL,因此这将是首选选项。Your code is already handling the files that the user has selected, you're just not doing anything with them.
The array returned from the
‑filenames
method contains the paths to the files that the user selected asNSString
objects. If they have only selected one file, there will only be one object in the array. If they have selected no files, the array will be empty.If you only want the user to be able to select a single file, then call
[openPanel setAllowsMultipleSelection:NO]
when you're configuring the panel. That way, there will be a maximum of one entry in thefilenames
array.As @VenoMKO points out, the
‑filenames
method is now deprecated and you should use the‑URLs
method instead. This will return an array of fileNSURL
objects rather than an array ofNSString
s. Since pretty much all the file handling APIs in Snow Leopard were revised to take URLs, this would be the preferred option.您想使用以下代码获取文件路径
You Want to Get File Path Using Following Code