The process cannot access the file "" because it is being used by another process

I am trying to finalize a project and have run into a problem which I have found numerous complaints about online, but have not been able to resolve.

The application is web drive and permits the owner of the site to create small websites for employees. this seems to work fine, with the exception in the subject line. I check to see if the top level folder exsists, if it doesn't, I begin creating the folders, once done, I upload any images to their proper location and finally I create a empty  file which is included in the default.asp page. This empty file is necessary because it allows the employee to access a private area and use a content editor to add personal information.

The problem is once the file is create, i.e, and you try to open the web page, you receive the following message:
The process cannot access the file "" because it is being used by another processThe process cannot access the file "" because it is being used by another process

You cannot even open it through ftp. And it continues this way until IIS is restarted, which obviously is not a concideration.

I will post some code to show how this file is being made in the hopes that I can get some advice.

Thanks in advance

----------  Code below ----------------

private void CreateDefaultPage(string completepath)
                  string intuserid = lblUserid.Text.Trim();
                  string strfirstname = txtFirstName.Text.Trim();
                  string strlastname = txtLastName.Text.Trim();
                  string straddress = txtFirstName.Text.Trim();
                  string strtitle = txtTitle.Text.Trim();
                  string strphoto = "";

                  foreach( DataRow dr in functions.GetUserDetails(intuserid).Tables[0].Rows )
                        strphoto = dr["employee_picture"].ToString();

                  string strhost = Request.ServerVariables["HTTP_HOST"];
                  string strurl = "http://" + strhost;
                  string strinstructors =  strfirstname + "_" + strlastname;

                  //set deault information
                  string strcourses = "Not Defined Yet";
                  string strdescription = txtDescription.Text.Trim().Replace("\n","<br>");

                  TextWriter tw = new StreamWriter(completepath  + "\\default.asp");
                  tw.WriteLine("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" >");
                  tw.WriteLine("<TITLE>strfirstname + " " + strlastname + "</TITLE>");
                  tw.WriteLine("<META NAME=\"GENERATOR\" Content=\"Microsoft Visual Studio 7.0\">");
                  tw.WriteLine("<TABLE id=\"Table2\" cellSpacing=\"3\" cellPadding=\"0\" width=\"778\" border=\"0\" style=\"COLOR: black; FONT-FAMILY: Arial\">");
                  tw.WriteLine("<td colspan=\"3\"><IMG alt=\"\" src=\"" + strurl + "/images/top_bar.jpg\"></td>");
                  tw.WriteLine("<td style=\"BACKGROUND-IMAGE: url(" + strurl + "/images/left_bar.jpg)\"></td>");
                  tw.WriteLine("<TABLE cellSpacing=\"3\" cellPadding=\"0\" width=\"778\" border=\"0\"");
                  tw.WriteLine("<TD style=\"WIDTH: 486px\" rowspan=\"2\" valign=\"top\">");      
                  tw.WriteLine("<span id=\"_ctl1_lblCourses\" class=\"title\">Courses:</span><BR>");
                  tw.WriteLine("<span id=\"_ctl1_txtCourseList\" class=\"admintext\">" + strcourses + "</span><BR><BR>");
                  tw.WriteLine("<span id=\"_ctl1_lblDescription\" class=\"title\">Description:</span><BR>");
                  tw.WriteLine("<P><TABLE style=\"WIDTH: 431px; HEIGHT: 135px\" cellSpacing=1 cellPadding=1 border=0><TBODY><TR><TD><FONT face=Arial size=2>" + strdescription + "</FONT></TD></TR></TBODY></TABLE></P>");            

                  //tw.WriteLine("<P><!--#include file=\"\" --></P>");

                  tw.WriteLine("<script runat=\"server\">");
                  tw.WriteLine("public string GetPage()");
                  tw.WriteLine("string filename = \"\";");
                  tw.WriteLine("System.IO.StreamReader SR;");
                  tw.WriteLine("string S;");
                  tw.WriteLine("return S;");                                                      
            tw.WriteLine("<%= GetPage() %>");

                  tw.WriteLine("<TD align=\"center\" valign=\"top\">");
                  tw.WriteLine("<P><IMG alt=\"\" src=\"" + strurl + "/images/CIT_LOGO_252.gif\"></P>");
                  tw.WriteLine("<input type=\"image\" src=\"" + strurl + strinstructors + "/images/" + strphoto + "\" alt=\"\" border=\"0\" /><BR>");
                  tw.WriteLine("<span id=\"_ctl1_txtTitle\" class=\"title\">" + strtitle + "</span><BR>");
                  tw.WriteLine("<span id=\"_ctl1_txtFullName\" class=\"title\">" + strfirstname + " " + strlastname + "</span></P>");
                  tw.WriteLine("<P>Click on any of the links below to download the appropriate documents:</P></TD>");
                  tw.WriteLine("<TD valign=\"top\">");
                  tw.WriteLine("<td style=\"BACKGROUND-IMAGE: url(" + strurl + "images/right_bar.jpg)\"></td>");
                  tw.WriteLine("<td colspan=\"3\"><IMG alt=\"\" src=\"" + strurl + "/images/bottom_bar.jpg\"></td>");

                  //check to see if the file is in the directory and or create it as needed

                  TextWriter tw2 = new StreamWriter(completepath  + "\\");


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Looking at your code, its hard to see a problem with it. You use a StreamWriter (TextWriter) to put the data into the file, and you call Close(). For a StreamWriter this is the same as calling Flush() and then Dispose(), so you are doing everything that I know of that you should be doing with the StreamWriter once you are finished with it.

I would maybe think about running something like ProcessExplorer (free from sysinternals) on your server, creating a website and seeing what process has a lock on that troublesome file, also see if it has a lock on the asp file.

Other than that, I am clean out of ideas.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
maybe you will have to change the acces rights first on that file to normal?
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

My suggestion is that, since the asker didn't post any comments, the points be split equally between all participating experts and the question PAQed
CJSantoraAuthor Commented:
Good Morning all, I was not trying to ignore anyone and always appreciate the help, howeveer there was not answer that useful in this case and I was forced to find another alternative. And yes I dod abandon the question and move on with other work, so my apologies for not keepingyou in the loop, but I never received any other suggestions either.

Thank You for your help
CJSantora ,

If you did get a solution, please share it here as it is useful for archiving purposes.

Also, one tends to get solutions to problems by always replying to experts as to whether or not their suggestions worked because other experts can then have a go.  If a suggestion is provided but the asker didn't reply then it is assumed that the asker hasn't tried the suggestion yet and so experts maybe put off.

Now you are here, I don't really mind what happens to the points, it is up to the moderator.

CJSantoraAuthor Commented:
Once again I offer my apologies and your point is well taken. I use this site often and would like to see the site continue to  prosper as it has helped me many times. I will take your advice. Unfortunately the project above is closed at our facility ad I do not remember the fix at this time. I have posted code where I found an alternative to my problem and even received better solutions as a result and will continue to do so in the future.

Thanks for the feedback CJSantora as well as your polite manner.  Let admins know what you would like to do with this question and they will sort it out, they are a friendly bunch ;-)
CJSantoraAuthor Commented:
Please split the point to all that attemtped to help - and Thank You
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.