Winforms中的相对路径 [英] Relative Paths in Winforms

查看:290
本文介绍了Winforms中的相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相对路径的行为是扭曲我。在一种情况下林处理的一组的Texture2D对象到我的应用,其采取的文件名并使用此定位文件和纹理加载到图像对象。然后,我从存储在类文件中的相对路径加载图像和使用,需要是相对于内容/ GFX的相对路径。但是,如果我不加载这些纹理,这些相对路径将失败。我怎样才能garuantee我的相对路径不会失败?在网站工作的所有相对路径是相对于该文件夹我们从工作文件是,我可以将它设置这种方式,使所有相对路径到我的应用程序位于根文件夹?

Relative paths in C# are acting screwy for me. In one case Im handling a set of Texture2d objects to my app, its taking the filename and using this to locate the files and load the textures into Image objects. I then load an image from a relative path stored in the class file and use a relative path that needs to be relative to Content/gfx. But if i dont load these textures these relative paths will fail. How can I garuantee that my rel path wont fail? In web work all rel paths are relative to the folder the file we're working from is in, can I set it up this way and make all rel paths to root folder where my app is located?

推荐答案

我不建议使用相对路径摆在首位。

I recommend not using relative paths in the first place.

使用的 Path.Combine 把你的相对路径为绝对路径。例如,你可以用它来获取你的启动EXE的完整路径:

Use Path.Combine to turn your relative paths into absolute paths. For example, you can use this to get the full path to your startup EXE:

string exeFile = (new System.Uri(Assembly.GetEntryAssembly().CodeBase)).AbsolutePath;



一旦你有,你可以得到它的目录:

Once you have that, you can get it's directory:

string exeDir = Path.GetDirectoryName(exeFile);

和把你的相对路径绝对路径:

and turn your relative path to an absolute path:

string fullPath = Path.Combine(exeDir, "..\\..\\Images\\Texture.dds");

这会比尝试使用相对路径更可靠。

This will be much more reliable than trying to use relative paths.

这篇关于Winforms中的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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