StorageFolder.TryGetItemAsync(String) Metode
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mencoba mendapatkan file atau folder dengan nama yang ditentukan dari folder saat ini. Mengembalikan null alih-alih menaikkan FileNotFoundException jika file atau folder yang ditentukan tidak ditemukan.
public:
virtual IAsyncOperation<IStorageItem ^> ^ TryGetItemAsync(Platform::String ^ name) = TryGetItemAsync;
/// [Windows.Foundation.Metadata.RemoteAsync]
IAsyncOperation<IStorageItem> TryGetItemAsync(winrt::hstring const& name);
[Windows.Foundation.Metadata.RemoteAsync]
public IAsyncOperation<IStorageItem> TryGetItemAsync(string name);
function tryGetItemAsync(name)
Public Function TryGetItemAsync (name As String) As IAsyncOperation(Of IStorageItem)
Parameter
- name
-
String
Platform::String
winrt::hstring
Nama (atau jalur relatif terhadap folder saat ini) dari file atau folder yang akan didapatkan.
Mengembalikan
Ketika metode ini berhasil diselesaikan, metode ini mengembalikan IStorageItem yang mewakili file atau folder yang ditentukan. Jika file atau folder yang ditentukan tidak ditemukan, metode ini mengembalikan null alih-alih menaikkan pengecualian.
Untuk bekerja dengan item yang dikembalikan, panggil metode IsOfType antarmuka IStorageItem untuk menentukan apakah item tersebut adalah file atau folder. Kemudian transmisikan item ke StorageFolder atau StorageFile.
Penerapan
- Atribut
Contoh
Contoh berikut menunjukkan cara mencoba mendapatkan satu file atau folder dari folder saat ini, atau untuk memeriksa apakah file atau folder ada, dengan memanggil metode TryGetItemAsync.
using Windows.Storage;
using System.Threading.Tasks;
using System.Diagnostics; // For writing results to Output window.
// Get the path to the app's Assets folder.
string root = Windows.ApplicationModel.Package.Current.InstalledLocation.Path;
string path = root + @"\Assets";
// Get the app's Assets folder.
StorageFolder assetsFolder = await StorageFolder.GetFolderFromPathAsync(path);
// Check whether an image with the specified scale exists.
string imageName = "Logo.scale-140.png";
if (await assetsFolder.TryGetItemAsync(imageName) != null)
Debug.WriteLine(imageName + " exists.");
else // Return value of TryGetItemAsync is null.
Debug.WriteLine(imageName + " does not exist.");
IAsyncAction MainPage::ExampleCoroutineAsync()
{
std::wstring imageName{ L"Wide310x150Logo.scale-200.png" };
// Get the path to the app's Assets folder.
std::wstring path{ Windows::ApplicationModel::Package::Current().InstalledLocation().Path() + L"\\Assets" };
// Get the folder object that corresponds to this absolute path in the file system.
Windows::Storage::StorageFolder assets{ co_await Windows::Storage::StorageFolder::GetFolderFromPathAsync(path) };
IStorageItem image{ co_await assets.TryGetItemAsync(imageName) };
std::wstring output;
if (image)
{
output = L"File: " + image.Name() + L" found \n";
}
else
{
output = L"File not found\n";
}
::OutputDebugString(output.c_str());
}
String^ imageName = "Logo.scale-140.png";
// Get the app'ss Assets folder
String^ path = Windows::ApplicationModel::Package::Current->InstalledLocation->Path + "\\Assets";
create_task(StorageFolder::GetFolderFromPathAsync(path)).then([=](StorageFolder^ assets) -> task < IStorageItem^ >
{
return create_task(assets->TryGetItemAsync(imageName));
}).then([=](IStorageItem^ image) {
String^ output = "";
if (image == nullptr)
{
output = "File not found\n";
}
else
{
//output = "File: " + image->Name + " found \n";
}
OutputDebugString(output->Begin());
});
Keterangan
Panggil metode TryGetItemAsync untuk mencoba mendapatkan file atau folder berdasarkan nama, atau untuk memeriksa apakah file atau folder ada, tanpa perlu menangani FileNotFoundException. Jika file atau folder tidak dapat ditemukan, TryGetItemAsync mengembalikan null alih-alih menaikkan pengecualian.
Panggil metode IsOfType antarmuka IStorageItem untuk menentukan apakah item yang dikembalikan adalah file atau folder.
Berlaku untuk
Lihat juga
Saran dan Komentar
https://aka.ms/ContentUserFeedback.
Segera hadir: Sepanjang tahun 2024 kami akan menghentikan penggunaan GitHub Issues sebagai mekanisme umpan balik untuk konten dan menggantinya dengan sistem umpan balik baru. Untuk mengetahui informasi selengkapnya, lihat:Kirim dan lihat umpan balik untuk