2017-07-18 26 views
1

我正在使用c#。我收到有关其他进程当前访问的路径的错误。我的系统试图做的是访问路径:@“C:\ temps \”+ client_ids +“_”+ rown +“.pdf”,并在发送给客户的电子邮件之前使用相同的路径进行附件。如何使2进程访问相同的路径?

这是我到目前为止所做的。我注释掉了我的一些代码,因为我不知道该怎么做。

FileStream fs = null; 
using (fs = new FileStream(@"C:\\temps\\" + client_ids + "_" + 
rown + ".pdf", 
FileMode.Open,FileAccess.Read,FileShare.ReadWrite)) 
{ 
    TextReader tr = new StreamReader(fs);     
    //report.ExportToDisk 
    //(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat,tr); 
    //report.Dispose(); 
    //Attachment files = new Attachment(tr); 
    //Mailmsg.Attachments.Add(files); 
    //Clients.Send(Mailmsg); 
} 
+0

什么类型是'report'变量?它看起来像你试图将报告导出到一个不太可能工作的“TextReader”。 –

+0

始终使用[Path.Combine](https://msdn.microsoft.com/en-us/library/system.io.path.combine(v = vs.110).aspx)构建路径。 – FortyTwo

+0

感谢您的建议四十二。 – 19GreenBlankets

回答

4

您在邮​​件的附件使用它之前,然后使用,而不是原来的文件

+0

阿里爵士,你有什么机会得到你想说的话吗?我得到你想说的先生,但我不确定我的代码库。非常感谢你的回复。 – 19GreenBlankets

+0

不客气。 从微软检出此链接:https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-copy-delete-and-move-files-and-folders – Ali

0

您不能将文件附加到电子邮件中,如果该文件是开放的副本,你可以使文件的临时副本。您必须先关闭(保存)文件。

@ali答案在技术上是正确的,这是没有必要的。为什么要经历创建文件副本的开销,然后需要删除等等?

假设我明白你想要做的是正确的,只需将你的邮件代码移动到文件被成功创建和保存后即可。而且,我不认为你需要文件流或文本阅读器的开销。只要您的报告对象可以将文件保存到磁盘的某个位置,您可以将该文件附加到您的电子邮件中,然后发送它。

虽然我并不知道的Crystal Decisions如何处理出口,等事情也许类似这样的工作:

(我得到了这样的代码:https://msdn.microsoft.com/en-us/library/ms226036(v=vs.90).aspx

private void ExportToDisk (string fileName) 
{ 
    ExportOptions exportOpts = new ExportOptions(); 
    DiskFileDestinationOptions diskOpts = 
     ExportOptions.CreateDiskFileDestinationOptions(); 

    exportOpts.ExportFormatType = ExportFormatType.RichText; 
    exportOpts.ExportDestinationType = 
     ExportDestinationType.DiskFile; 

    diskOpts.DiskFileName = fileName; 
    exportOpts.ExportDestinationOptions = diskOpts; 

    Report.Export(exportOpts); 
} 

你会需要更改ExportFormatType属性。

然后,将文件只需连接到您的电子邮件,然后发送:

Attachment Files = new Attachment(filename); 
Mailmsg.Attachments.add(files); 
Clients.Send(Mailmsg);