question

Voytec avatar image
0 Votes"
Voytec asked Castorix31 commented

UWP C# Creating & replacing a file

My following code doesn't work:

 string[] createText = { "Test" };
 Windows.Storage.StorageFile file = await DownloadsFolder.CreateFileAsync("MyTest.txt", Windows.Storage.CreationCollisionOption.ReplaceExisting);
 await Windows.Storage.FileIO.WriteLinesAsync(file, createText, Windows.Storage.Streams.UnicodeEncoding.Utf8);

I ask for proffessional help.
Error: Parameter is not correct.

windows-uwp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Castorix31 avatar image
1 Vote"
Castorix31 answered Castorix31 commented

See the doc at DownloadsFolder.CreateFileAsync
with :
Because the app can only access files in the Downloads folder that it created, you can't specify OpenIfExists or ReplaceExisting for this parameter.



· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks @Castorix31
So how do I access a folder I can replace if existing, like Documents?
How do I reach documents if yes? I had some errors with it too.

0 Votes 0 ·

cause I can\'t find Documents Capabilities...

0 Votes 0 ·

You can use the same method as in this thread Create Folder Error In Metro UI

Something like :

 // using Windows.Storage.AccessCache; 
 Windows.Storage.StorageFile file;
 if (StorageApplicationPermissions.FutureAccessList.Entries.Any(item => item.Metadata == "MyTest.txt"))
 {
     var entry = StorageApplicationPermissions.FutureAccessList.Entries.FirstOrDefault(item => item.Metadata == "MyTest.txt");
     file = await StorageApplicationPermissions.FutureAccessList.GetFileAsync(entry.Token);
 }
 else
 {
     file = await Windows.Storage.DownloadsFolder.CreateFileAsync("MyTest.txt");
     StorageApplicationPermissions.FutureAccessList.Add(file, "MyTest.txt");
 }
 string[] createText = { "Test" };
 await Windows.Storage.FileIO.WriteLinesAsync(file, createText, Windows.Storage.Streams.UnicodeEncoding.Utf8);



0 Votes 0 ·