【问题标题】:Saving an e-mail attachment to a UNC path将电子邮件附件保存到 UNC 路径
【发布时间】:2012-01-15 09:25:35
【问题描述】:

我在为 Outlook 编写的 VSTO 加载项中有以下代码:

        savefolder = Regex.Replace(Guid.NewGuid().ToString(), @"[- ]", String.Empty);

        savepathfull = string.Format(@"{0}{1}", netloc, savefolder);
        DirectoryInfo di = new DirectoryInfo(@savepathfull);
        if (!(di.Exists))
            Directory.CreateDirectory(@savepathfull);



        removedFiles = new List<string>();

        for (int d = attachs.Count; d > 0; d--)
        {
            if (attachs[d].Size > smallAttachment)
            {
                removedFiles.Add(attachs[d].FileName);
                attachs[d].SaveAsFile(savepathfull);
            }
        }

在我尝试保存附件之前一切正常,此时我收到 UnauthorizedAccessException。我知道我的测试用户拥有该文件夹的完全权限,但我仍然收到此错误。

想法?

谢谢。

【问题讨论】:

  • 哪一行抛出异常?你也可以发布异常消息和堆栈跟踪吗?
  • 我也会尝试删除那里的目录,看看它是否在 Directory.CreateDirectory 上出错尝试 catch 不要假设代码不会出错..如果它返回错误,那么可能是权限/权限问题您是否有权访问网络共享或文件夹..?
  • 我对你的实现感到困惑。 savepathfull 是文件还是目录?您似乎将它用作 (Directory.CreateDirectory(@savepathfull);attachs[d].SaveAsFile(savepathfull);)
  • @SliverNinja - Outlook.Attach.SaveAsFile 使用路径作为其变量。
  • @AdamRalph - attachs[d].SaveAsFile(savepathfull)

标签: c# .net outlook vsto outlook-addin


【解决方案1】:

调用Attachment.SaveAsFile 时需要提供有效的文件名。您正在尝试保存到目录,而不是文件。有关reference code,请参阅 MSDN。

attachs[d].SaveAsFile(Path.Combine(savepathfull, attachs[d].DisplayName);

【讨论】:

  • 同意@AdamRalph。 IDE 仅将(字符串路径)声明为变量。我想我应该先检查一下 MSDN。
猜你喜欢
  • 1970-01-01
  • 2013-02-19
  • 1970-01-01
  • 2019-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-12
相关资源
最近更新 更多