Writing a file into the Downloads folder with UWP

2020-07-18 10:00发布

I need to create a file in the downloads folder for a UWA on Windows 10 and copy the content of an existing file into it. I use the following code:

StorageFile cleanFile = await Windows.Storage.DownloadsFolder.CreateFileAsync(cleanFileName);

await file.CopyAndReplaceAsync(cleanFile);

This works ok, but the folder the file is stored is this:

C:\Users\MyUser\Downloads\e15e6523-22b7-4188-9ccf-8a93789aa8ef_t8q2xprhyg9dt!App\WordComment-clean.docx

I assume this is some type of Isolated Storage. But really, that is not what I need. Since the user can't see the file like this.

2条回答
甜甜的少女心
2楼-- · 2020-07-18 10:04

From MSDN:

User’s Downloads folder. The folder where downloaded files are saved by default.

By default, your app can only access files and folders in the user's Downloads folder that your app created. However, you can gain access to files and folders in the user's Downloads folder by calling a file picker (FileOpenPicker or FolderPicker) so that users can navigate and pick files or folders for your app to access.

If you don't use a file picker, you are saving the file in your app folder in Downloads.

Souce

Using the Download folder

For using the download folder, the user needs to select the download folder manually.

FolderPicker picker = new FolderPicker { SuggestedStartLocation = PickerLocationId.Downloads };
    picker.FileTypeFilter.Add("*");
    StorageFolder folder = await picker.PickSingleFolderAsync();
    if (folder != null) {
           await folder.CreateFileAsync("Hello1.txt");  
    }

I hope this can help you.

查看更多
劫难
3楼-- · 2020-07-18 10:05

Try this: await file.CopyAndReplaceAsync(cleanFile); await file.RenameAsync("the name you want")

查看更多
登录 后发表回答