-4

I have this code to copy a file to another destination, however I need the users to choose the destination plus the name of file copied is = the old name with the date and hour of my PC ..

string fileToCopy = @"d:\pst\2015.pst";
string destinationDirectoryTemplate = textBox2.Text;

var dirPath = String.Format(destinationDirectoryTemplate, DateTime.UtcNow);
var di = new DirectoryInfo(dirPath);

if (!di.Exists)
{
    di.Create();
}

var fileName = Path.GetFileName(fileToCopy);
var targetFilePath = Path.Combine(dirPath, fileName);

File.Copy(fileToCopy, targetFilePath);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    Sorry, but I don't know how to fix **image** with bad code formatting. – Sinatr Nov 26 '15 at 13:36
  • Please copy/paste the code into your post (so other people can paste it into their own editor to make changes), and describe what is happening when you try to run this. – Alex Nov 26 '15 at 13:36
  • string fileToCopy = @"d:\pst\2015.pst"; string destinationDirectoryTemplate = textBox2.Text; var dirPath = string.Format(destinationDirectoryTemplate, DateTime.UtcNow); var di = new DirectoryInfo(dirPath); if(!di.Exists) { di.Create(); } var fileName = Path.GetFileName(fileToCopy); var targetFilePath = Path.Combine(dirPath, fileName); File.Copy(fileToCopy, targetFilePath); – Piero Chankji Nov 26 '15 at 13:37
  • Possible duplicate of [How to copy a file to another path?](http://stackoverflow.com/questions/1979920/how-to-copy-a-file-to-another-path) – Fᴀʀʜᴀɴ Aɴᴀᴍ Nov 26 '15 at 13:39
  • no i need string destinationDirectoryTemplate got his full path in textbox – Piero Chankji Nov 26 '15 at 13:42

1 Answers1

0

This should work

protected void Button3_Click(object sender, EventArgs e)
        {
            string fileToCopy = @"e:\TestFile.pdf"; 
            string destinationDirectoryTemplate = TextBox1.Text;
            var dirPath = string.Format(destinationDirectoryTemplate, DateTime.UtcNow);
            var di = new DirectoryInfo(dirPath);
            if (!di.Exists) 
            { di.Create(); } 
            var fileName = Path.GetFileNameWithoutExtension(fileToCopy);
            var extn = Path.GetExtension(fileToCopy);

            var finalname = fileName + " " + string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}", DateTime.Now) + extn;
            var targetFilePath = Path.Combine(dirPath, finalname);
            File.Copy(fileToCopy, targetFilePath);
        }
Akhil R J
  • 184
  • 2
  • 14