2013-05-22 5 views
0

저는 C# 프로그램을 만들고 있습니다. 이 프로그램에서는 텍스트 상자의 값을 CSV 파일에 쓰려고합니다. 이것은 지금까지 작동합니다.C# 쓰기/읽기 CSV

hellobye| 
hello (TextBox1) 
bye (TextBox2) 

언제나 새 줄을 맺을 수있는 방법은 무엇입니까? 벌써 환경을 시험해 봤어 .NewLine, 그냥 일하는 게 아니야. 이것은 지금까지 내 코드입니다

:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.IO; 


namespace test 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      if (textBox1.Text.Length != 0) 
      { 
       String input = textBox1.Text + textBox2.Text; 
       string[] lines = {input + "|" }; 
       System.IO.File.AppendAllLines(@"c:\output.csv", lines); 
       textBox1.Clear(); 
       textBox2.Clear(); 
      } 

      } 
    } 
} 
+0

CSV는 * 쉼표로 구분 된 값을 의미합니다. * Pipe *를 구분 기호로 사용하려고합니다. –

+0

그리고 당신의 결과는 당신이 말한 그대로입니다. 다른 것을 원한다면 다른 것을 말해야합니다. –

+0

Dan-o, 네가하는 말은 어때? 내가 어떻게 .. 이해가 안 돼 입력은 내 CSV 파일에 새 줄에 있어야합니다. – haassiej

답변

0

그래서 내가 맞으면 현재 출력은 "hellobye |"입니다.

하지만 당신이 안녕하세요 안녕 그래서

되고 싶어 당신이 다음 쉼표로 요소를 DELIM, 다음 행에 대한 줄 바꿈을 삽입 할 것 CSV를 만드는 경우.

static void Main(string[] args) 
    { 
     string string1 = "hello"; 
     string string2 = "bye"; 

     string[] lines = 
      { 
       string1 + 
       Environment.NewLine + 
       string2 
      }; 

     System.IO.File.AppendAllLines(@"c:\output.csv", lines); 
    } 

문자열 1 & 문자열 2는 단순히 대신 사용의 StreamWriter.WriteLine 방법의 예를 사용할 수 있습니다

0

이 문제입니다 :

String input = textBox1.Text + textBox2.Text; 

당신은 함께 하나 개의 단어로 두 개의 텍스트 상자의 내용을 총괄하고 있으며, 시스템 그 이후 한 단어가 끝나고 다음 단어가 시작되는 위치를 더 이상 말할 수 없습니다.

1

텍스트 상자의 출력으로 기능 것 : 그래서 빠른 콘솔 응용 프로그램이 다음과 같이합니다 :

using System; 
using System.IO; 

class Test 
{ 
    public static void Main() 
    { 
     string path = @"c:\temp\MyTest.txt"; 
     if (!File.Exists(path)) 
     { 
      // Create a file to write to. 
      using (StreamWriter sw = File.CreateText(path)) 
      { 
       sw.WriteLine("Hello"); 
       sw.WriteLine("And"); 
       sw.WriteLine("Welcome"); 
      } 
     } 

     // Open the file to read from. 
     using (StreamReader sr = File.OpenText(path)) 
     { 
      string s = ""; 
      while ((s = sr.ReadLine()) != null) 
      { 
       Console.WriteLine(s); 
      } 
     } 
    } 
}