1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
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 TextEdit
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region 변수 선언
string fileContent;
string filePath;
int counter = 0;
string line;
string saveName;
#endregion 변수선언
// 불러오기
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = "C:\\";
openFileDialog.Filter = "txt files (*.txt)|*.txt";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
//파일 경로를 얻는다
filePath = openFileDialog.FileName;
//파일 내용을 Stream으로 읽는다
var fileStream = openFileDialog.OpenFile();
StreamReader reader = new StreamReader(fileStream);
fileContent = reader.ReadToEnd();
textBox1.Text = filePath.ToString();
richTextBox1.Text = fileContent.ToString();
// Read the file and display it line by line.
StreamReader file = new StreamReader(@filePath);
StringBuilder content = new StringBuilder();
while ((line = file.ReadLine()) != null)
{
if(line.ToString().Length == 0)
{
counter++;
}
else if(line.ToString().Length != 0)
{
content.AppendLine();
content.AppendLine();
content.Append(line);
}
if (counter == 2)
{
counter = 0;
}
}
richTextBox2.Text = content.ToString();
}
}
// 저장하기
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog
{
InitialDirectory = "C:\\",
Filter = "txt files (*.txt)|*.txt",
FilterIndex = 2,
RestoreDirectory = true
};
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
saveName = saveFileDialog.FileName;
FileStream saveStream = new FileStream(saveName, FileMode.Create, FileAccess.Write);
StreamWriter writer = new StreamWriter(saveStream);
writer.WriteLine(richTextBox2.Text);
writer.Close();
}
}
// 닫기
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
|
파일 업로드는 마이크로소프트 문서를 참고했다.
'공부 > C#' 카테고리의 다른 글
[C#] enum vs final static (0) | 2020.04.07 |
---|---|
[C#] 텍스트파일 줄바꿈/엔터 제거 프로그램 (4) | 2020.02.09 |