什么导致类似代码的错误?

时间:2013-08-17 17:53:00

标签: c# asp.net

我有以下代码从简历中提取名称。请参阅以下代码:

public void name(string str1)
        {
            try
            {

                Microsoft.Office.Interop.Word.ApplicationClass Application = new Microsoft.Office.Interop.Word.ApplicationClass();
                object nullobj = System.Reflection.Missing.Value;
                string a = Server.MapPath("/resumes/" + fileUpload1.FileName);
                fileUpload1.SaveAs(Server.MapPath("/resumes/" + fileUpload1.FileName));
                object file = Server.MapPath("/resumes/" + fileUpload1.FileName);
                Microsoft.Office.Interop.Word.Document doc = Application.Documents.Open(ref file, ref nullobj, ref nullobj,
                                                  ref nullobj, ref nullobj, ref nullobj,
                                                  ref nullobj, ref nullobj, ref nullobj,
                                                  ref nullobj, ref nullobj, ref nullobj,
                                                  ref nullobj, ref nullobj, ref nullobj, ref nullobj);
                doc.Activate();
                string Doc_Content = doc.Content.Text;
                string str = Doc_Content;
                var words = str.Split(new char[] { ' ', ':', '\r', '\t' });

                for (int i = 0; i < words.Length; i++)
                {
                    string val1 = words[i].ToString();
                    val1 = val1.ToLower();
                   // string val2 = "";

                    //if (val1 != "resume")
                    //{
                    //    //i = i + 1;
                    //    string val3 = words[i].ToString();
                    //    string val4 = "";
                    //    int result = string.Compare(val3, val4, true);
                    //    if (result != 0)
                    //    {
                    //        if (j == 0)
                    //        {
                    //            string val5 = words[i].ToString();
                    //            j++;

                    //            if (words[i + 1].ToString() != "")
                    //            {
                    //                TextBox1.Text = words[i].ToString() + " " + words[i + 1].ToString();
                    //                //txtLastName.Text = words[i + 1].ToString();
                    //                doc.Close(ref nullobj, ref nullobj, ref nullobj);
                    //                return;
                    //            }
                    //            else
                    //            {
                    //                //txtLastName.Text = words[i + 2].ToString();
                    //                doc.Close(ref nullobj, ref nullobj, ref nullobj);
                    //                return;
                    //            }
                    //        }
                    //    }
                    //}


//start here


                    if (words[i].ToString().ToLower() == "resume")
                    {
                        string val3 = words[i + 1].ToString();
                        string val4 = words[i + 2].ToString();
                        TextBox1.Text = val3 + " " + val4; doc.Close(ref nullobj, ref nullobj, ref nullobj);
                        return;
                    }
                    else if (words[i].ToString().ToLower() == "curriculum")
                    {
                        if (words[i + 1].ToString().ToLower() == "vitae")
                        {
                            string val3 = words[i + 2].ToString();
                            string val4 = words[i + 3].ToString();
                            TextBox1.Text = val3 + " " + val4; doc.Close(ref nullobj, ref nullobj, ref nullobj);
                            return;
                        }
                    }
                    else
                    {
                        string val3 = words[i].ToString();
                        string val4 = words[i + 1].ToString();
                        TextBox1.Text = val3 + " " + val4; doc.Close(ref nullobj, ref nullobj, ref nullobj);
                        return;
                    }     
                }
 //end here
                doc.Close(ref nullobj, ref nullobj, ref nullobj);

            }
            catch (Exception)
            {

            }
        }

上面代码的问题在于它会生成一些错误消息,如下所示: -

  

进程无法访问文件'C:\ Users \ Roshan \ Documents \ Visual   Studio 2012 \ Projects \ HRMS \ HRMS \恢复\ Roshan.doc',因为它正在   被另一个过程使用。

     

描述:执行期间发生了未处理的异常   当前的网络请求。请查看堆栈跟踪了解更多信息   有关错误的信息以及它在代码中的起源。

     

异常详细信息:System.IO.IOException:进程无法访问   文件'C:\ Users \ Roshan \ Documents \ Visual Studio   2012 \ Projects \ HRMS \ HRMS \恢复\ Roshan.doc',因为它正在被使用   通过另一个过程。

     

来源错误:

     

第73行:{第74行:字符串路径=   Server.MapPath(Request.ApplicationPath)+“/ resume /”+   fileUpload1.FileName;第75行:
  fileUpload1.SaveAs(路径);第76行:第77行:
  fileUpload1.SaveAs(Server.MapPath(“〜/ resumes /”+ filename));

如果我取消注释当前注释的行,并在“start here”和“end here”之间注释代码,代码工作正常。为什么会那样?

1 个答案:

答案 0 :(得分:0)

有多个路径允许您的代码运行一次而不会到达doc.close语句。因此,第二次调用可能会触发您获得的异常。

您是否可以尝试将doc.close语句放在finally块中(这仍然是一种很好的做法)?