2015-10-25 26 views
2

我試圖從Android手機上的文件夾中獲取項目。無法從外部設備獲取目錄

但是,FolderBrowserDialog不會讓我從手機中選擇一個文件夾。路徑看起來像這樣This PC\Xperia Z3 Compact\SD Card\Music

要選擇我目前使用的文件夾:

private void button_Click(object sender, EventArgs e) 
{ 
    System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog(); 
    if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
    { 
     UserFolderLocation = dlg.SelectedPath; 
    } 
    else { } 
} 

然後搜索其內容的文件夾,當我使用:

try 
{ 
    folderItems = Directory.GetFiles(directory).Select(f => Path.GetFileNameWithoutExtension(f)).ToArray(); 
} 
catch (Exception e) 
{ 
    MessageBox.Show(e.ToString()); 
} 

如果我插入路徑This PC\Xperia Z3 Compact\SD Card\Music作爲一個變量,然後搜索它,它會拋出一個System.IO.DirectoryNotFoundException

如何選擇和使用不以c:,d:等開頭的路徑?

+0

請添加更多信息您如何在Android上運行您的代碼。你在用什麼框架? –

+0

這是一個桌面應用程序,它試圖連接的手機正在運行Android 5.1.1。我正在使用.NET框架的4.5.2版本。 – difurious

回答

2

最後我結束了使用shell32庫。它能夠處理便攜式設備(包括但不包括驅動器號)。

包括用於shell32.dll 參考和包括庫:

using Shell32; 

然後,而不是使用的FolderBrowserDialog我使用了使用殼瀏覽文件夾。它返回一個手機上的文件夾一個奇怪的路徑,爲手機,我用來測試的路徑是這樣的: ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\\?\usb#vid_04e8&pid_6860&ms_comp_mtp&samsung_android#6&fee689d&3&0000#{6ac27878-a6fa-4155-ba85-f98f491d4f33}\SID-{20002,SECZ9519043CHOHB01,63829639168}\{013C00D0-011B-0130-3A01-380113012901}

public int Hwnd { get; private set; } 

private void button3_Click(object sender, EventArgs e) 
{ 
    Shell shell = new Shell(); 
    Folder folder = shell.BrowseForFolder((int)Hwnd, "Choose Folder", 0, 0);  
    if (folder == null) 
    { 
     // User cancelled 
    } 
    else 
    {    
     FolderItem fi = (folder as Folder3).Self; 
     UserFolderLocation = fi.Path; 
    } 
} 

然後選擇搜索文件夾及其內容:

try 
{ 
    Folder dir = shell.NameSpace(directory); 

    List<string> list = new List<string>(); 
    foreach (FolderItem curr in dir.Items()) 
    { 
     list.Add(curr.Name); 
    } 
    folderItems = list.ToArray(); 
} 
catch (Exception e) 
{ 
    MessageBox.Show(e.ToString()); 
} 
0

「這臺PC」僅存在於用戶的眼中 - 在內部根本不使用。您可以通過在Windows資源管理器將第一標記設置看到自己 screenshot

另外的Windows分配驅動器號到每個本地設備 - 它只是默認情況下不表現出來。 (使用第二個標記設置來檢查)

所以在現實中,你必須使用(假設你的電話被分配驅動器F:)像F:\SD Card\Music\

可能相關:Get List Of Connected USB Devices關於在不知道分配的驅動器號的情況下查找設備的能力。

+0

即使啓用了這些設置,手機仍然被列爲「This PC \ Xperia Z3 Compact」 – difurious