【问题标题】:How Can I copy an exe file from My Visual Studio Project to My Desktop using C#? [closed]如何使用 C# 将 exe 文件从我的 Visual Studio 项目复制到我的桌面? [关闭]
【发布时间】:2018-07-16 04:35:29
【问题描述】:

我在使用 C# 将我的 Visual Studio 项目文件夹中的 exe 文件复制到我的计算机桌面或计算机中的其他位置时遇到问题。

我该怎么做?

【问题讨论】:

  • 我在复制时遇到问题 [...] - 问题究竟是什么
  • 一般可以使用File.Copy
  • 如果您想知道为什么没有人知道您在问什么,这里有一些方法可以解释您的问题:“我如何复制文件?” “我怎样才能获得我的桌面的路径?”“它适用于其他目录,但在这里我得到一个例外” 等。包括您的代码 (minimal reproducible example) 并澄清你的问题。
  • 还有一点要记住:在 StackOverflow 上,您会非常快速获得响应。如果您提出问题,请至少坚持前 30 分钟左右。否则你可能会发现很多这样的:idownvotedbecau.se/beingunresponsive

标签: c# visual-studio


【解决方案1】:

此答案直接来自Microsoft网站,请查看参考链接。

以下示例显示如何复制文件和目录。

// Simple synchronous file copy operations with no user interface.
// To run this sample, first create the following directories and files:
// C:\Users\Public\TestFolder
// C:\Users\Public\TestFolder\test.txt
// C:\Users\Public\TestFolder\SubDir\test.txt
public class SimpleFileCopy
{
    static void Main()
    {
        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);

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

        // To copy a file to another location and 
        // overwrite the destination file if it already exists.
        System.IO.File.Copy(sourceFile, destFile, true);

        // To copy all the files in one directory to another directory.
        // Get the files in the source folder. (To recursively iterate through
        // all subfolders under the current directory, see
        // "How to: Iterate Through a Directory Tree.")
        // Note: Check for target path was performed previously
        //       in this code example.
        if (System.IO.Directory.Exists(sourcePath))
        {
            string[] files = System.IO.Directory.GetFiles(sourcePath);

            // Copy the files and overwrite destination files if they already exist.
            foreach (string s in files)
            {
                // Use static Path methods to extract only the file name from the path.
                fileName = System.IO.Path.GetFileName(s);
                destFile = System.IO.Path.Combine(targetPath, fileName);
                System.IO.File.Copy(s, destFile, true);
            }
        }
        else
        {
            Console.WriteLine("Source path does not exist!");
        }

        // Keep console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}

参考: Microsoft (06-02-2018)。

【讨论】:

    【解决方案2】:

    获取你的工作目录:

    string appDir = AppDomain.CurrentDomain.BaseDirectory;
    

    指定你的目标目录:

    string targetDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
    

    使用Directory.GetFiles获取你想要的文件:

    FileInfo[] files = new DirectoryInfo(appDir).GetFiles(*.*, SearchOption.TopDirectoryOnly);
    

    复制您的文件:

    try
    {
        foreach(FileInfo file in files)
        {
            File.Copy(file.FullName, Path.Combine(targetDir, file.Name), true);
        }
    }
    catch (Exception ex)
    {
        //Handle DirectoryAccess errors and others
    }
    

    【讨论】:

    • 您也可以添加Environment.GetFolderPath(Environment.SpecialFolder.Desktop)); 作为ExpandEnvironmentVariables 的替代品(桌面路径并不总是遵循该模式),或者至少使用%USERPROFILE% 代替。
    • @ManfredRadlwimmer 我刚刚做到了:D 我自己注意到了:P
    • 刚看到^^。不过我还是会换掉那个%USERNAME%
    • @ManfredRadlwimmer 是的,我想使用Environement 更“干净”。您知道 Environment.SpecialFolder.DesktopEnvironment.SpecialFolder.DesktopDirectory 之间的区别吗?调试时根本没有区别..
    • 不是 100% 确定它在什么样的特殊情况下是相关的,但显然 this 是答案(老实说,听起来像是遗留问题)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-16
    • 2022-01-02
    • 2014-02-24
    相关资源
    最近更新 更多