개발/C#

# 일기 쓰고 텍스트 파일로 저장하는 간단한 프로그램 예제

노마드 2021. 1. 25. 08:26

 

먼저 확인 버튼 - 속성 - 이벤트에서 button1_Click이라는 이벤트 핸들러를 Click이라는 이벤트에 연결해야한다.

(이벤트에서 Click을 찾아서 우측 공간을 더블클릭하면 된다)

 

using System;

using System.Windows.Forms;

using System.IO;

namespace DiaryProject

{

  public partial class Form1 : Form

  {

    public Form1()

    {

      InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)

    {

      //Stream fileSave = new FileStream(@"C:\Users\inwoo\Desktop\test\test.txt", FileMode.Create);

      File.WriteAllText(@"C:\Users\inwoo\Desktop\test\" + textBox1.Text + ".txt" , textBox2.Text + Environment.NewLine + Environment.NewLine + "작성 시간: " + DateTime.Now);

      MessageBox.Show("새로운 일기가 등록되었습니다!");

    }

  }

}