2009-11-10 4 views

답변

3

이 작업을 수행하는 방법에는 여러 가지, 간단한 존재가있다. 당신은 .NET Compact Framework에서를 대상으로하지 않은 경우 태그 제안으로

, 당신도 간단하게 할 수있는 : 당신은 어떤 뇌사 버전에 붙어있는 경우

File.WriteAllText(path, string); 
+0

귀하의 예와 지시 사항을 읽어 주셔서 감사합니다. 이렇게 계속하십시오! ;) –

2
System.IO.File.WriteAllText("myfile.txt", textBox.Text); 

BCL, 당신은 자신이 그 기능을 쓸 수 있습니다 :

static void WriteAllText(string path, string txt) { 
    var bytes = Encoding.UTF8.GetBytes(txt); 
    using (var f = File.OpenWrite(path)) { 
     f.Write(bytes, 0, bytes.Length); 
    } 
} 
+0

+1이 더 쉽게 얻을 수 없습니다! – James

1

을이 시도 :

using System.Text; 
using System.IO; 
static void Main(string[] args) 
{ 
    // replace string with your file path and name file. 
    using (StreamWriter sw = new StreamWriter("line.txt")) 
    { 
    sw.WriteLine(MyTextBox.Text); 
    } 
} 
,536을

물론 예외 처리 등을 추가하십시오.

+0

Duh! 'using' *은 예외 처리입니다! –

+0

나는 'try-catch'구문을 사용하여 사용자에게 그 사실을 알렸다. 파일을 작성할 수 없습니다. b. 그는 권한이 없다. c. 텍스트 상자는 비어 있습니다. –

0

richTextBox의 경우이 목적으로 "저장"버튼을 추가 할 수 있습니다. 또한 Toolbox에서 saveFileDialog 컨트롤을 추가 한 다음 버튼의 click 이벤트에 다음 코드를 추가하십시오.

private void button1_Click(object sender, EventArgs e) 
{ 
    DialogResult Result = saveFileDialog1.ShowDialog();//Show the dialog to save the file. 
    //Test result and determine whether the user selected a file name from the saveFileDialog. 
    if ((Result == DialogResult.OK) && (saveFileDialog1.FileName.Length > 0)) 
    { 
     //Save the contents of the richTextBox into the file. 
     richTextBox1.SaveFile(saveFileDialog1.FileName); 
    } 
} 
관련 문제