如何将文件保存在 windows 应用程序的文件夹中
How to save file in a folder in windows application
我想在电子邮件中发送附件,所以我想先将附件文件保存在文件夹中。
So what's happening with my current code is, the mail is going with attachment but my file is not getting saved into the ATTACHMENT
folder.
这是我试过的代码
for (int i = 0; i < table.Rows.Count; i++)
{
if (1 == 1)
{
string StrPriBody = "This is a test mail for checking notification.";
MailMessage mail = new MailMessage();
mail.Subject = "Daily Holding followup scheduler";
mail.From = new System.Net.Mail.MailAddress("autosupport@powersoft.in");
SmtpClient smtp = new SmtpClient();
smtp.Timeout = 1000000;
smtp.Port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["Port"]);
smtp.UseDefaultCredentials = true;
smtp.Host = System.Configuration.ConfigurationManager.AppSettings["MailHost"];
smtp.Credentials = new NetworkCredential(mail.From.ToString(), "PS123456");
smtp.EnableSsl = true;
mail.To.Add(new MailAddress("abc@test.com"));
mail.IsBodyHtml = true;
mail.Body = StrPriBody;
DataSet ds = new DataSet();
ds.Tables.Add(table);
ds.Tables[0].TableName = "Post sale follow up entries auto mailer";
SaveFileDialog save = new SaveFileDialog();
save.InitialDirectory = "\Attachment";
save.RestoreDirectory = true;
ExcelLibrary.DataSetHelper.CreateWorkbook("Employee_Details.xls", ds);
mail.Attachments.Add(new Attachment("Employee_Details.xls"));
smtp.Send(mail);
foreach (Attachment attachments in mail.Attachments)
{
attachments.Dispose();
}
您不需要 SaveFileDialog 来执行此操作(并且在您的代码中定义了它但没有使用它)。只需使用 File.Copy。因此,在此处创建工作簿后:
ExcelLibrary.DataSetHelper.CreateWorkbook("Employee_Details.xls", ds);
只需添加如下内容:
File.Copy("Employee_Details.xls","\Attachment\Employee_Details.xls");
1) 为什么要使用 SaveFileDialog
?用户必须选择要保存到的文件夹和文件名?然后你忘记了 "display" 对话框,并且在用户关闭它之后 - 从中检索选择的 folder/file 名称并且(最好)使用完整路径(C:\Folder\File ...) CreateWorkbook
和 new Attachment(...)
.
2) 您确定在调用 ExcelLibrary.DataSetHelper.CreateWorkbook
之后该文件实际上已将内容写入磁盘吗?可能您需要调用一些 Save()
方法? (这是库特定的,请阅读库文档)
3) 您确定通过编写代码调用ExcelLibrary.DataSetHelper.CreateWorkbook
后新文件是closed/unlocked 吗?也许您需要 Dispose
一些东西? (再次检查图书馆文档)
4) 您是在服务器(网站?)还是台式机上进行测试?检查您是否可以对要存储文件的文件夹进行写入访问。
我想在电子邮件中发送附件,所以我想先将附件文件保存在文件夹中。
So what's happening with my current code is, the mail is going with attachment but my file is not getting saved into the
ATTACHMENT
folder.
这是我试过的代码
for (int i = 0; i < table.Rows.Count; i++)
{
if (1 == 1)
{
string StrPriBody = "This is a test mail for checking notification.";
MailMessage mail = new MailMessage();
mail.Subject = "Daily Holding followup scheduler";
mail.From = new System.Net.Mail.MailAddress("autosupport@powersoft.in");
SmtpClient smtp = new SmtpClient();
smtp.Timeout = 1000000;
smtp.Port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["Port"]);
smtp.UseDefaultCredentials = true;
smtp.Host = System.Configuration.ConfigurationManager.AppSettings["MailHost"];
smtp.Credentials = new NetworkCredential(mail.From.ToString(), "PS123456");
smtp.EnableSsl = true;
mail.To.Add(new MailAddress("abc@test.com"));
mail.IsBodyHtml = true;
mail.Body = StrPriBody;
DataSet ds = new DataSet();
ds.Tables.Add(table);
ds.Tables[0].TableName = "Post sale follow up entries auto mailer";
SaveFileDialog save = new SaveFileDialog();
save.InitialDirectory = "\Attachment";
save.RestoreDirectory = true;
ExcelLibrary.DataSetHelper.CreateWorkbook("Employee_Details.xls", ds);
mail.Attachments.Add(new Attachment("Employee_Details.xls"));
smtp.Send(mail);
foreach (Attachment attachments in mail.Attachments)
{
attachments.Dispose();
}
您不需要 SaveFileDialog 来执行此操作(并且在您的代码中定义了它但没有使用它)。只需使用 File.Copy。因此,在此处创建工作簿后:
ExcelLibrary.DataSetHelper.CreateWorkbook("Employee_Details.xls", ds);
只需添加如下内容:
File.Copy("Employee_Details.xls","\Attachment\Employee_Details.xls");
1) 为什么要使用 SaveFileDialog
?用户必须选择要保存到的文件夹和文件名?然后你忘记了 "display" 对话框,并且在用户关闭它之后 - 从中检索选择的 folder/file 名称并且(最好)使用完整路径(C:\Folder\File ...) CreateWorkbook
和 new Attachment(...)
.
2) 您确定在调用 ExcelLibrary.DataSetHelper.CreateWorkbook
之后该文件实际上已将内容写入磁盘吗?可能您需要调用一些 Save()
方法? (这是库特定的,请阅读库文档)
3) 您确定通过编写代码调用ExcelLibrary.DataSetHelper.CreateWorkbook
后新文件是closed/unlocked 吗?也许您需要 Dispose
一些东西? (再次检查图书馆文档)
4) 您是在服务器(网站?)还是台式机上进行测试?检查您是否可以对要存储文件的文件夹进行写入访问。