2012-04-23 2 views
1

사용자가 여러 파일로로드해야하는 프로그램을 만들고 있습니다. 그러나 ListBox에서는로드 한 파일의 파일 이름 만 표시해야하지만로드 된 파일은 계속 사용할 수 있어야합니다. 그래서 전 경로를 숨기고 싶습니다. 이것은 내가 지금 ListBox로 파일을로드하는 방법이지만, 전체 경로를 보여줍니다listbox에 파일 이름을 표시하지만 openfiledialog를 사용하여 상대 경로를 유지하려면 어떻게해야합니까?

private void browseBttn_Click(object sender, EventArgs e) 
{ 
    OpenFileDialog OpenFileDialog1 = new OpenFileDialog(); 
    OpenFileDialog1.Multiselect = true; 
    OpenFileDialog1.Filter = "DLL Files|*.dll"; 
    OpenFileDialog1.Title = "Select a Dll File"; 
    if (OpenFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
    { 
     dllList.Items.AddRange(OpenFileDialog1.FileNames); 
    } 
} 
+0

에서 이 작업도하고 있는데, 버튼 만 누르면 Kinect에서 골격 ID를 저장합니다. ['Add'] (http : // msdn) 때문에 [ComboBoxs] (http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.aspx)를 사용하기로 결정했습니다. microsoft.com/ko-kr/library/system.windows.forms.combobox.objectcollection.add.aspx) 방법,하지만 내가 틀렸는 지 궁금합니다. 나는 이것이 어떻게 될지보고 싶어서 기다릴 수 없다 ... –

답변

3
// Set a global variable to hold all the selected files result 
List<String> fullFileName; 

// Browse button handler 
    private void button1_Click(object sender, EventArgs e) 
    { 
     OpenFileDialog OpenFileDialog1 = new OpenFileDialog(); 
     OpenFileDialog1.Multiselect = true; 
     OpenFileDialog1.Filter = "DLL Files|*.dll"; 
     OpenFileDialog1.Title = "Seclect a Dll File"; 
     if (OpenFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
     { 
      // put the selected result in the global variable 
      fullFileName = new List<String>(OpenFileDialog1.FileNames); 

      // add just the names to the listbox 
      foreach (string fileName in fullFileName) 
      { 
       dllList.Items.Add(fileName.Substring(fileName.LastIndexOf(@"\")+1)); 
      } 


     } 
    } 

    // handle the selected change if you wish and get the full path from the selectedIndex. 
    private void dllList_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     // check to make sure there is a selected item 
     if (dllList.SelectedIndex > -1) 
     { 
      string fullPath = fullFileName[dllList.SelectedIndex]; 

      // remove the item from the list 
      fullFileName.RemoveAt(dllList.SelectedIndex); 
      dllList.Items.Remove(dllList.SelectedItem); 
     } 
    } 
+0

그냥 코드 –

+0

이상을 추가해 주셔서 감사합니다. 나는 이것을 시험해 보겠습니다. 훌륭한 코드입니다. 이해가됩니다. – Annabelle

+0

한 가지 더, 어떻게 목록 상자에서 해당 항목을 제거하는 함수를 쓸 것인가? – Annabelle

2

당신은 사용하여 절대 경로의 fileName을 추출 할 수 있습니다를 static Class PathSystem.IO namespace

//returns only the filename of an absolute path. 

Path.GetFileName("FilePath"); 
관련 문제