C# 是什么导致了类似代码的错误?

C# 是什么导致了类似代码的错误?,c#,asp.net,C#,Asp.net,我有以下从简历中提取姓名的代码。请参阅下面的代码: public void name(string str1) { try { Microsoft.Office.Interop.Word.ApplicationClass Application = new Microsoft.Office.Interop.Word.ApplicationClass(); object

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

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)
            {

            }
        }
公共无效名称(字符串str1)
{
尝试
{
Microsoft.Office.Interop.Word.ApplicationClass Application=新的Microsoft.Office.Interop.Word.ApplicationClass();
对象nullobj=System.Reflection.Missing.Value;
字符串a=Server.MapPath(“/resumes/”+fileUpload1.FileName);
fileUpload1.SaveAs(Server.MapPath(“/resumes/”+fileUpload1.FileName));
对象文件=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();
字符串Doc_Content=Doc.Content.Text;
字符串str=文档内容;
var words=str.Split(新字符[]{',':','\r','\t'});
for(int i=0;i
上述代码的问题在于,它会生成一些错误消息,如下所示:-

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

描述:在执行过程中发生未处理的异常 当前的web请求。请查看堆栈跟踪以了解更多信息 有关错误的信息及其在代码中的来源

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

源错误:

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


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

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


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

是否用Word打开了.doc文件?如果保持关闭,是否会发生同样的情况?文件未打开。