2014-10-10 2 views
0

내가이 문제를 해결할 수 있는지 궁금합니다. 나는 그것에 두 권의 책이 저장된 DataGridView을 가지고있다. 책을 클릭하면 책의 이야기가 RichTextBox에로드됩니다. 해당 텍스트를 변경하고 저장하려면 .txt 파일에 내용을 저장하고 싶습니다. 어떻게해야할까요?.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(); 
} 

감사 :

+0

는 여기를 참조하십시오 : nBook.TxtFileName + "를 사용하지 않는 이유 http://stackoverflow.com/questions/7306214/append-lines-to-a-file-using-a-streamwriter –

+4

이해할 수없는 TXT. " –

+0

흠, 나는이 질문을 파일에 쓰는 방법을 묻는 것으로 해석했다. 끝에 파일 이름을".txt "로 가져 오는 것과 반대이다. @ JBTGE, 문제가 있습니까? –

답변

-1

당신은 당신의 경우에, 당신의 StreamWriter를 생성자에 선언해야한다 :

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

@CaptainFindus 형식화가 제대로 된 것 같지 않습니다. 'OutputFile = new StreamWriter (nBook.TxtFileName + ".txt");' – Wizard

+0

'.' 보지 않고, -2 대답 ... lol – CapitanFindus

2

이 일을합니까? 나는 그렇게 희망한다.

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

그래, 나는 또한 다음과 같은 일을 발견 : OutputFile = new StreamWriter (nBook.TxtFileName + ". txt"); – JBTGE