如何在C#中将文件从当前文件夹复制到另一个文件夹

问题描述 投票:0回答:3

如何将 test.pdf 从当前文件夹复制到 C:exfolder 我知道 C# 中的简单复制方式,但我想要适用于 exe 文件所在的任何文件夹的代码。 我很困惑,因为当我想使用当前文件夹路径 Visual Studio 2008 错误时 因为我用过:

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);
c# file copy
3个回答
1
投票

您可以使用

System.Environment.CurrentDirectory
获取 exe 文件的目录,然后使用
System.IO.File.Copy
方法将该文件复制到任何目的地。另外,为了获取当前目录,您可能需要查看this


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


0
投票

字符串 FactorImagename = Factor.FactorImage;

if (System.IO.File.Exists(Request.MapPath("~/Images/Factor/" + FactorImagename))) { System.IO.File.Copy(HttpContext.Server.MapPath("~/Images/Factor/") + FactorImagename, HttpContext.Server.MapPath("~/Images/Account/") + acDocument.Img, true); }

© www.soinside.com 2019 - 2024. All rights reserved.