Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用C将exe文件从Visual Studio项目复制到桌面?_C#_Visual Studio - Fatal编程技术网

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

C# 如何使用C将exe文件从Visual Studio项目复制到桌面?,c#,visual-studio,C#,Visual Studio,使用C将visual studio项目文件夹中的exe文件复制到计算机桌面或计算机中的其他位置时出现问题 我该怎么做呢?这个答案直接来自网站,请查看参考链接 下面的示例演示如何复制文件和目录 // Simple synchronous file copy operations with no user interface. // To run this sample, first create the following directories and files: // C:\Users\Pu

使用C将visual studio项目文件夹中的exe文件复制到计算机桌面或计算机中的其他位置时出现问题


我该怎么做呢?

这个答案直接来自网站,请查看参考链接

下面的示例演示如何复制文件和目录

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

参考:06-02-2018。

获取您的工作目录:

string appDir = AppDomain.CurrentDomain.BaseDirectory;
string targetDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
指定目标目录:

string appDir = AppDomain.CurrentDomain.BaseDirectory;
string targetDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
使用以下方法获取所需的文件:

复制您的文件:

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

我在复制方面有一个问题[…]-这到底是什么问题?一般来说,如果你想知道为什么没有人知道你在问什么,你可以使用以下方法来解释你的问题:如何复制文件?如何获取到桌面的路径?它与其他目录一起工作,但这里有一个例外,还有一件事要记住:在StackOverflow上,你会很快得到回复。如果你问一个问题,至少在头30分钟左右呆在这里。否则,您可能会发现很多这样的问题:您还可以添加Environment.GetFolderPathEnvironment.SpecialFolder.Desktop;作为ExpandEnvironmentVariables的替代方法,桌面路径并不总是遵循这种模式,或者至少使用%USERPROFILE%来代替它。@ManfredRadlwimmer我刚刚这么做了:我自己注意到了:PJust saw^^。不过我还是会换掉那个%USERNAME%。@ManfredRadlwimmer是的,我想使用environment更干净。您知道通过任何更改Environment.SpecialFolder.Desktop和Environment.SpecialFolder.DesktopDirectory之间的差异吗?当调试时,根本没有区别。不是100%确定在什么样的特殊情况下它是相关的,但很明显,答案听起来像是一个遗留问题