How to access file path for XML file in an assembly folder?
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.
- Visual Studio -> item (e.g. XML file) -> Properties -> Build Action -> Embedded Resource
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
精彩评论