C# 如何让File.Delete()实际删除我的文件?
我生成一个PDF文件,将其保存在服务器上:C# 如何让File.Delete()实际删除我的文件?,c#,file,sharepoint-2010,delete-file,C#,File,Sharepoint 2010,Delete File,我生成一个PDF文件,将其保存在服务器上: var bytes = ms.ToArray(); . . . String fileFullpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), pdfFileName); . . . File.WriteAllBytes(fileFullpath, bytes); …然后将其保存到Sharepoint文档库中,并将其作为
var bytes = ms.ToArray();
. . .
String fileFullpath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), pdfFileName);
. . .
File.WriteAllBytes(fileFullpath, bytes);
…然后将其保存到Sharepoint文档库中,并将其作为电子邮件附件发送给生成文件的人员:
SavePDFToDocumentLibrary(fileFullpath);
String from = GetFromEmailID();
String to = GetUserEmail();
String subjLine = String.Format("The PDF file you generated ({0})", pdfFileName);
String body = String.Format("The Direct Pay PDF file you generated ({0}) is attached.", pdfFileName);
SendEmailWithAttachment(fileFullpath, from, to, subjLine, body);
// Now that it has been put in a Document Library and emailed, delete the file that was saved locally
File.Delete(fileFullpath);
…此时,我不再需要保存到服务器磁盘上的文件,因此,如上面最后一行所示,尝试将其删除
但是,它不起作用。现在的冗余文件仍在保存位置
为什么?我怎样才能理解“删除”真正的意思是“删除”
更新
以下是Scott希望看到的方法:
// This works; got it from Henry Zucchini's answer at http://stackoverflow.com/questions/468469/how-do-you-upload-a-file-to-a-document-library-in-sharepoint
private void SavePDFToDocumentLibrary(String fullpath)
{
String fileToUpload = fullpath;
String sharePointSite = siteUrl;
String documentLibraryName = "DirectPayPDFForms";
using (SPSite oSite = new SPSite(sharePointSite))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
if (!System.IO.File.Exists(fileToUpload))
throw new FileNotFoundException("File not found.", fileToUpload);
SPFolder doclib = oWeb.Folders[documentLibraryName];
// Prepare to upload
Boolean replaceExistingFiles = true;
String fileName = System.IO.Path.GetFileName(fileToUpload);
FileStream fileStream = File.OpenRead(fileToUpload);
// Upload document
SPFile spfile = doclib.Files.Add(fileName, fileStream, replaceExistingFiles);
// Commit
doclib.Update();
}
}
}
// This is adapted from https://msdn.microsoft.com/en-us/library/system.net.mail.mailmessage(v=vs.90).aspx
public static void SendEmailWithAttachment(string fileToMail, String from, String to, String subj, String body)
{
String server = GetSMTPHostName(); //"468802-DEV-SPWF"; // change this to prod when go live, or programatically assign?
// Specify the file to be attached and sent.
string file = fileToMail;
// Create a message and set up the recipients.
MailMessage message = new MailMessage(
from,
to,
subj,
body);
// Create the file attachment for this e-mail message.
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
// Add the file attachment to this e-mail message.
message.Attachments.Add(data);
//Send the message.
SmtpClient client = new SmtpClient(server);
// Add credentials if the SMTP server requires them.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
try
{
client.Send(message);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}", ex.ToString());
}
// Display the values in the ContentDisposition for the attachment.
// May not need/want this section
ContentDisposition cd = data.ContentDisposition;
Console.WriteLine("Content disposition");
Console.WriteLine(cd.ToString());
Console.WriteLine("File {0}", cd.FileName);
Console.WriteLine("Size {0}", cd.Size);
Console.WriteLine("Creation {0}", cd.CreationDate);
Console.WriteLine("Modification {0}", cd.ModificationDate);
Console.WriteLine("Read {0}", cd.ReadDate);
Console.WriteLine("Inline {0}", cd.Inline);
Console.WriteLine("Parameters: {0}", cd.Parameters.Count);
foreach (DictionaryEntry d in cd.Parameters)
{
Console.WriteLine("{0} = {1}", d.Key, d.Value);
}
// </ May not need/want this section
data.Dispose();
}
…IOException捕获块中毕竟存在异常:
进程无法访问文件“C:\Users\TEMP.SP.018\Desktop\DirectPayDynamic\u 2015Jul28\u 19\u 02\u clayshan\u 0.pdf”,因为另一进程正在使用该文件
那么,其他方法中的一种是如何抓住它的呢?ISTM表明SavePDFToDocumentLibrary()是安全的,因为它使用using块
是data.Dispose();在SendEmailWithAttachment()中不够?我需要在那里显式地调用close,还是什么
更新3
我在SendEmailWithAttachment()中的“data.Dispose();”之前添加了“message.Dispose();”,但没有任何区别。尝试处理
SavePDFToDocumentLibrary
中使用的文件流,如下所示:
using (FileStream fileStream = File.OpenRead(fileToUpload))
{
...
}
您在
SavePDFToDocumentLibrary
或sendmailwithattachment
中出现错误,该错误未关闭文件。您需要显示这两种方法的代码。具体地说,您使用fileFullpath
打开文件的位置是根据“如果文件不存在,则不会引发异常”。在删除文件之前,请执行File.Exists()检查,以查看文件是否由于任何奇怪的原因不存在。您可能还需要处理SmtpClient
和MailMessage
(隐式地通过使用
语句)我以前有过这种情况,不得不求助于重试/睡眠场景——不知何故,文件系统没有立即释放锁。如果你不想正确地点击它们,你可以在特定文件夹中创建文件,并删除该文件夹中超过一分钟的所有文件。。。这两种方法都是丑陋的黑客行为,因为你的代码应该是正确的。@B.ClayShannon在SavePDFToDocumentLibrary
中使用文件.OpenRead
怎么样?
using (FileStream fileStream = File.OpenRead(fileToUpload))
{
...
}