简体   繁体   中英

Saving file in .txt format (Notepad)

I was wondering if you can help me out with this. I have a DataGridView with a couple of books saved on it. When I click on a book, the book's story loads in a RichTextBox . When I change that text and want to save it, I want to save the content in a .txt file. How can I go about it?

I have included code for you to see what I want to do.

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();
}

Thanks :)

Does this work? I hope so.

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

在这种情况下,您必须在StreamWriter构造函数上声明它:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM