简体   繁体   中英

How do you check if a storagefolder is null c#

I'm surprised this has not come up before and im thinking im just not using the right search terms but been stuck on this for an hour now.

heres the issue, when some one presses the roll button on my app i want a folder picker to appear and allow the person to pick a folder, then the files are saved in that folder. I only want it to run once.

heres what i have

FolderPicker folder =new FolderPicker();
folder.FileTypeFilter.Add(".html");
folder.ViewMode = PickerViewMode.List;
folder.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
folder.SettingsIdentifier = "folder";
theFolder = await folder.PickSingleFolderAsync(); 
StorageFile file = await theFolder.CreateFileAsync(name + ".html",  CreationCollisionOption.ReplaceExisting);

this works but ofcourse the picker appears every time i run the method (as its setup to do at the moment.

StorageFolder theFolder set up at the start of the programs run simply as public StorageFolder theFolder;

i have tried changing

theFolder = await folder.PickSingleFolderAsync(); 

to

while (theFolder.Equals(null))
{
    theFolder = await folder.PickSingleFolderAsync(); 
}

but that just causes a crash

"Object reference not set to an instance of an Object."

i also tried getting the display name of the folder and if it was blank then getting the folderpicker to show...same error

I can not even just have the folder picker show every time the person clicks run as a click of cancel will crash it (and it would be very annoying to the user)

any ideas ?

my app is a universal app in c# for windows phone and store, it is the windows store version im currently working on

If variable is null you can't call method on it.

while (theFolder == null)
{
    theFolder = await folder.PickSingleFolderAsync(); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM