如何让2个进程访问相同的路径?

时间:2017-07-18 20:05:22

标签: c#

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

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

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);
}

2 个答案:

答案 0 :(得分:4)

您可以在使用邮件附件之前制作文件的临时副本,然后使用副本而不是原始文件

答案 1 :(得分: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);
相关问题