ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [C#]CSV파일
    C#/WPF_예제소스 2022. 3. 30. 10:22

    쓰기

     

    , 콤마로 구분

    public void CreateCSV()
            {
                //StreamWriter file_CSV = new StreamWriter(new FileStream(path_CSV, FileMode.Create)); 
                StreamWriter file_CSV = new StreamWriter(path_CSV);
                file_CSV.WriteLine("A\tB\tC\tD");
                file_CSV.WriteLine("1, 2, 3, 4, 5");
                file_CSV.Close();
            }

     


    탭으로 구분

    public void CreateCSV()
            {
                //StreamWriter file_CSV = new StreamWriter(new FileStream(path_CSV, FileMode.Create)); 
                StreamWriter file_CSV = new StreamWriter(path_CSV, false, Encoding.Unicode);
                file_CSV.WriteLine("A\tB\tC\tD");
                file_CSV.WriteLine("1, 2, 3, 4, 5");
                file_CSV.Close();
            }

    파일쓰기

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    
    namespace HumanDetector
    {
        public class TempToFileSave
        {
            static string path = System.IO.Directory.GetCurrentDirectory() + @"\test.txt";
            static string path_CSV = System.IO.Directory.GetCurrentDirectory() + @"\test.CSV";
            public void CreateFile()
            {
                if (!File.Exists(path))
                {
                    File.Create(path);
                    //using (File.Create(path))
                    //{
                    //    MessageBox.Show("파일 생성 성공");
                    //}
                }
                else
                {
                    //MessageBox.Show("이미 파일이 존재 합니다.");
                }
            }
            public void WriteFile()
            {
                //파일 존재 유무 확인
                if (File.Exists(path))
                {
                    TxtWrite(path);
                    //MessageBox.Show("파일 쓰기 성공");
                }
                else
                {
                    CreateFile(); //파일이 존재 하지 않으면 다시 생성
                }
            }
            private void TxtWrite(string path)
            {
                StreamWriter writer;
                writer = File.AppendText(path);
                writer.WriteLine("텍스트 파일 이어 쓰기 성공");
                writer.Close();
            }
            public void DeleteFile()
            {
                string path = @"C:\test\test.txt";
    
                //파일 존재 유무 확인
                if (File.Exists(path))
                {
                    File.Delete(path);
                    //MessageBox.Show("파일 삭제 성공");
                }
            }
            public void CreateCSV()
            {
                //StreamWriter file_CSV = new StreamWriter(new FileStream(path_CSV, FileMode.Create)); 
                StreamWriter file_CSV = new StreamWriter(path_CSV);
                file_CSV.WriteLine("A\tB\tC\tD");
                file_CSV.WriteLine("1, 2, 3, 4, 5");
                file_CSV.Close();
            }
    
        }
    }
    728x90

    'C# > WPF_예제소스' 카테고리의 다른 글

    [C#] Redirect WriteLine stream to a textblock  (0) 2022.04.12
    [C#]ColorMap  (0) 2022.04.06
    [C#] 파일경로 설정  (0) 2022.03.29
    [C#]Serial 버퍼 Data 이어 붙이기  (0) 2022.03.28
    [C#]Polygon  (0) 2022.02.28

    댓글

Designed by Tistory.