2011-11-07 5 views
1

그래서이 질문에 답을 얻었지만 다소 어려움을 겪고 있습니다. 배열에 4 개의 값을 저장하고 목록보기에 표시하는 기본 Windows 양식 프로그램이 있습니다. 하지만 지금은 여분의 버튼이 있습니다. 클릭하면 저장된 값을 저장하고 텍스트 파일로 내보내기 만하면됩니다. 배열에서 직접 내보내는 것이 더 쉬울까요? 그리고 어떻게 할 수 있습니까?listview에서 텍스트 파일로 데이터를 저장하는 방법

미리 감사드립니다.

+1

어떤 응용 프로그램입니까? WinForms/WPF/WebForms? 컨텍스트를 반영하기 위해 태그를 더 추가하십시오. – sll

+0

배열은 무엇입니까? StreamWriter를 열고 배열을 반복하면 아마도 가장 쉽습니다. – samjudson

+0

죄송합니다, 그것은 WinForms 응용 프로그램과 그 문자열 배열 –

답변

3
File.WriteAllLines(path, array, Encoding.UTF8); 
+0

오류 알 수없는 이스케이프 시퀀스 \t 경로를 입력 할 때이 오류가 발생 했습니까? –

+0

@ "C : \ Documents \ File.txt"와 같이 경로 앞에 @를 시도하십시오. – neeKo

+0

경로 문자열의 시작 부분에 @를 추가하십시오. –

1
using (TextWriter tw = new StreamWriter(@"C:\listViewContent.txt")) { 
    foreach (ListViewItem item in listView.Items) { 
     tw.WriteLine(item.Text); 
    } 
} 
+0

이 코드는 잘 작동하지만 첫 번째 열의 값만 표시합니다.이 값은 이름 일뿐입니다. 다른 3 열을 어떻게 보이게 할 수 있습니까? –

+0

3 개의 다른 열은 무엇입니까? 당신의'listView''View' 속성이'Details'로 설정되어 있습니까? – Otiel

+0

네, 세부 사항으로 설정되어 있습니다. 첫 번째 열은 이름 (성), 성, 연령, 회의 유형입니다. –

0

그리고 지금, 파일 대화 저장 공상과 함께! :)

private void saveButton_Click(object sender, EventArgs e) 
    { 
     Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog(); 
     dlg.FileName = "DumpFile1"; // Default file name 
     dlg.DefaultExt = ".txt"; // Default file extension 
     dlg.Filter = "Text files (.txt)|*.txt"; // Filter files by extension 

     // Show save file dialog box 
     Nullable<bool> result = dlg.ShowDialog(); 

     // Process save file dialog box results 
     if (result == true) 
     { 
      // Save document 
      string filename = dlg.FileName; 

      File.WriteAllLines(filename, array, Encoding.UTF8); //array is your array of strings 
     } 
    } 

당신은 PresentationFramework에 대한 참조를 추가 해야합니다. 이 단지 철저한 버전 @othiel 응답 사람은 아직 그것을 필요로 그냥 넣다

1

- ( 탭 PresentationFramework을 선택>.NET에서, 참조 추가 마우스 오른쪽 솔루션 탐색기참조 클릭) 하위 항목 포함

 try 
     { 
      using (System.IO.TextWriter tw = new System.IO.StreamWriter(@"C:\listViewContent.txt")) 
      { 
       foreach (ListViewItem item in listView1.Items) 
       { 
        tw.WriteLine(item.Text); 
        for (int a = 1; a <= 3; a++) //the 3 = number of subitems in a listview 
        { 
         tw.WriteLine(item.SubItems[a].Text); 
        } 
       } 
      } 
     } 
     catch { 
      MessageBox.Show("TEXT FILE NOT FOUND"); 
     } 

listView1을 자세히 설정해야합니다.

관련 문제