C#如何将可执行文件复制到目录中 [英] C# how to copy executable to a directory

查看:116
本文介绍了C#如何将可执行文件复制到目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试将我的程序复制到%appdata%\ master文件夹中。这是我的代码。

 //这是用于创建目录
string a =
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) .ToString()+\\Master \\;
DirectoryInfo di = Directory.CreateDirectory(a);
//这是用于获取exe路径
string src = System.Diagnostics.Process.GetCurrentProcess()。MainModule.FileName;
//这是我要复制exe
字符串的地方dest = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString()+\\\\\\\\\\\\\
File.Copy(src,dest);



我收到此错误:



< blockquote class =quote>

Quote:



System.IO.DirectoryNotFoundException未处理

消息:未处理的异常在mscorlib.dll中输入'System.IO.DirectoryNotFoundException'

附加信息:找不到路径'C:\Users\ShomilaC\AppData \Roaming\Master的一部分\'。





提前致谢

解决方案

< blockquote>将文件复制到另一个目录时,还必须指定文件名;现在你正在复制到%AppData%\ Master ,但你必须复制到%AppData%\ Master \ FileNameHere.exe 。使用此 File.Copy 调用正确复制可执行文件:

 File.Copy(src,Path.Combine) (dest,Path.GetFileName(src))); 


Hello, I am trying to get my program copied on run to %appdata%\master folder. This is my code.

// this is for making a directory
string a =
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString() + "\\Master\\";
            DirectoryInfo di = Directory.CreateDirectory(a);
// this is for getting exe path
            string src = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
// this is where I want to copy the exe
            string dest = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString() + "\\Master\\";
            File.Copy(src, dest);


I get this error:

Quote:


System.IO.DirectoryNotFoundException was unhandled
Message: An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll
Additional information: Could not find a part of the path 'C:\Users\ShomilaC\AppData\Roaming\Master\'.



Thanks in advance

解决方案

When you copy a file into another directory, you also have to specify a file name; right now you are copying to %AppData%\Master, but you have to copy to %AppData%\Master\FileNameHere.exe. Use this File.Copy call to copy the executable correctly:

File.Copy(src, Path.Combine(dest, Path.GetFileName(src)));


这篇关于C#如何将可执行文件复制到目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆