• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 339
  • Last Modified:

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 whatever.inc, and you try to open the web page, you receive the following message:
The process cannot access the file "freestyle.inc" because it is being used by another processThe process cannot access the file "whatever.inc" 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
CJSantora

----------  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("<HTML>");
                  tw.WriteLine("<HEAD>");
                  tw.WriteLine("<TITLE>strfirstname + " " + strlastname + "</TITLE>");
                  tw.WriteLine("<META NAME=\"GENERATOR\" Content=\"Microsoft Visual Studio 7.0\">");
                  tw.WriteLine("</HEAD>");
                  tw.WriteLine("<BODY>");
                  tw.WriteLine("<TABLE id=\"Table2\" cellSpacing=\"3\" cellPadding=\"0\" width=\"778\" border=\"0\" style=\"COLOR: black; FONT-FAMILY: Arial\">");
                  tw.WriteLine("<tr>");
                  tw.WriteLine("<td colspan=\"3\"><IMG alt=\"\" src=\"" + strurl + "/images/top_bar.jpg\"></td>");
                  tw.WriteLine("</tr>");
                  tw.WriteLine("<tr>");
                  tw.WriteLine("<td style=\"BACKGROUND-IMAGE: url(" + strurl + "/images/left_bar.jpg)\"></td>");
                  tw.WriteLine("<td>");
                  tw.WriteLine("<TABLE cellSpacing=\"3\" cellPadding=\"0\" width=\"778\" border=\"0\"");
                  tw.WriteLine("<TR>");
                  tw.WriteLine("<TD style=\"WIDTH: 486px\" rowspan=\"2\" valign=\"top\">");      
                  tw.WriteLine("<P>");
                  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=\"whatever.inc\" --></P>");

                  tw.WriteLine("<script runat=\"server\">");
                  tw.WriteLine("public string GetPage()");
                  tw.WriteLine("{");
                  tw.WriteLine("string filename = \"\";");
                  tw.WriteLine("System.IO.StreamReader SR;");
                  tw.WriteLine("string S;");
                  tw.WriteLine("SR=System.IO.File.OpenText(Server.MapPath(\"freestyle.inc\"));");
                  tw.WriteLine("S=SR.ReadLine();");
                  tw.WriteLine("while(S!=null)");
                  tw.WriteLine("{");
                  tw.WriteLine("Response.Write(S);");
                  tw.WriteLine("S=SR.ReadLine();");
                  tw.WriteLine("}");
                  tw.WriteLine("SR.Close();");
                  tw.WriteLine("return S;");                                                      
                  tw.WriteLine("}");            
            tw.WriteLine("</script>");
            tw.WriteLine("<%= GetPage() %>");

                  tw.WriteLine("</TD>");
                  tw.WriteLine("<TD align=\"center\" valign=\"top\">");
                  tw.WriteLine("<P><IMG alt=\"\" src=\"" + strurl + "/images/CIT_LOGO_252.gif\"></P>");
                  tw.WriteLine("<P>");
                  tw.WriteLine("<input type=\"image\" src=\"" + strurl + strinstructors + "/images/" + strphoto + "\" alt=\"\" border=\"0\" /><BR>");
                  tw.WriteLine("<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("");
                  tw.WriteLine("<P>Click on any of the links below to download the appropriate documents:</P></TD>");
                  tw.WriteLine("</TR>");
                  tw.WriteLine("<TR>");
                  tw.WriteLine("<TD valign=\"top\">");
                  tw.WriteLine("<P>&nbsp;</P>");
                  tw.WriteLine("");
                  tw.WriteLine("<P>&nbsp;</P>");
                  tw.WriteLine("</TD>");
                  tw.WriteLine("</TR>");
                  tw.WriteLine("</TABLE>");
                  tw.WriteLine("</td>");
                  tw.WriteLine("<td style=\"BACKGROUND-IMAGE: url(" + strurl + "images/right_bar.jpg)\"></td>");
                  tw.WriteLine("</tr>");
                  tw.WriteLine("<tr>");
                  tw.WriteLine("<td colspan=\"3\"><IMG alt=\"\" src=\"" + strurl + "/images/bottom_bar.jpg\"></td>");
                  tw.WriteLine("</tr>");
                  tw.WriteLine("</TABLE>");
                  tw.WriteLine("</BODY>");
                  tw.WriteLine("</HTML>");
                  tw.Close();

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

                  TextWriter tw2 = new StreamWriter(completepath  + "\\freestyle.inc");
                  tw2.WriteLine("&nsbp;");
                  tw2.Close();

            }










 
0
CJSantora
Asked:
CJSantora
3 Solutions
 
AGBrownCommented:
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.

Andy
0
 
vo1dCommented:
maybe you will have to change the acces rights first on that file to normal?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
gbzhhuCommented:
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
0
 
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
0
 
gbzhhuCommented:
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.

Hassan
0
 
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.

CJSantora
0
 
gbzhhuCommented:
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 ;-)
0
 
CJSantoraAuthor Commented:
Please split the point to all that attemtped to help - and Thank You
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now