2013-08-17 32 views
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) 
      { 

      } 
     } 

与上面的代码的问题是,它会产生类似下面的一些错误消息: -

The process cannot access the file 'C:\Users\Roshan\Documents\Visual Studio 2012\Projects\HRMS\HRMS\resumes\Roshan.doc' because it is being used by another process.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.IOException: The process cannot access the file 'C:\Users\Roshan\Documents\Visual Studio 2012\Projects\HRMS\HRMS\resumes\Roshan.doc' because it is being used by another process.

Source Error:

Line 73: { Line 74: string path = Server.MapPath(Request.ApplicationPath) + "/resumes/" + fileUpload1.FileName; Line 75:
fileUpload1.SaveAs(path); Line 76: Line 77:
fileUpload1.SaveAs(Server.MapPath("~/resumes/" + filename));

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

+0

您是否在Word中打开了.doc文件?如果你关闭它,是否会发生同样的事情? –

+0

该文件未打开。如果我取消对当前注释行的注释,而是取消注释“在此处开始”和“在此结束”之间的代码 – user2625672

+0

,那么它将工作得很好。使用TaskManager在启动之前终止所有正在运行的Word实例。当您调试并半途停止该程序时,您的文件保持打开状态。 –

回答

0

有多条路径允许您的代码运行一次而无需到达doc.close语句。因此,第二次电话会触发您得到的例外情况。

你可以尝试把doc.close语句放在finally块中吗(反正这是个好习惯)?

+0

将做临时工,并发布过期 – user2625672

+0

将其添加到finally块中说,“doc”在当前上下文中不存在......换句话说,“doc”首先在try块内部使用 – user2625672

+0

声明doc变量在try块之外设置为空,并在try语句中为其赋值 – Marshall777

相关问题