1、首先设置该txt文件的属性,选择“复制到输出目录”为“如果较新则复制”或“始终复制”,以此保证会在bin/Debug目录下生成相应文件
2、如项目名称下包括文件夹test,而txt文件位于该文件夹内
string startPath=System.Windows.Forms.Application.StartupPath;//获取当前程序所在路径
startPath=System.IO.Path.Combine(startPaht,"test","Introduction.txt");//将两个路径名称连接起来
3、将读取的txt内容写入Word中
StreamReader sr=new StreamReader(startPath,Encoding.Default);
String line;
whilt((line=sr.ReadLine())!=null)
{
wordDoc.Paragraphs.Last.Range.Text=line;
wordDoc.Content.InsertAfter("\n");
}
即可完成从txt中读入字符串,并将其写入Word的功能。