2014-04-09 5 views
0

파일을 *.txt 기반 문서로 저장하는 프로그램을 만들려고합니다. 나는 현재 저장된 파일C#의 모방/작성 미니멀리스트 파일 브라우저

(Located in C:\ProgramData\ProgramName\Incidents)

의 목록 버튼을 클릭하고 끌어 할 수 있도록하려면

enter image description here

위 내가 140219-000727이의 이름입니다 달성하기 위해 노력하고있어의 예입니다 파일, 나머지는 필요하지 않습니다. 열기 또는 더블 클릭을 클릭하면 해당 파일을 "열기"하고 이미 작성한 WinForm 응용 프로그램의 기존 양식으로 .txt를 구문 분석합니다.

최소한의 시스템 리소스로이 작업을 수행하는 가장 좋은 방법은 무엇입니까?

+0

파일 브라우저가 필요 한가요? y txt가 필요 했습니까? – HackerMan

답변

1

나는 Directory.GetFiles을 찾고 있습니다. 가장 간단한 마스크 "* .txt"를 사용하여 모든 txt 파일을 가져온 다음 Path.GetFileName을 사용하면 전체 경로에서 파일 이름을 잘라낼 수 있습니다. 그리고 나중에는 (더블 클릭이나 버튼 클릭에) 개방에 대한 디렉토리 이름 + 파일 이름을 사용 :

//populating: 
var files = Directory.GetFiles(YOUR_FOLDER_PATH, "*.txt"); 
foreach (var file in files) 
{ 
    var fileName = Path.GetFileName(file); 
    //assuming ListBox: 
    listBox.Items.Add(filename); 
} 

//opening (from listbox) 
var fileName = Path.Combine(YOUR_FOLDER_PATH, listBox.SelectedItem.ToString()); 
File.ReadAllText(fileName); 
1

당신은 단지 FolderBrowserDialog 컨트롤이 필요합니다.

var fileNames = new List<string>(); 
var fileContents = new Dictionary<string, string>(); 
var filePaths = Directory.EnumerateFiles(folderBrowserDialog1.SelectedPath, "*.txt"); 
foreach (var filePath in filePaths) 
{ 
    var fileName =new FileInfo(filePath).Name; 
    fileNames.Add(fileName); 
    fileContents.Add(fileName, File.ReadAllText(filePath)); 
}