【发布时间】:2022-01-22 01:21:42
【问题描述】:
我正在尝试制作一个简单的 exe,它在打开时会移动到我的文档文件夹中,但是当我打开它时,它并没有这样做,我该怎么办?
string fileName = "installer.exe";
string strExeFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string strWorkPath = System.IO.Path.GetDirectoryName(strExeFilePath);
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFileMove = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
System.IO.File.Move(sourceFile, destFileMove);
【问题讨论】:
-
“它没有那样做” - 有没有抛出任何错误?
-
"移动自身" - 当应用程序运行时,它的文件将被锁定,并且不能被移动。那么你真正想做的是什么?
-
如果这就是程序所做的一切,您会在 Windows 应用程序事件日志中找到它无法正常工作的原因。使用调试器诊断异常,注意 try/catch-say-nothing 代码。 stackoverflow.com/a/3133249/17034
-
@JonasH 不正确。您可以移动当前的 exe,因为这只是一个重命名操作。用一个简单的控制台应用程序试一试,你会看到它在可执行文件运行时也能正常工作。
-
@Ortund - 这不是一个有用的评论。
标签: c# visual-studio