如何使用C#将exe文件从My Visual Studio项目复制到我的桌面?

时间:2018-02-06 07:21:07

标签: c# visual-studio

我有一个问题,即使用C#将我的visual studio项目文件夹中的exe文件复制到我的计算机桌面或计算机的其他位置。

我该怎么做?

2 个答案:

答案 0 :(得分: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)。

答案 1 :(得分:1)

获取您的工作目录:

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
}