0

我有以下代码从简历中提取姓名。请看下面的代码:

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\resumes\Roshan.doc”,因为它正被另一个进程使用。

说明:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

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

源错误:

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

如果我取消注释当前注释的行,并在“从这里开始”和“从这里结束”之间注释代码,则代码可以正常工作。为什么会这样?

4

1 回答 1

0

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

您能否尝试将 doc.close 语句放在 finally 块中(无论如何这是一个好习惯)?

于 2013-08-17T18:28:56.847 回答