简体   繁体   English

命令将文件复制到另一个用户选择的目录?

[英]Command to copy a file to another, user-chosen directory?

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);

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);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM