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

시작하기 전에 실행중인 모든 Word 인스턴스를 종료하려면 TaskManager를 사용하십시오. 이 프로그램을 디버그하고 중간에 중지하면 파일이 열려 있습니다. –

답변

0

코드가 doc.close 문에 도달하지 않고 한 번 실행될 수있는 다중 경로가 있습니다. 따라서 두 번째 호출로 인해 예외가 발생할 수 있습니다.

finally 블록 안에 doc.close 문을 넣으시겠습니까? (어쨌든 좋은 연습입니다)?

+0

그쪽에서 할 것이고, udates를 게시 할 것입니다. – user2625672

+0

finally 블록에 추가하면 "doc"이 현재 컨텍스트에 존재하지 않습니다. 즉, "doc"가 try 블록 내에서 처음 사용되었습니다. – user2625672

+0

Declare the doc 변수를 try 블록 외부로 설정하여 null로 설정하고 try 문 내부에 값을 지정하십시오. – Marshall777

관련 문제