반응형
* 로그 파일 남기기 예제 ...
위 그림과 같이 리스트뷰 Columns 붉은 테두리 안에 있는 ... 버튼을 클릭하여 ColumnHeader 컬렉션 편집기를
열어 멤버를 추가 해 줍니다.
첫번째 열은 No. 에 해당하며 Log 갯수를 표시 할 Column
두번째 열은 로그 내용에 해당하며 로그 파일안에 내용을 표시할 Column
전체 소스 코드
Form1.cs
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;
namespace CSharp_Log
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
listView1.View = View.Details;
listView1.FullRowSelect = true;
listView1.GridLines = true;
//2. 로그 남기기...
Log_Info("Log 프로그램 예제를 시작 합니다.");
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
//2. 로그 남기기...
Log_Info("Log 프로그램 예제를 종료 합니다.");
}
//1. 로그 함수 작성...
public bool Log_Info(string strMsg)
{
try
{
string strCheckFolder = "";
string strFileName = "";
//현재 EXE 파일가 위치 하고 있는 폴더를 가져옴.
string strLocal = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\"));
//로그 폴더가 없으면 생성
strCheckFolder = strLocal + "\\Log";
if (!System.IO.Directory.Exists(strCheckFolder))
{
System.IO.Directory.CreateDirectory(strCheckFolder);
}
strFileName = strCheckFolder + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
System.IO.StreamWriter FileWriter = new System.IO.StreamWriter(strFileName, true);
FileWriter.Write(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + " => " + strMsg + "\r\n");
FileWriter.Flush();
FileWriter.Close();
}
catch
{
return false;
}
return true;
}
private void button1_Click(object sender, EventArgs e)
{
//2. 로그 남기기...
Log_Info("로그 남기기 버튼을 클릭 하였습니다.");
}
private void button2_Click(object sender, EventArgs e)
{
string strLocal = Application.ExecutablePath.Substring(0, Application.ExecutablePath.LastIndexOf("\\"));
OpenFileDialog ofd = new OpenFileDialog();
//텍스트 파일만 불러오기...
ofd.Filter = "TXT 파일(*.txt) | *.txt";
//파일대화상자 시작시 기본 폴더 지정으로 지정된 폴더로 바로 띄우기
ofd.InitialDirectory = strLocal + "\\Log";
if (ofd.ShowDialog() == DialogResult.OK)
{
listView1.Items.Clear();
string strFileName = "";
strFileName = ofd.FileName;
if (!System.IO.File.Exists(strFileName))
{
MessageBox.Show("해당 파일이 없거나 선택된 파일이 없습니다.", "확 인", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return;
}
else
{
string[] strValue =System.IO. File.ReadAllLines(strFileName);
for (int iCount = 0; iCount < strValue.Length; iCount++)
{
ListViewItem lvi = new ListViewItem();
lvi.Text = (iCount + 1).ToString();
lvi.SubItems.Add(strValue[iCount]);
listView1.Items.Add(lvi);
}
}
}
}
}
}
* 예제 결과
[C#] File Create & Delete & Read & Write 예제
[C#] File Create & Delete & Read & Write 예제
* C# 파일 예제 Form1.cs using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Wind..
kdsoft-zeros.tistory.com
반응형
'C# Programming' 카테고리의 다른 글
[C#] Folder Copy 폴더 복사 예제 (0) | 2019.10.18 |
---|---|
[C#] File CheckSum 예제 (MD5 Checksum) (0) | 2019.10.16 |
[C#] 파일 및 폴더 감시 (FileSystemWatcher) (0) | 2019.10.10 |
[C#] File Copy (파일 복사 예제) (0) | 2019.10.08 |
[C#] 프로그램 중복 실행 방지 (0) | 2019.10.06 |