启动时exe的当前目录与启动后exe的启动目录不同 [英] Current directory of exe at startup is different to when exe is started after startup

查看:110
本文介绍了启动时exe的当前目录与启动后exe的启动目录不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个文件夹,其中放置了我的可执行文件及其配置文件.我正在使用Windows XP.
当PC启动后,我通过双击exe来启动可执行文件时,它会找到配置文件.

我在注册表中添加了一个条目,以便在启动时启动我的可执行文件.从PC启动时启动时,找不到配置文件.我发现在这种情况下运行代码时当前目录为C:\Documents and settings\DEFAULT,但是当PC启动后我启动exe时,当前目录就是exe的运行目录,这正是我期望的. />
谁能告诉我为什么在启动exe时当前目录不同?有没有一种方法可以将当前目录设置为启动时exe所在的目录?

谢谢您的帮助.

I have created a folder in which I have put my executable and its config file. I am using Windows XP.
When I start the executable after the PC has already started up, by double-clicking on the exe, it finds the config file.

I have put an entry in the registry to start my executable at startup. When it starts up from PC startup, it does not find the config file. I have found that the current directory when the code is running in this case is C:\Documents and settings\DEFAULT however when I start the exe after the PC has started the current directory is where the exe is running from, which is what i would expect.

Can anyone please tell me why the current directory is different when the exe is started up at start-up? Is there a way to set the current directory to the directory that the exe is in at startup?

Thank you for any help.

推荐答案

exe的当前目录在启动时可以是任何目录.尝试以下操作:创建一个名为"X"的文件夹,然后将您的exe放在该文件夹中.进入控制台中的x文件夹,然后通过键入"X \ myprogram.exe"启动exe.在这种情况下,编的当前目录将是控制台当前目录的目录.要从exe所在的目录中获取配置文件,可以执行以下操作之一:
-在控制台程序中,argv [0]在我的示例中将为"X \ myprogram.exe",因此您可以剥离文件名并将配置文件名附加到"X \",这样将导致"X \ myprogram.cfg" .
-在Windows上,调用GetModuleFilename(NULL)会返回exe的完整路径,以便您可以更改文件名,并获得配置文件的完整路径.
The current directory of your exe can be anything when you start it. Try this: create a folder called "X" and put your exe inside this folder. go outside this x folder in your console and start your exe by typing in "X\myprogram.exe". in this case the current directory of your prog will be the directory that was the current dir of your console. To get your config file from the directory your exe is located you can do one of these:
- In a console program argv[0] will be "X\myprogram.exe" in my example so you can strip off the filename and append the config filename to the "X\" so it will result in "X\myprogram.cfg".
- On Windows call GetModuleFilename(NULL) that gives back the full path of your exe so you can change the filename and you get a full path to your config file.


这篇关于启动时exe的当前目录与启动后exe的启动目录不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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