带UWP的Zip文件夹

时间:2018-09-20 14:20:21

标签: c# uwp zip

几天前,我问了一个有关压缩文件的问题……下面给出了答案,我认为它的使用是理所当然的,但实际上却给我一个错误“访问路径'C:\ folder path'被拒绝。”。如何正确压缩文件夹?

MainPage.xaml

<Grid>
    <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
        <StackPanel Orientation="Horizontal" Margin="5">
            <Button x:Name="BtnChooseFolder" Click="BtnChooseFolder_Click" Content="Choose Folder" Margin="5"/>
            <TextBlock Text="Folder to Zip: " VerticalAlignment="Center"/>
            <TextBlock x:Name="TxbFolderToZip" VerticalAlignment="Center"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal" Margin="5">
            <Button x:Name="BtnChooseDestination" Click="BtnChooseDestination_Click" Content="Choose Destination" Margin="5"/>
            <TextBlock Text="Zip Folder: " VerticalAlignment="Center"/>
            <TextBlock x:Name="TxbZipFolder" VerticalAlignment="Center"/>
        </StackPanel>
        <StackPanel Orientation="Horizontal">
            <Button x:Name="BtnZip" Click="BtnZip_Click" Content="Zippa" Margin="10"/>
            <TextBlock x:Name="TxbPercentage" VerticalAlignment="Center"/>
        </StackPanel>
    </StackPanel>
</Grid>

MainPage.xaml.cs

string DestinationFolderPath = string.Empty;
string SourceFolderPath = string.Empty;

StorageFolder SourceFolder;
StorageFolder DestinationFolder;

private async void BtnChooseFolder_Click(object sender, RoutedEventArgs e)
{
    FolderPicker FolderPickFol = new FolderPicker();
    FolderPickFol.SuggestedStartLocation = PickerLocationId.Desktop;
    FolderPickFol.FileTypeFilter.Add("*");
    Windows.Storage.StorageFolder SelectFolderToZipa = await FolderPickFol.PickSingleFolderAsync();
    StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolder", SelectFolderToZipa);
    SourceFolder = SelectFolderToZipa;
    SourceFolderPath = SelectFolderToZipa.Path;
    TxbFolderToZip.Text = SourceFolderPath;
}

private async void BtnChooseDestination_Click(object sender, RoutedEventArgs e)
{
    FolderPicker FolderPickFol = new FolderPicker();
    FolderPickFol.SuggestedStartLocation = PickerLocationId.Desktop;
    FolderPickFol.FileTypeFilter.Add("*");
    StorageFolder SelectFolderToZipa = await FolderPickFol.PickSingleFolderAsync();
    StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedDestination", SelectFolderToZipa);
    DestinationFolder = SelectFolderToZipa;
    DestinationFolderPath = SelectFolderToZipa.Path;
    TxbZipFolder.Text = DestinationFolderPath;
}

private async void BtnZip_Click(object sender, RoutedEventArgs e)
{
    if (SourceFolder != null)
    {
        StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", SourceFolder);
        await Task.Run(() =>
        {
            try
            {
                System.IO.Compression.ZipFile.CreateFromDirectory(SourceFolder.Path, $"{DestinationFolder.Path}\\{SourceFolder.Name}.zip");
            }
            catch (Exception w)
            {

            }
        });
    }
}
  

(System.IO.Compression.ZipFile.CreateFromDirectory(SourceFolder.Path,$“ {DestinationFolder.Path} \ {SourceFolder.Name} .zip”))拒绝对这两个路径的访问。怎么解决?

总是提前感谢。

0 个答案:

没有答案