개발 노트

C# listbox 텍스트파일 저장후 불러오기 본문

프로그래밍/C#

C# listbox 텍스트파일 저장후 불러오기

알 수 없는 사용자 2022. 3. 7. 17:37

리스트 저장버튼 만든후

함수생성

private void buttonTopicSave_Click(object sender, EventArgs e)
		{
			StreamWriter sw;
			sw = new StreamWriter("Sub.txt");
			int nCount = listBoxSub.Items.Count;
			for (int i = 0; i < nCount; i++)
			{
				listBoxSub.Items[i] += "\r\n";
				sw.Write(listBoxSub.Items[i]);
			}
			sw.Close();
		}

생성된 패스값 불러와서 리스트박스로 가져오기

	private void buttonTopicOpen_Click(object sender, EventArgs e)
		{
			string currentPath = System.IO.Directory.GetCurrentDirectory();
			StreamReader file = new StreamReader(currentPath + "\\Sub.txt", Encoding.Default); 
			string s = ""; 
			while (s != null)
			{ 
				s = file.ReadLine(); 
				if (!string.IsNullOrEmpty(s)) listBoxSub.Items.Add(s); 
			}
			file.Close();
		}

 

'프로그래밍 > C#' 카테고리의 다른 글

System.IO 파일입출력 텍스트모드 바이너리모드  (0) 2022.03.10
로그파일저장  (0) 2022.03.10
C# 현재경로로 파일 저장하기  (0) 2022.03.07
C# ini 저장해서 재실행시 불러오기  (0) 2022.03.07
mosquitto conf  (0) 2022.03.07