tags:

views:

73

answers:

4

I have an assembly that contains a function that could be called from IIS or from a console app.

Because of this I have opted for the following to get the path:

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)

obviously this will return the bin directory in the case of the function being called from IIS.

I intend to create a txt file on this path. Is it such a bad idea to have text files sitting in the bin directory? Please give valid objections (if any) why this could cause problems.

+2  A: 

You simply may not have enough permissions to write to this directory. Try using TEMP folder instead (think Path.GetTempPath()).

Anton Gogolev
Testing permissions now....
JL
Ideally, using the System.IO.Path.GetTempPath() property.
Zhaph - Ben Duguid
Actually this is a great suggestion, only thing I don't like is that the temp directory is an obscure location and also subject to external forces (like admins cleaning it out)
JL
+2  A: 

If you delete/copy/overwrite files in "bin" directory, the IIS will restart processes - because IIS thinks the application was changed, so new requestes should be proceeded with "new" appliaction.

So, yes it is bad idea.

TcKs
indeed, so ANY change to any file in there will cause an IIS reset?
JL
I thought it was just changes to DLLs and web.config
Tim Robinson
Any file causing IIS reset - because the assemblies can has any extensions or no extension or the file is not assembly, but can be part of application.
TcKs
+5  A: 

Assembly.Location can return surprising results, e.g. if shadow copying is turned on (for instance, when running through NUnit), or when running from a location not on the local file system (you can run .NET apps over HTTP).

AppDomain.CurrentDomain.BaseDirectory is a safer option, as it returns the original path prior to shadow copying (although in the case of HTTP deployment I think it returns the directory where ieexec.exe is located).

The safest option is to embed whatever data you need as a resource within the assembly, and use Assembly.GetManifestResourceStream to access it at runtime.

Tim Robinson
+1  A: 

Tim Robinson's got the right idea.

The only other thing I should say is if ASP.NET is writing the file, be careful with your threading code; maybe two requests for the same page will try to write the same file at the same time, so just be careful to lock your writes to the file.

Steve Cooper