C# Winforms 中的相对路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1019641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Relative Paths in Winforms
提问by
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?
C# 中的相对路径对我来说很奇怪。在一种情况下,我将一组 Texture2d 对象处理到我的应用程序,它采用文件名并使用它来定位文件并将纹理加载到 Image 对象中。然后我从存储在类文件中的相对路径加载图像,并使用需要相对于 Content/gfx 的相对路径。但是如果我不加载这些纹理,这些相对路径就会失败。我如何保证我的 rel 路径不会失败?在网络工作中,所有 rel 路径都相对于我们正在工作的文件所在的文件夹,我可以这样设置并将所有 rel 路径设置为我的应用程序所在的根文件夹吗?
采纳答案by Reed Copsey
I recommend not using relative paths in the first place.
我建议首先不要使用相对路径。
Use Path.Combineto turn your relative paths into absolute paths. For example, you can use this to get the full path to your startup EXE:
使用Path.Combine将您的相对路径转换为绝对路径。例如,您可以使用它来获取启动 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.
这比尝试使用相对路径要可靠得多。
回答by jjxtra
If you are expecting a resource to be in the same directory as the executable file or in a sub directory of that directory, it's best to always use
如果您希望资源与可执行文件位于同一目录中或位于该目录的子目录中,则最好始终使用
string fullPath = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), subPath);
or if you are worried that the working directory might be wrong you can do this:
或者如果您担心工作目录可能有误,您可以这样做:
string fullPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location), subPath);
回答by Suhaib ghrear
// [name space] is name space //you should copy your file.-- into Depug file
// [命名空间] 是命名空间//你应该复制你的文件。-- 到Depug 文件中
string path = (Assembly.GetEntryAssembly().Location + "");
path = path.Replace("name space", "filename.--");
// [WindowsFormsApplication4] is name space
//you should copy your "mysound.wav" into Depug file
//example::
string path_sound = (Assembly.GetEntryAssembly().Location + "");
path_sound = path_sound.Replace("WindowsFormsApplication4.exe", "mysound.wav");
SoundPlayer player1 = new SoundPlayer(path_sound);
player1.Play();