For various security reasons we implemented an assembly that requires the presence of a text file along with it. We would like to use this library in our ASP.NET application. The problem we're running into is that when the application starts up the loading of our assembly fails because the required text file is not in the Temporary ASP.NET files subdirectory where the assembly is loaded from. Is there a way to configure the assembly to be loaded from the bin directory? If not is there a way to predict where the dll will go and write a script to manually copy our file there?
Asked
Active
Viewed 719 times
2 Answers
0
I think by including that text file to your project will solver your problem.
Here is what you need to
- Add Text to your project by Add-Existing item
- Right click on Textfile and select Build Action as "Content"
- Select Copy to output directory as "Copy Always"

Nishant
- 306
- 1
- 4
0
The problem is certainly the way you are resolving the current folder to load your text file in your assembly.
You should use the AppDomain.CurrentDomain.BaseDirectory
property to get your IIS root folder.
If your text file is already in your Web Application bin
folder, combine :
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin");
You can use the AppDomain.CurrentDomain.RelativeSearchPath
property to be clean.
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, AppDomain.CurrentDomain.RelativeSearchPath);

JoeBilly
- 3,057
- 29
- 35
-
I wish I had come back to check on your answer sooner. I think that solution might work. What about a console app situation, or windows app situation. Would it still work in that instance too? – Colin Dec 17 '13 at 17:16
-
Is there any way I could do something that tells IIS to copy the text file into the location of the assembly at dynamic compile time? That way the text file would already be alongside the assembly when i call Assembly.GetExecutingAssembly().Location from within the assembly itself. – Colin Dec 17 '13 at 17:23
-
About a console or windows application, the good thing is the `AppDomain` is related to where the application is present. So the `BaseDirectory` is right in each case, unlike the `Environment.CurrentDirectory`. – JoeBilly Dec 18 '13 at 16:30
-
For your second comment, IIS by itself is not able and not supposed to do something with your binaries when your application start. It is a hoster. If you want to strongly link you files with your assemblies, you can [embed your files](http://stackoverflow.com/questions/433171/how-to-embed-a-text-file-in-a-net-assembly) instead of putting them appart. In this case, your text files will be [IN your assemblies](http://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file) and will not be presents in your folder (not modifiable, not readable...). – JoeBilly Dec 18 '13 at 16:36