【问题标题】:C# UWP: copy file to folder inside local folder not functioning properlyC# UWP:将文件复制到本地文件夹内的文件夹无法正常运行
【发布时间】: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

标签: c# file-io uwp


【解决方案1】:

你看错地方了。我猜你正在查看项目的 bin 文件夹。

应用需要在启动前进行部署,其LocalFolder位于

%localappdata%\Packages\PACKAGENAME\LocalState

其中 PACKAGENAME 是包系列名称,格式为

YourApp_8wekyb3d8bbwe

您可以在Packaging 标签下查看项目的.appxmanifest

【讨论】:

    【解决方案2】:

    Click here,希望对你有所帮助。

    在此,您从openPicker.FileTypeFilter.Add(".jpg"); 更改为openPicker.FileTypeFilter.Add(".mp3");。如果您想接受所有文件,那么您只需编写这行代码openPicker.FileTypeFilter.Add("*");

    在我这边工作正常。 谢谢!!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-18
      • 1970-01-01
      相关资源
      最近更新 更多