【问题标题】:UWP FileStream throws "Access to path is denied."UWP FileStream 抛出“访问路径被拒绝”。
【发布时间】:2019-12-10 15:21:43
【问题描述】:

我正在上大学,我被授予一项任务来制作一个应用程序,该应用程序可以计算并显示 UWP 应用程序中文件的哈希值。每次当我想计算我选择的文件的哈希值时,我都会得到“访问路径被拒绝”。错误。我想计算将文件流作为参数传递的哈希值。我尝试以管理员身份运行 Visual Studio,但没有成功。下面是代码。

public partial class MainPage : Page
{
    byte[] result;
    string[] algorithms = { "MD5", "SHA-1", "SHA256", "SHA384", "SHA512" };
    string algorithm = "", path = "";
    HashAlgorithm hash;

    public MainPage()
    {
        this.InitializeComponent();

        AlgorithmsList.ItemsSource = algorithms;
    }

    /* Browse for file */
    private async void BrowseButton(object sender, RoutedEventArgs e)
    {
        FileOpenPicker openPicker = new FileOpenPicker();

        openPicker.ViewMode = PickerViewMode.Thumbnail;
        openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
        openPicker.FileTypeFilter.Add("*");

        StorageFile file = await openPicker.PickSingleFileAsync();

        if (file != null)
        {
            resultTextBlock.Text = "Result";
            pathTextBlock.Text = file.Path;
            path = file.Path;
        }
    }

    /* Method that shows the hash after computing it */
    private async void GoButton(object sender, RoutedEventArgs e)
    {
        if (path == "" || AlgorithmsList.SelectedIndex == -1)
        {
            MessageDialog dialog;

            if (path == "")
                dialog = new MessageDialog("You have to select a file");
            else
                dialog = new MessageDialog("You have to select an algorithm");

            await dialog.ShowAsync();
        }
        else
        {
            algorithm = AlgorithmsList.Text;
            string hash = await Task.Run(() => CalculateHash());
            resultTextBlock.Text = hash;
        }
    }

    private string CalculateHash()
    {
        string exception = "";

        hash = InitAlgorithm();

        try
        {
            using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                fileStream.Position = 0;
                result = hash.ComputeHash(fileStream);
            }

            StringBuilder sb = new StringBuilder(result.Length * 2);
            foreach (byte b in result)
            {
                sb.AppendFormat("{0:x2}", b);
            }

            return sb.ToString();
        }
        catch (Exception e)
        {
            exception = e.ToString();
        }

        return exception;
    }

    private HashAlgorithm InitAlgorithm()
    {
        HashAlgorithm hash = null;

        switch (algorithm)
        {
            case ("MD5"):
                hash = MD5.Create();
                break;
            case ("SHA-1"):
                hash = SHA1.Create();
                break;
            case ("SHA256"):
                hash = SHA256.Create();
                break;
            case ("SHA384"):
                hash = SHA384.Create();
                break;
            case ("SHA512"):
                hash = SHA512.Create();
                break;
        }

        return hash;
    }
}

【问题讨论】:

  • 您应该保存选择器返回的 StorageFile 并通过该 StorageFile 打开流。因为它是具有访问权限的 StorageFile。 FileAccess sample 显示了如何执行此操作。当它被选中时,它将StorageFile 保存在rootPage.sampleFile 中,然后当它想从文件中读取时,它使用rootPage.sampleFile.OpenAsync

标签: c# visual-studio uwp


【解决方案1】:

请不要使用路径直接创建FileStream,UWP应用对通过路径访问文件有严格限制。

您已经通过FileOpenPicker获得了StorageFile对象,请使用它代替path变量,并使用如下代码:

using (var stream = await file.OpenStreamForReadAsync())
{
    stream.Position = 0;
    result = hash.ComputeHash(stream);
}

StringBuilder sb = new StringBuilder(result.Length * 2);
foreach (byte b in result)
{
    sb.AppendFormat("{0:x2}", b);
}

return sb.ToString();

最好的问候。

【讨论】:

    【解决方案2】:

    您无权访问该路径。您只能访问StorageFile。所以让它在MainPage 中可变并在CalculateHash 中使用。

    【讨论】:

    • 你说得对,谢谢!直到 Richard Zhang 向我展示代码时才知道如何使用它。
    猜你喜欢
    • 1970-01-01
    • 2018-09-13
    • 2014-05-11
    • 1970-01-01
    • 2022-01-21
    • 2018-07-19
    • 2016-11-11
    相关资源
    最近更新 更多