应用程序不会从命令行的完整路径启动,但会在CD到目录后启动 [英] Application will not launch from command line full path, but will after CDing to directory

查看:86
本文介绍了应用程序不会从命令行的完整路径启动,但会在CD到目录后启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从注册表Run项(在HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run,其他字符串值存在并启动的地方)运行.Net C#应用程序.但是由于某些无法解释的原因,我的应用程序路径无法启动我的应用程序.因此,我决定从命令提示符处手动运行命令,以查看是否是问题所在.它是.所以现在我真的很困惑:

I am attempting to get a .Net C# application to run from a Registry Run key (at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run, where other string values live and launch things just fine). But for some inexplicable reason, the path to my app doesn't launch my app. So I decided to run the command manually from a command prompt, just to see if that was the problem. It was. So now I'm really perplexed:

这是我在发布模式下编译的.Net 4.0 C#应用程序.该应用程序位于:

This is a .Net 4.0 C# application which I've compiled in Release mode. The application lives at:

C:\Program Files (x86)\MyCompany\MyProduct\MyProduct.exe

我可以双击该应用程序,它可以正常运行.我还可以打开CMD窗口并执行以下操作:

I can double-click on the application and it runs properly. I can also open a CMD window and do the following:

cd "C:\Program Files (x86)\MyCompany\MyProduct\"
MyProduct.exe

应用程序启动就很好了. 但是,如果我尝试这样做:

And the application launches just fine. HOWEVER, if I try this:

"C:\Program Files (x86)\MyCompany\MyProduct\MyProduct.exe"

应用程序无法启动. (!)因此,显然注册表项也不起作用.

The application does not launch. (!) SO obviously the Registry key isn't going to work either.

要从其完整路径运行.Net应用程序,是否还需要采取某些其他步骤?

Is there some kind of additional step which must be taken to run a .Net application from its full path?

推荐答案

显然,您的应用依赖于当前目录".用于打开某种文件.

Apparently your app depends on the "Current Directory". For opening some sort of file.

最好的办法是找到该依赖关系,并使用绝对路径对其进行修复.

The best thing to do is to find that dependency and fix it with an absolute path.

当这不可能时,第二好的选择是尽快将当前"文件夹更改为正在运行的.EXE的文件夹.这意味着您应该尽快执行此行:

When that's not possible the second-best option would be to change the Current folder to that of the running .EXE as soon as possible. That mean you should execute this line as soon as possible:

Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;

但这仅在您设法在错误代码之前执行的情况下才有效.例如在静态构造函数中可能很难.

But that will only work if you manage to execute that before the faulty code. Could be hard when it's for instance in a static constructor.

这篇关于应用程序不会从命令行的完整路径启动,但会在CD到目录后启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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