What causes error with similar code?
I have the following code that extracts names from resumes. Please see the
code below:
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 problem with above code is that it generates some error messages like
the one below:-
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));
If I uncomment the currently commented lines, and comment the code between
"start here" and "end here", the code works fine. Why is it like that?
No comments:
Post a Comment