如何将test.pdf从当前文件夹复制到C:exfolder 我知道C#中的siple复制方式但是我想要的代码适用于exe文件所在的任何文件夹。 当我想使用当前文件夹路径visual studio 2008 erros时,我被迷惑了 beacaus我用过:
string fileName = "test_log.LDF";
string sourcePath = @ + Application.StartupPath;
string targetPath = @"C:\honar2";
// Use Path class to manipulate file and directory paths.
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
// To copy a folder's contents to a new location:
// Create a new target folder, if necessary.
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
System.IO.File.Copy(sourceFile, destFile, true);
答案 0 :(得分:1)
您可以使用System.Environment.CurrentDirectory
获取exe文件的目录,然后使用System.IO.File.Copy
方法将该文件复制到任何目标。另外,要获取当前目录,您可能需要查看this
答案 1 :(得分:1)
试试这个
string fileName = "test.txt";
string sourcePath = @"C:\Users\Public\TestFolder";
string targetPath = @"C:\Users\Public\TestFolder\SubDir";
// Use Path class to manipulate file and directory paths.
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(sourceFile, destFile, true);
更多信息请点击MSDN