2013-12-12 4 views
1

두 가지 형태가 있습니다. 첫 번째 양식에서 버튼을 클릭하면 RichTextBox.rtf 파일이로드 된 두 번째 양식이 자동으로로드됩니다.RichTextBox에서 .rtf 파일을로드하는 방법은 무엇입니까?

RichTextBox.rtf 파일을로드하는 데 도움을 요청하려면 양식에 경로가 코딩되어 있지 않습니까? 나는 Directory.GetCurrentDirectory을 사용해 보았지만 경험 많은 프로그래머가 아니기 때문에 힘든 시간을 보내고 있습니다.

답변

3

이 시도 :

public void LoadMyFile() 
{ 
    // Create an OpenFileDialog to request a file to open. 
    OpenFileDialog openFile1 = new OpenFileDialog(); 

    // Initialize the OpenFileDialog to look for RTF files. 
    openFile1.DefaultExt = "*.rtf"; 
    openFile1.Filter = "RTF Files|*.rtf"; 

    // Determine whether the user selected a file from the OpenFileDialog. 
    if(openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK && 
     openFile1.FileName.Length > 0) 
    { 
     // Load the contents of the file into the RichTextBox. 
     richTextBox1.LoadFile(openFile1.FileName); 
    } 
} 
+0

나는 내 문제를 명확히하고자합니다. .rtf 파일은 프로젝트 폴더 안에 있습니다. 대화 상자를로드하지 않고 자동으로 프로그램을로드하기를 원합니다. 고맙습니다. – user3093755

+0

Directory.GetCurrentDirectory()를 호출하면 big/debug 폴더 경로가 반환됩니다. 확실하게 인쇄하고 무엇이 반환되는지 확인하십시오. try 문자열 path = "\\ .. \\ .. \\"+ Directory.GetCurrentDirectory() + filepath; –

+0

이것이 내가 한 일이며 효과가있었습니다. 고맙습니다. string path = Directory.GetCurrentDirectory(); string Chosen_File = ""; openFileDialog1.FileName = "Document.rtf"; Chosen_File = openFileDialog1.FileName; richTextBox1.LoadFile (Chosen_File, RichTextBoxStreamType.RichText); – user3093755

관련 문제