以.txt格式保存文件(记事本)

时间:2014-10-10 15:05:40

标签: c# filestream notepad

我想知道你是否可以帮我解决这个问题。我有DataGridView,上面保存了几本书。当我点击一本书时,该书的故事会加载到RichTextBox。当我更改该文本并想要保存它时,我想将内容保存在.txt文件中。我怎么能去做呢?

我已经为您提供了代码,以便了解我想要做的事情。

string FileLine = "";
StreamWriter OutputFile;
foreach (Book book in myBookList)
{
    //Book Book = new Book();
    Book nBook = myBookList[dgv.CurrentCell.RowIndex];
    //PROBLEM IS HERE
    OutputFile = new StreamWriter(nBook.TxtFileName);
    //-------------------------------------------------------------
    //I want it to be something like this:  {nBook.TxtFileName}.txt
    //-------------------------------------------------------------
    //Code to write data to file
    OutputFile.WriteLine(FileLine);
    OutputFile.Close();
}

由于 :)

2 个答案:

答案 0 :(得分:2)

这有用吗?我希望如此。

OutputFile = new StreamWriter(string.format("{0}.txt", nBook.TxtFilename));

答案 1 :(得分:-1)

您需要在StreamWriter构造函数中声明它,在您的情况下:

OutputFile = new StreamWriter(nBook.TxtFileName + ".txt");