We help IT Professionals succeed at work.
Get Started

c#, file IO issue

ITsolutionWizard
on
485 Views
Last Modified: 2019-01-28
I see alert message "Process cannot access the file because it is being used by another process".
when I click the button at third time. It seems reading is okay. but when i click the button to write something inside  to the xml.
it fails. how can i resolve this issue?

I know the  "Process cannot access the file because it is being used by another process". continue to happen the new message is sent.
sendMsg_Click(), but that does not mean the issues came from there.

And I check all controllers I used, and when the file is opened, it is closed immediately.

  protected void Page_Load(object sender, EventArgs e)
        {
            phoneNoList.DataSource = Utility.Controller.GetUniquePhoneNo(fromPhoneNo);
            phoneNoList.DataBind();
}

  protected void sendMsg_Click(object sender, EventArgs e)
        {
		 #region add a record to all message xml
                Domain.User u = new Domain.User();
                u.fullName = fullName.Text;
                u.from = "+1" + fromPhoneNo;
                u.to = "+1" + toPhoneNo.Text;
                u.body = body.Text;
                u.dateTimeSent = DateTime.Now.ToString();
                u.direction = "outbound-reply";
                string result = Utility.XML.Add(Utility.Controller.filePathUserMessagesAll, u);
                #endregion


}

  public static string Add(string inputFilePath,Domain.User u)
        {
            XDocument doc = XDocument.Load(inputFilePath);
            XElement user = new XElement("User",
                new XAttribute("PhoneNo",""),
                new XElement("FullName",u.fullName),
                new XElement("From",u.from),
                new XElement("To",u.to),
                new XElement("Body",u.body),
                new XElement("DateTimeSent",u.dateTimeSent),
                new XElement("Direction",u.direction),
                new XElement("Carrier",
                      new XElement("Name","Twilio"),
                      new XElement("SID",Utility.Controller.TwilioSID),
                      new XElement("Token",Utility.Controller.TwilioToken)));
            doc.Root.Add(user);
            doc.Save(inputFilePath);
            return "Added";
        }

    public static DataTable GetUniquePhoneNo(string accountPhoneNo)
        {
            string clientPhoneNo = string.Empty;
            string inputFilePath = filePathUserMessagesAll;
                //@"C:\Users\Documents\visual studio 2015\Projects\AppText\AppText\Data\UserMessagesAll.xml";
            DataTable dt = new DataTable();
            dt.Clear();
            dt.Columns.AddRange(new DataColumn[]
           {
                new DataColumn("PhoneNo",typeof(string)),
                new DataColumn("Client",typeof(string)),
                new DataColumn("DateCreated",typeof(string))
           });
            XDocument xdoc = null;
            using (XmlReader xr = XmlReader.Create(inputFilePath))
            {
                xdoc = XDocument.Load(xr);
                var query1 = from t in xdoc.Descendants("User")
                             where t.Element("From").Value.ToLower() == "+1" + accountPhoneNo.ToLower()
                             select new
                             {
                                 To = t.Element("To").Value,
                                 FullName = t.Element("FullName").Value,
                                 DateTimeSent = t.Element("DateTimeSent").Value
                                 //DateTime.Parse(txtDatepicker_end.Text).ToString("YYYY-MM-dd");
                             };
                var query2 = from t in xdoc.Descendants("User")
                             where t.Element("To").Value.ToLower() == "+1" + accountPhoneNo.ToLower()
                             select new
                             {
                                 To = t.Element("From").Value,
                                 FullName = t.Element("FullName").Value,
                                 DateTimeSent = t.Element("DateTimeSent").Value
                             };
                var query3 = query1.Concat(query2).OrderByDescending(z => z.DateTimeSent).GroupBy( z => z.To).Select(z=> z.First());
                foreach (var x in query3)
                {
                    dt.Rows.Add(x.To, x.FullName, (DateTime.Parse(x.DateTimeSent)).ToString("MM/dd/yy", System.Globalization.CultureInfo.InvariantCulture));
                }

               // string timeF = "2017-06-15T14:49:19-07:00";
               // DateTime dt = DateTime.Parse(timeF);
               // string timeS = dt.ToUniversalTime().ToString("MM/dd/yy hh:mm", System.Globalization.CultureInfo.InvariantCulture);


                // var messages2a = MessageResource.Read().Where(x => x.From.ToString() == "+1" + fromPhoneNo).GroupBy(x => x.To).Select(y => y.First());
                // var messages3a = MessageResource.Read().Where(y => y.To == "+1" + fromPhoneNo).GroupBy(x => x.To).Select(y => y.First());
                // var allz = messages2a.Concat(messages3a).OrderByDescending(z => z.DateCreated);
                return dt;
            }
        }

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Top Expert 2010
Commented:
This problem has been solved!
Unlock 1 Answer and 58 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE