【问题标题】:Copy files to and from Application Support将文件复制到应用程序支持和从应用程序支持复制
【发布时间】:2013-10-25 03:39:57
【问题描述】:

我在公司经常处理 Lotus Notes。我用 C# 编写了一个很棒的应用程序,用于将特定文件复制到用户的 Lotus Notes 目录和从用户的 Lotus Notes 目录复制。现在我想在 Objective C 中为 OSX 编写该应用程序。我有一些不同的文件需要从 ~/Library/Application Support/Lotus Notes Data/ 复制。

我在运行测试以复制单个文件时遇到了管理问题。提示用户输入管理员凭据并使用新获得的权限执行文件复制代码的最佳/最简单方法是什么?

我确实尝试过实现我在网上找到的 BLAuthentication 类,但它无法编译。我目前无法访问我的工作计算机来发布代码。

【问题讨论】:

    标签: cocoa admin file-copying


    【解决方案1】:

    试试下面这样:-

    NSString *path=[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES)lastObject];
    NSString *testUrl =[path stringByAppendingPathComponent:@"/LotusNotesData/source.rtf"];
    //
    if ([[NSFileManager defaultManager]fileExistsAtPath:testUrl]) {
        NSLog(@"yes");
    }
    //Below destination is folder name which should be exist on your machine or else you can create programmatically as well
    NSString *testUrl2 = @"/Users/home/Documents/destination";
    
    NSLog(@"%@",testUrl);
    NSLog(@"%@",testUrl2);
    NSError *err=nil;
    
    //Now we are copying the souce path to destination folder with appending file name (it can be any your name becuase file manager copy source file contents to your destination file contents)
    //Here given file name is a source.rtf where you can give any your name. Also this is for copying source contents to destination contents
    
    NSFileManager *fm=[NSFileManager defaultManager];
    if ([fm copyItemAtPath:testUrl toPath:[testUrl2 stringByAppendingPathComponent:@"source.rtf"] error:&err])
    {
        NSLog(@"success");
    }
    else
    {
        NSLog(@"%@",[err localizedDescription]);
    }
    

    【讨论】:

    • 另外,如果想枚举多个文件,请点击此链接stackoverflow.com/questions/18683345/…
    • 侯赛因,效果很好。非常感谢!你能解释一下这是如何从 /Library/ vs CopyItemAtPath 复制失败的吗?
    • 我没有得到你的问题??你想知道 copyItemAtPath 是如何工作的吗???
    • 有点,因为我之前尝试过 CopyItemAtPath,但它总是会失败,因为我无权在用户 /Library 复制文件。我只是好奇你的代码是如何绕过它的。
    • 看到 copyItemAtPath 只会复制路径中的文件项,这意味着您的源路径应该是准确的,当您复制到目标路径时,您必须附加目标路径的文件。然后只有它会复制。在您的情况下,可能是目标路径不正确。
    【解决方案2】:

    使用 Apple 脚本复制具有特权访问权限的文件。

    do shell script "cp source_path destination_path" with administrator privileges
    

    其中源路径是要复制的文件的路径。

    您可以通过在捆绑包中添加带有上述脚本的“.scpt”文件并使用以下代码来调用 Apple 脚本:

    - (void) runEmbeddedScriptFile: (NSString*)fileName
    {
        NSString* path = [[NSBundle bundleForClass:[self class]] pathForResource:fileName ofType:@"scpt"];
        NSURL* url = [NSURL fileURLWithPath:path];
        NSDictionary* errors = [NSDictionary dictionary];
        NSAppleScript* appleScript = [[NSAppleScript alloc] initWithContentsOfURL:url error:&errors];
        [appleScript executeAndReturnError:nil];
        [appleScript release];
    }
    

    【讨论】:

    • 这将提示用户输入密码。
    • 脚本在脚本编辑器中工作,但您的代码没有运行脚本。
    • script 正在脚本编辑器中工作,但您的代码不运行 script 。因为 URL 已更改,请参见路径 = /Users/priyadarshanjoshi/Library/Developer/Xcode/DerivedData/PMB-cxezmjfqztaqoyaedjsgubffmbso/Build/Products/Debug/PMB.app/Contents/Resources/CopyAppleScript.scpt 并且在 URL 中转换之后是 = 文件: ///Users/priyadarshanjoshi/Library/Developer/Xcode/DerivedData/PMB-cxezmjfqztaqoyaedjsgubffmbso/Build/Products/Debug/PMB.app/Contents/Resources/CopyAppleScript.scpt 但在执行脚本文件后未复制且未显示任何错误。
    猜你喜欢
    • 1970-01-01
    • 2020-01-31
    • 2011-10-25
    • 1970-01-01
    • 2013-10-08
    • 2019-08-07
    • 1970-01-01
    • 2017-04-13
    • 2013-12-25
    相关资源
    最近更新 更多