Csharp
C# - 파일 읽기와 쓰기 ( 저장 )
sckwon770
2019. 7. 7. 14:54

Setup
"System.IO" 네임스페이스를 추가해야 함
1
|
using System.IO;
|
Usage
- 파일 읽기
.txt 와 .dat 파일을 읽어서 사용함
- File.ReadAllText : 파일의 모든 내용을 읽음
- File.ReadAllLines : 파일의 내용을 한줄씩 읽음
1
2
3
|
string variable = File.ReadAllText("Path");
string[] variable = File.ReadAllLines("Path");
|
cs |
- 파일 작성
.txt와 .dat 확장자로 파일을 작성하여 저장함
- 덮어쓰기
1
2
3
|
StreamWrite writer = new StreamWriter("Path");
writer.WriteLine(string);
writer.Write(string);
|
- 이어쓰기
1
|
StreamWrite writer = new StreamWriter("Path", true);
|
- 실행되고 있는 프로그램 경로 얻기
12 string path = AppDomain.CurrentDomain.BaseDirectory; // C:\\Program file\\Cs\\string text = File.ReadAllText(path + "notepad.txt");cs