Answer the question
In order to leave comments, you need to log in
How to solve a problem with writing a file to an ASP.NET (IIS) application folder?
Hello.
Faced the following problem:
files stopped being written to the local folder of the application hosted on IIS.
Application architecture:
ASP.NET MVC 3 application, it has several WCF services. One of them just provides the functionality of downloading a file. The clint of this service is the silverlight application. It is loaded through it. The method inside the client asynchronously pulls the web service and gives it batches of data that should be written.
I added a new web service to the application, but after deploying the new version, this functionality stopped working. Reverting to an older version didn't help.
Locally everything is tested and works (both new and old versions).
With the help of logging, we managed to find out what the following piece of code does not execute:
void SendFile()
{
// какой-то код, действия которого можно отследить
//Режимы открытия надо будет уточнить, но они не менялись уже много лет
using (FileStream stream = File.Open(filename, FileMode.Append, FileAccess.Write, FileShare.Read))
{
// внуть using не передается управление, ничего нельзя отловить
try
{
//
}
catch (Exception ex)
{
//
}
}
}
Answer the question
In order to leave comments, you need to log in
Rewrote the code so that you can catch the error.
Threw : System.IO.DirectoryNotFoundException: Could not find a part of
the
path -not-fin...
support.microsoft.com/kb/827421
Solution:
Manually create a folder where the file will be written.
string dir = Path.GetDirectoryName(filePath);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
FileStream stream = null;
try {
stream = File.Open(filename, FileMode.Append, FileAccess.Write, FileShare.Read);
....
}
catch(Exception ... ) {
...
}
finally {
if(stream != null)
stream.Dispose()
}
You can try to deploy using and get an exception
FileStream stream = null;
try {
stream = File.Open(filename, FileMode.Append, FileAccess.Write, FileShare.Read);
....
}
catch(Exception ... ) {
...
}
finally {
if(stream != null)
stream.Dispose()
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question