2013-08-17 112 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

相關問題