我目前正在构建一个将存储和显示音乐文件的音乐库应用程序。
该应用程序将允许用户按下一个按钮,然后上传一个音频文件。我编写了代码,将音乐文件从用户的计算机复制到我创建的本地文件夹内的音乐文件夹中。我的代码似乎运行良好,没有明显的错误,但是,当查看应用程序的文件夹内部时,我没有看到音乐文件夹或已放置在其中的文件。
这是我的 MainPage.xaml.cs
代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
namespace MusicLibraryTest
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
//Music Library is opened on user's computer and displays all available mp3 files
var picker = new Windows.Storage.Pickers.FileOpenPicker
{
ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail,
SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.MusicLibrary
};
picker.FileTypeFilter.Add(".mp3");
picker.FileTypeFilter.Add(".mp4");
picker.FileTypeFilter.Add(".m4a");
var file = await picker.PickSingleFileAsync();
var folder = ApplicationData.Current.LocalFolder;
var musicFolder = await folder.CreateFolderAsync("musicfolder", CreationCollisionOption.OpenIfExists);
//put file in future access list so it can be accessed when application is closed and reopened
Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(file);
//File is copied to local folder for use in music library
if (folder != null && file != null)
{
await file.CopyAsync(musicFolder, file.Name, NameCollisionOption.GenerateUniqueName);
}
}
}
}
我的代码来自 MainPage.xaml
:
<Page
x:Class="MusicLibraryTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MusicLibraryTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Button Content="Add Music" HorizontalAlignment="Left" Margin="750,407,0,0" VerticalAlignment="Top" Click="Button_Click" />
</Grid>
</Page>
这可能是一个简单的修复,但我的代码中是否有明显不允许复制文件夹创建或文件的内容?
最佳答案
你看错地方了。我猜你正在查看项目的 bin 文件夹。
应用在启动前需要部署,其LocalFolder
位于
%localappdata%\Packages\PACKAGENAME\LocalState
其中 PACKAGENAME 是
形式的包系列名称YourApp_8wekyb3d8bbwe
您可以在项目的 .appxmanifest
中检查,在 Packaging
选项卡下。
关于C# UWP : copy file to folder inside local folder not functioning properly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52228560/