How do you check if a storagefolder is null c# - 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();
}

Related

Edit Music metadata with Windows Runtime API with .net 6

After some research, I found out how to have my project access the Windows Runtime API.
Now I can happily read the Music properties of my file (Albums, Title etc):
StorageFile sf = await StorageFile.GetFileFromPathAsync(filename);
var props = await sf.Properties.GetMusicPropertiesAsync();
I can read and edit them, but I didn't find a way to persist.
I tried like this:
props.Album = "TEST";
await sf.Properties.SavePropertiesAsync();
Nothing breaks, but nothing changes, and my album is still what it was before.
Am I doing something silly and wrong?

Trying Upload same file in windows form

I am working to develop a Windows form Project, we should use a Product picture.
Every this working well, we use File.Exists() to check if the file already exists then copy the image to the product Image folder.
if (image.ShowDialog() == DialogResult.OK && !string.IsNullOrEmpty(image.FileName))
{
_view.FirmLogo.Image = new Bitmap(image.FileName);
_view.LogoURL = Path.Combine(System.IO.Path.GetFullPath(#"..\..\"), #"Resources\ProductImage\", Path.GetFileName(image.FileName));
//
if (File.Exists(_view.LogoURL))
{
File.Delete(_view.LogoURL);
}
//File.Move(#"c:\test\SomeFile.txt", #"c:\test\Test\SomeFile.txt");
File.Copy(image.FileName, _view.LogoURL);
}
The Bug
when we try to update the same file it fired an error:
"because it is being used by another process.
Ex. if we upload pic1, everything is well, we try to change to pic2 great job.
but if we try to back the pic1 fired the error especially if it happens in the same seesion.

UserActivity Opening last edited file

How can I get my app to open the last edited file after a click in the Windows 10 Timeline? Thanks for the help.
var activityId = Guid.NewGuid().ToString();
UserActivityChannel channel = UserActivityChannel.GetDefault();
UserActivity userActivity = await channel.GetOrCreateUserActivityAsync(activityId);
userActivity.VisualElements.DisplayText = PageTitle.Text;
if (File != null)
{
userActivity.VisualElements.Description = File.DisplayName;
}
userActivity.VisualElements.BackgroundColor = Colors.Black;
userActivity.VisualElements.Content = AdaptiveCardBuilder.CreateAdaptiveCardFromJson(cardText);
userActivity.ActivationUri = new Uri("my-app:navigate?page=" + _index);
await userActivity.SaveAsync();
_currentActivity?.Dispose();
_currentActivity = userActivity.CreateSession();
From the UserActivityChannel Class, there is a GetRecentUserActivitiesAsync method, but it only get the specified number of the most recently engaged user activities sorted by the time each user activity ended. Due to the privacy protecting policy of UWP app, your app can not get the user activities from system or other apps.
Currently, in your app, you can only operate the user activities which your app created. If the file is edited by the system or other app, you can not implement the effect you want. But if the file is operated by your app, you can try to create a user activity which is for opening the file then handle the protocol activated event in your app to open it. Maybe you should see the topic track recently used files.

UWP FolderPicker.PickSingleFolderAsync fails with COMException / E_FAIL

In my UWP app I have the following code:
private async void testButton_Click(object sender, RoutedEventArgs e)
{
var picker = new Windows.Storage.Pickers.FolderPicker();
StorageFolder folder = await picker.PickSingleFolderAsync();
}
but when I run this it fails on the second line with the message An exception of type 'System.Runtime.InteropServices.COMException' occurred in .... but was not handled in user code. The HRESULT from the exception is -2147467259 = 0x80004005 = E_FAIL.
I'm using file pickers already elsewhere in the app without problem. This is running on a Win10 desktop (launched from VS2015). Can anyone suggest why the error occurs and/or what to do to resolve it? Having a meaningless error message in what appears to be the simplest code possible I'm not sure how to proceed.
This is a bit of an oddity in WinRT. Although it is not mentioned in the documentation explicitly, it is necessary to add at least one item in the FileTypeFilter collection:
var folderPicker = new FolderPicker();
folderPicker.FileTypeFilter.Add("*");
await folderPicker.PickSingleFolderAsync();
You can use a specific extension like ".jpg", but it doesn't seem to have effect in a FolderPicker anyway. The only thing that matters is that at least one valid item is present.

How to get Image Account of user currently logged on Windows?

Searching on the web I found this code UWP, to get the image but I'm not be able to include namespace Windows.Storage in WPF. I tried to include all possible references but doesn't work. I would have a solution WPF to include Windows.Storage(if possible) or simply another way to implement code to get image
StorageFile image = UserInformation.GetAccountPicture(AccountPictureKind.SmallImage) as StorageFile;
if (image != null)
{
rootPage.NotifyUser("SmallImage path = " + image.Path, NotifyType.StatusMessage);
}
else
{
rootPage.NotifyUser("Small Account Picture is not available", NotifyType.StatusMessage);
mediaPlayer.Visibility = Visibility.Collapsed;
smallImage.Visibility = Visibility.Collapsed;
largeImage.Visibility = Visibility.Collapsed;
}
The Windows.​System.​User​Profile namespace is for UWP app development and does not apply to WPF.
You may be able to use it if you package (convert) your app to UWP: https://learn.microsoft.com/sv-se/windows/uwp/porting/desktop-to-uwp-supported-api#apis-supported-in-both-converted-apps-and-desktop-applications. Note that the UserInformation class is not supported on Windows 10 or later. Instead, you should use the User class.
In a .NET application, you could try to search the C:\Users\<user>\AppData\Roaming\Microsoft\Windows\Account Pictures folder for the latest modified file:
How to find the most recent file in a directory using .NET, and without looping?
Based on this answer if you take a look here
As long as you have the currently logged in username, you should be able to access it. This is the case for Win OS 7 at least, I don't know which OS you're working with.

Categories