Donato Donato - 3 months ago 32
C# Question

UWP Can't convert from StorageFolder to StorageFile

so I'm trying to write a simple app that will let me create a text file with some content and file name then save it to a file, which can then be re-edited later. However for some reason I am getting 2 errors being new to c# and the whole UWP I'm not sure what I'm doing wrong any help with the following to errors is much appreciated.

private async void writebutton(object sender, RoutedEventArgs e)
{
String fileName = txtFileName.Text;
String text = txtContent.Text;
StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
(51) StorageFolder file = await localFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
(52) await FileIO.WriteTextAsync(file, text);
MessageDialog md = new MessageDialog("File saved" + fileName);
}


First error (line 51): Cannot implicitly convert type 'Windows.Storage.StorageFile' to 'Windows.Storage.StorageFolder'

Second error (line 52): Argument 1: cannot convert from 'Windows.Storage.StorageFolder' to 'Windows.Storage.IStorageFile'

Answer

This line is the problem.

(51) StorageFolder file = await localFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);

Change this to

(51) StorageFile file = await localFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);