相对路径问题 - 向上一个目录
这是工作: 我的 exe 与 Images 文件夹位于同一目录中;
Main:
|-Images
|-cross_ball
|-frame.bmp
|-game.exe
我指的是我的game.exe 中的frame.bmp。
这条路径工作正常:“Images\\cross_ball\\frame.bmp”
这不起作用: exe位于bin文件夹中。 bin 文件夹与 Images 文件夹位于同一文件夹中;
Main:
|-Images
|-cross_ball
|-frame.bmp
|-bin
|-game.exe
此路径不起作用:“..\\Images\\cross_ball\\frame.bmp”
This is workig:
I have my exe in the same directory as Images folder;
Main:
|-Images
|-cross_ball
|-frame.bmp
|-game.exe
I'm refering to frame.bmp in my game.exe.
This path is workig good: "Images\\cross_ball\\frame.bmp"
This is not working:
Exe is in bin folder. bin folder is in the same folder as Images folder;
Main:
|-Images
|-cross_ball
|-frame.bmp
|-bin
|-game.exe
This path is not working: "..\\Images\\cross_ball\\frame.bmp"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果“Images\cross_ball\frame.bmp”正在运行,那么您的应用程序的当前目录并不是您想象的那样的 Images 目录。它必须是一个目录才能工作。这也可以解释为什么将 exe 移动到 bin 目录失败。
您是从快捷方式还是在调试器中启动应用程序?快捷方式和调试器指定应用程序的当前目录,该目录不一定与 exe 所在的目录相同。
If "Images\cross_ball\frame.bmp" is working, then your app's current directory isn't the Images directory like you think it is. It must be one dir up for that to work. That would also explain why moving the exe to the bin directory fails.
Are you starting your app from a shortcut or in a debugger? Shortcuts and debuggers specify the app's current directory, which isn't necessarily the same directory the exe is in.
作为实验,将每个 \ 替换为 \\ 并再次测试。
反斜杠通常用于转义序列,如“\n”。
通过将其中两个放在一起,运行时应该将它们变成一个。
“图像\\cross_ball\\frame.bmp”
As an experiment, replace each \ by \\ and test again.
Back slash is generally used for escape sequences like '\n'.
By putting two of them together, the run time ought to change them into one.
"Images\\cross_ball\\frame.bmp"
看来你的逻辑是没问题的。您确定您的进程没有更改当前工作目录吗?即,当您在可执行文件中进行此更改时,Main/bin/ 可能不是您的工作目录。
It appears that your logic is fine. Are you sure your process hasn't changed the current working directory - i.e. Main/bin/ might not be your working directory when you go to make this change in the executable.