开发者

How to access file path for XML file in an assembly folder?

开发者 https://www.devze.com 2023-03-11 01:56 出处:网络
In my .NET assembly I need to access an XML file which is in \'Import\' folder off my class assembly project.

In my .NET assembly I need to access an XML file which is in 'Import' folder off my class assembly project.

How do I do this? Almost like ASP.NET Server.MapPath() but for an assembly. I don't want the reflection method of getting the currently executing assembly as this points to the GAC folder. I want the fold开发者_如何学Cer of the current project.

I also need this to work when being deployed. Basically it is importing an XML file and inserting the seed data using EF Code First database initializer. This is bootstrapped in the Global.asax.cs. I need it to work both in development and on live target.


  1. Visual Studio -> item (e.g. XML file) -> Properties -> Build Action -> Embedded Resource
  2. Assembly.GetExecutingAssembly().GetManifestResourceStream(path)


Add the path and/or filename to an app/web.config file and use the functionality in the core System.Configuration assembly to retrieve the value.


How about using: Environment.CurrentDirectory
Check out this question for more info.

Update:
For asp.net, you can use Request.PhysicalApplicationPath


you could use

HttpRuntime.AppDomainAppPath

http://msdn.microsoft.com/en-us/library/system.web.httpruntime.appdomainapppath.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消