c#
-
문자열 예제C# 2021. 10. 2. 17:35
Substring - 일부분 가져오기 string s3 = "Visual C# Express"; System.Console.WriteLine(s3.Substring(7, 2)); // Output: "C#" String[] pairs = { "Color1=red", "Color2=green", "Color3=blue", "Title=Code Repository" }; foreach (var pair in pairs) { int position = pair.IndexOf("="); if (position
-
Stream 에서 한줄씩 읽기C# 2021. 10. 2. 17:10
int counter = 0; string line; // Read the file and display it line by line. System.IO.StreamReader file = new System.IO.StreamReader(@"c:\test.txt"); while((line = file.ReadLine()) != null) { System.Console.WriteLine(line); counter++; } file.Close(); System.Console.WriteLine("There were {0} lines.", counter); // Suspend the screen. System.Console.ReadLine();
-
파일 선택. File pickerC# 2021. 10. 2. 02:49
var fileContent = string.Empty; var filePath = string.Empty; using (OpenFileDialog openFileDialog = new OpenFileDialog()) { openFileDialog.InitialDirectory = "c:\\"; openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; openFileDialog.FilterIndex = 2; openFileDialog.RestoreDirectory = true; if (openFileDialog.ShowDialog() == DialogResult.OK) { //Get the path of specified file fi..
-
파일 읽기, StreamReaderC# 2021. 10. 2. 02:13
인스턴스를 사용하여 StreamReader 파일에서 텍스트를 읽기. using System; using System.IO; class Test { public static void Main() { try { // 인스턴스 생성 // using 을 사용함으로서, 사용후에 close 하게 된다. using (StreamReader sr = new StreamReader("TestFile.txt", Encoding.UTF8)) { string line; // 파일 끝까지 읽어서 출력. while ((line = sr.ReadLine()) != null) { Console.WriteLine(line); } } } catch (Exception e) { // 에러 처리. Console.WriteLine("The ..