尝试保存 Outlook 附件时出现“System.IO.FileNotFound”

我构建了一个小型网络应用程序来读取 Outlook 日历中的约会,并且使用了Microsoft.Office.Interop.Outlook. 现在我希望能够保存约会中的附件。


到目前为止,这是我的代码:


foreach (var item in AppointmentItems) {    

    for (int i = 1; i <= item.Attachments.Count; i++) {


        var Attachment = item.Attachments[i];


        string SavePath = Path.Combine(@"D:\SaveTest", Attachment.FileName);


        Attachment.SaveAsFile(SavePath);

    }

}

问题 :


抛出异常:'System.IO.FileNotFoundException 恰好位于

Attachment.SaveAsFile(SavePath);


我已经到处查看了,此方法应该将附件保存到路径,但它以某种方式尝试读取文件。


慕容708150
浏览 246回答 3
3回答

开心每一天1111

假设附件存在,FileNotFoundExecption是由路径中不存在的部分触发的。您可以先检查该路径是否存在:Directory.Exists(@"D:\SaveTest")然后你可以检查你是否对该目录有写权限:Try{return System.IO.Directory.GetAccessControl(@"D:\SaveTest")&nbsp; &nbsp; &nbsp; &nbsp; .GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount))&nbsp; &nbsp; &nbsp; &nbsp; .Cast<System.Security.AccessControl.FileSystemAccessRule>()&nbsp; &nbsp; &nbsp; &nbsp; .Where(rule => (System.Security.AccessControl.FileSystemRights.Write & rule.FileSystemRights) == System.Security.AccessControl.FileSystemRights.Write)&nbsp; &nbsp; &nbsp; &nbsp; .Any(rule => rule.AccessControlType == System.Security.AccessControl.AccessControlType.Allow);} catch(Exception){&nbsp; return false;}

拉风的咖菲猫

您可以尝试做的三件事:确保该目录存在检查Attachment.FileName是否具有有效的名称和扩展名检查您的写入权限

慕沐林林

System.IO.FileNotFoundExecption 意味着它找不到您正在查找的文件或您尝试保存到的路径。删除@并尝试“D:\文件夹名称\”+附件.文件名。虽然删除 @ 应该仍然有效,但我认为您需要使用加号运算符。将帮助您发布整个代码块,以便我们可以从上到下了解发生了什么。
打开App,查看更多内容
随时随地看视频慕课网APP