프로그래밍/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();
		}