获取文件路径 文件打开对话框可可?

发布于 2024-11-29 19:34:08 字数 654 浏览 1 评论 0原文

我的应用程序中有一个文件打开对话框可以从中选择文件,但是当用户单击框中的“选择”按钮时,它显然不会执行任何操作。如何从所选文件中提取文件路径?我需要文件路径,以便我可以获取要加密的文件内容。最初,我将要使用的文件硬编码到我的应用程序中,但这仅用于测试目的。这是我用于文件打开对话框的内容:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千纸鹤 2024-12-06 19:34:08

使用 - (NSArray *)URLs 方法而不是 filenames

Use - (NSArray *)URLs method instead of filenames.

暮凉 2024-12-06 19:34:08

您的代码已经在处理用户选择的文件,您只是没有对它们执行任何操作。

-filenames 方法返回的数组包含用户选择为 NSString 对象的文件的路径。如果他们只选择了一个文件,则数组中将只有一个对象。如果他们没有选择任何文件,则数组将为空。

if ([openDlg runModalForDirectory:nil file:nil] == NSOKButton )
{
    NSArray* files = [openDlg filenames];
    for(NSString* filePath in [openDlg filenames])
    {
        NSLog(@"%@",filePath);
        //do something with the file at filePath
    }
}

如果您只希望用户能够选择单个文件,请在配置面板时调用[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 as NSString 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 ([openDlg runModalForDirectory:nil file:nil] == NSOKButton )
{
    NSArray* files = [openDlg filenames];
    for(NSString* filePath in [openDlg filenames])
    {
        NSLog(@"%@",filePath);
        //do something with the file at filePath
    }
}

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 the filenames 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 file NSURL objects rather than an array of NSStrings. Since pretty much all the file handling APIs in Snow Leopard were revised to take URLs, this would be the preferred option.

半边脸i 2024-12-06 19:34:08

您想使用以下代码获取文件路径

 NSOpenPanel* openPanel = [NSOpenPanel openPanel];
                [openPanel setCanChooseFiles:YES];
                [openPanel setCanChooseDirectories:NO];
                [openPanel setAllowsMultipleSelection: NO];
                [openPanel setAllowedFileTypes:ArrExtension ];
                if ([openPanel runModal] == NSOKButton ){

                   NSString *FilePath = [NSString stringWithFormat:@"%@",[openPanel URL]];
                   [openPanel canHide];
                 }

You Want to Get File Path Using Following Code

 NSOpenPanel* openPanel = [NSOpenPanel openPanel];
                [openPanel setCanChooseFiles:YES];
                [openPanel setCanChooseDirectories:NO];
                [openPanel setAllowsMultipleSelection: NO];
                [openPanel setAllowedFileTypes:ArrExtension ];
                if ([openPanel runModal] == NSOKButton ){

                   NSString *FilePath = [NSString stringWithFormat:@"%@",[openPanel URL]];
                   [openPanel canHide];
                 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文