【发布时间】:2018-09-07 19:26:23
【问题描述】:
我目前正在构建一个用于存储和显示音乐文件的音乐库应用程序。
应用程序将允许用户按下按钮,然后上传音频文件。我编写了代码,将音乐文件从用户的计算机复制到我创建的本地文件夹中的音乐文件夹中。我的代码似乎运行良好,没有明显的错误,但是,在查看应用程序的文件夹时,我看不到音乐文件夹或已放置在其中的文件。
这是我的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>
这可能是一个简单的修复,但在我的代码中是否有任何明显的内容不允许创建文件夹或复制文件?
【问题讨论】:
-
您是在检查
LocalState\musicfolder还是其他地方?我可以告诉你的代码是正确的,我可以看到文件复制到该文件夹。以防万一,此文件夹位于%localappdata%\Packages\PACKAGENAME\LocalState