移动时如何对资源的文件路径进行编程? [英] How to program filepath to resources when being moved?

查看:53
本文介绍了移动时如何对资源的文件路径进行编程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好.

在学校的.NET C#项目中,我为媒体播放器使用了插件.
我将于5月2日以CD形式提交我的项目.

名为插件"的文件夹位于我的项目文件夹中的资源文件夹下.
但是在Visual Studio中,在我的计算机上,我已经为该文件夹设置了绝对文件路径.
将其交到CD上时,传感器在计算机上将没有该路径
那么我该如何编程,以便自动在他的计算机上找到资源文件夹?

最好的问候,Trond:)

Hello.

In my .NET C# project at school, i use plugins for my media player.
May 2nd im going to hand in my project, on a CD.

The folder called "plugins" is under the resource-folder in my project folder.
But in visual studio, on my computer, i have set a absolute file path, to that folder.
When handing it in on a CD, sensor wont have that path on his computer
so how do i program so the resource folder is found on his computer automatically?

Best regards, Trond :)

推荐答案

您可以使用以下AppDomain.CurrentDomain.BaseDirectory获取应用程序的路径.
这样一来,您就可以访问应用程序目录中的文件,这些文件可以在计算机之间变化,并且您的应用程序可以正常运行而不会出现错误.
You can get the path of your application with the following AppDomain.CurrentDomain.BaseDirectory.

So with this you can access the files in your application directory, which can change from computer to computer, and your app will work fine without errors.


在任何情况下,硬编码路径都不会发生名称可以使用,绝对或相对.所有路径都必须在运行时根据用户数据的位置(您始终可以使用System.Environment.GetFolderPath(Environment.SpecialFolder)或从入口点程序集的主要可执行模块的位置(您在其中编写入口点方法)进行检索)进行计算. ,通常是Main函数).这是一种实现方法:
There are no situations where hard-coded path name can be used, absolute or relative. All paths have to be calculated during run time, based on the location of user data (which you can always retrieve using System.Environment.GetFolderPath(Environment.SpecialFolder) or from the location of the main executable module of your entry-point assembly (where you write you entry point method, normally the Main function). This is a way to do it:
exePath = System.IO.Path.GetDirectoryName(
    System.Reflection.Assembly.GetEntryAssembly().Location)


Mehdi在解决方案1中显示的解决方案也应该起作用.

请注意,有很多方法可以找到此位置,但是其中一些方法取决于额外的程序集或根据代码的托管方式提供结果(对于Windows Service应用程序或在调试器下,可能无法正常工作).上面显示的方法是通用的,并且在所有情况下都可以正常工作.

在通常情况下,可执行位置找到的所有资源都应该是只读的.读/写文件应仅按每个用户或所有用户"放置在数据目录中;请先参阅下面的两个参考文献.

请参阅:
http://msdn.microsoft.com/en-us/library/system.environment. getfolderpath.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.environment. specialfolder.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.reflection. assembly.aspx [^ ],
http://msdn.microsoft.com/en-us/library/system. io.path.getdirectoryname.aspx [ ^ ].

—SA


The solution shown by Mehdi in Solution 1 should also work.

Note that there are many methods to find this location, but some of them depends on extra assemblies or give the results depending on how the code is hosted (and may not work properly, for Windows Service application or under debugger). The method shown above is universal and works correctly in all cases.

All resources found by the executable location should be read-only, in general case. The read/write files should be placed only in the data directories per user or for "All users"; please see two first referenced below.

Please see:
http://msdn.microsoft.com/en-us/library/system.environment.getfolderpath.aspx[^],
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx[^],
http://msdn.microsoft.com/en-us/library/system.reflection.assembly.aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.path.getdirectoryname.aspx[^].

—SA


这篇关于移动时如何对资源的文件路径进行编程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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