Reading notepad is common and difficult. As follows:
/// <Summary>
/// Read notepad
/// </Summary>
/// <Param name = "path"> </param>
/// <Returns> </returns>
Private string Txt2Text (string path)
{
StreamReader srFile = null;
String msg = string. Empty;
Try
{
SrFile = new StreamReader (path, System. Text. Encoding. Default );
Msg = srFile. ReadToEnd ();
}
Catch (Exception ex)
{
Msg = ex. Message;
}
Finally
{
If (srFile! = Null)
{
SrFile. Dispose ();
SrFile. Close ();
}
}
Return msg;
}
I have referred to relevant materials for processing Word documents. I think the following methods are better:
First add reference: Microsoft. Office. Interop. Word;
/// <Summary>
/// Read the word document
/// </Summary>
/// <Param name = "docFileName"> </param>
/// <Returns> </returns>
Public string Doc2Text (string docFileName)
{
# Region
String msg = string. Empty;
Microsoft. Office. Interop. Word. ApplicationClass wordApp = null;
Microsoft. Office. Interop. Word. Document doc = null;
Object fileobj = null;
Object nullobj = null;
Try
{
// C # reading word file instantiation COM
WordApp = new Microsoft. Office. Interop. Word. ApplicationClass ();
Fileobj = docFileName;
Nullobj = System. Reflection. Missing. Value;
// Open the specified file (the number of COM parameters varies in different versions. Generally, nullobj is used except the first one)
Doc = wordApp. documents. open (ref fileobj, ref nullobj, ref upload, ref nullobj, ref nullobj, ref nullobj, ref nullobj );
// Obtain the text in the doc file
Msg = doc. Content. Text;
}
Catch (Exception ex)
{
Msg = ex. Message;
}
Finally
{
If (doc! = Null)
// C # Close A word file
Doc. Close (ref nullobj, ref nullobj, ref nullobj );
If (wordApp! = Null)
// C # Close COM when reading word Files
WordApp. Quit (ref nullobj, ref nullobj, ref nullobj );
}
// Return
Return msg;
# Endregion
}