没有管理员权限我可以保存到哪个存储库?

时间:2011-07-29 17:04:05

标签: c# asp.net

我正面临着我正在研究的互联网应用程序的问题(用C#编程)。

我必须创建报告,然后通过电子邮件将其发送给某个用户。创建报告后,我首先将其保存到临时文件中,然后将其附加到提供文件路径的电子邮件中。

它正在我的计算机上运行,​​因为我有管理员权限,但它不适用于那些没有管理员权限的同事。

我正在使用的文件路径是:

string filePath = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.InternetCache), 
    fileName
);

我是否可以使用不需要管理员权限的临时存储库?

感谢。

3 个答案:

答案 0 :(得分:3)

考虑到您的ASP.NET代码,您应该考虑使用Isolated Storage

答案 1 :(得分:2)

如果您正在使用.Net中的内置邮件类,那么根本不需要将附件写入文件,除非生成报告的内容需要它。

这可行,假设您的报告生成器不需要文件输出,只能返回字节。

            SmtpClient smtpClient = new SmtpClient(); //do whatever else you need to do here to configure this
            byte[] report = GetReport();//whatever your report generator is
            MailMessage m = new MailMessage();
            //add your other mail fields (body, to, cc, subject etc)
            using (MemoryStream stream = new MemoryStream(report))
            {
                m.Attachments.Add(new Attachment(stream,"reportfile.xls"));//just guessing, use the right filename for your attachment type
                smtpClient.Send(m);  //note that we send INSIDE this using block, because it will not actually read the stream until you send
                                     //and you want to make sure not to dispose the stream before it reads it
            }

答案 2 :(得分:0)

您如何将其附加到电子邮件中?从你的问题的声音看来,你正在做的只是给他们你创建的文件的路径,而不是附加它(因为,一旦你附加它,它嵌入在电子邮件中,因此没有涉及的路径)

如果Web应用程序正在创建临时文件,那么您可以使用app_data文件夹(通常是可写的),并使用Path.GetRandomFileName()获取唯一的文件名。

所以,像

var myTemporaryFileName = Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data", 
                                       Path.GetRandomFileName());

然后将您的文件写入此临时文件名,然后将其附加到电子邮件

MailMessage message = new MailMessage("recipient@example.com", "", ""               
                      "subject",
                      "mail body");

Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
ContentDisposition disposition = data.ContentDisposition;
disposition.FileName = "thefilenameyouwanttouseintheemail.ext";
message.Attachments.Add(data);

现在你可以发送它。

尽管不要忘记清理它们!