[英]Using DeviceID from Windows.Devices.Enumeration to find the Drive Letter of a Removable Drive (USB) in C# Windows 10 IoT Core?
我正在嘗試掃描插入到運行 Windows 10 IoT Core 的 Raspberry Pi 中的 USB 驅動器,以查找特定文件夾中的特定文件。 我目前能夠使用 Windows.Devices.Enumeration.DeviceWatcher 檢測何時從設備中添加或刪除 USB 驅動器,並且也可以使用它找到 DeviceId。 我使用DeviceEnumerationAndPairing演示項目做到了這一點。 我知道 StorageDevice.FromId() 通常適用於此,但它似乎不同意 Windows 10 IoT Core。 我還嘗試使用KnownFolders.RemovableDevices 在添加設備時遍歷所有設備,但總是會立即使應用程序崩潰。 我希望能夠使用 DeviceId 來查找 USB 設備的驅動器號,然后檢查驅動器是否為空,如果不是,則在該設備中導航到我希望在那里的目錄和文件。
在此先感謝您的幫助!
您可以在DeviceEnumerationAndPairing演示項目的場景 2 中的handlerAdded handlerAdded()
中添加以下代碼行。
var removableDevices = KnownFolders.RemovableDevices;
// Get all driver letters
var folders = await removableDevices.GetFoldersAsync();
if (folders.Count > 0)
{
foreach (StorageFolder folder in folders)
{
System.Diagnostics.Debug.WriteLine(folder.Name);
// Check the driver letter is empty or not.
var items = folder.GetItemsAsync();
if (items == null)
{
System.Diagnostics.Debug.WriteLine("There are no files and folders.");
}
}
}
同時,您需要聲明您要訪問的文件類型的Capability
,例如圖片,您可以在Package.appxmanifest 中添加以下行。
這對我有用。 希望對你有幫助。
<uap:Capability Name="picturesLibrary" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.