C#如何将可执行文件复制到目录中

编程入门 行业动态 更新时间:2024-10-28 18:23:34
本文介绍了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);

我收到此错误:

Quote:

System.IO.DirectoryNotFoundException未处理 消息:未处理的异常在mscorlib.dll中输入'System.IO.DirectoryNotFoundException' 附加信息:找不到路径'C:\Users\ShomilaC\AppData \Roaming\Master的一部分\'。

提前致谢

解决方案

将文件复制到另一个目录时,还必须指定文件名;现在你正在复制到%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#如何将可执行文件复制到目录中

本文发布于:2023-11-09 13:51:54,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572527.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可执行   如何将   文件   目录中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!