【问题标题】:set access permission works in debug mode but not in release mode设置访问权限在调试模式下有效,但在发布模式下无效
【发布时间】:2019-03-18 09:43:41
【问题描述】:

我正在开发一个 UWP 软件,我需要在其中写入位于 Temp 目录中的“input.txt”文件。但是,当在发布模式下授予此目录权限时,我遇到了问题,并且看起来权限未设置:

        string str = inputmessage.Text;

        string path = @"input.txt";

        try
        {
            SetAccess(WindowsIdentity.GetCurrent().Name, 
            Path.GetTempPath());// Path.GetFullPath("."));

            // FileStream.SetAccessControl();
            File.WriteAllText(Path.GetTempPath()+path,str);
        }

并且设置访问权限定义为:

       private static bool SetAccess(string user, string folder)
    {
        const FileSystemRights Rights = FileSystemRights.FullControl;

        // *** Add Access Rule to the actual directory itself
        var AccessRule = new FileSystemAccessRule(user, Rights,
            InheritanceFlags.None,
            PropagationFlags.NoPropagateInherit,
            AccessControlType.Allow);

        var Info = new DirectoryInfo(folder);
        var Security = Info.GetAccessControl(AccessControlSections.Access);
        bool Result;

        Security.ModifyAccessRule(AccessControlModification.Set, AccessRule, out Result);

        if (!Result) return false;

        // *** Always allow objects to inherit on a directory
        const InheritanceFlags iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;

        // *** Add Access rule for the inheritance
        AccessRule = new FileSystemAccessRule(user, Rights,
            iFlags,
            PropagationFlags.InheritOnly,
            AccessControlType.Allow);

        Security.ModifyAccessRule(AccessControlModification.Add, AccessRule, out Result);

        if (!Result) return false;

        Info.SetAccessControl(Security);

        return true;
    }

【问题讨论】:

  • 你检查过 app.manifest 的权限了吗?

标签: c# visual-studio uwp file-permissions uwp-xaml


【解决方案1】:

FileSystemAccessRule属于System.Security.AccessControl命名空间,与uwp不属于compatible。你不能用它来访问TemporaryFolder

如果您想写入位于 Temp 目录中的“input.txt”文件。请参考以下流程。

private async void writeTextToTem(string info)
{
    var file = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("info.text", CreationCollisionOption.OpenIfExists);

    if (file != null)
    {
        await Windows.Storage.FileIO.WriteTextAsync(file, info);
    }
}

Path.GetTempPath()也可以在uwp中工作,匹配的文件夹是 C:\Users\Administrator\AppData\Local\Packages\497f6a93-9de3-4985-b27e-c2215ebabe72_75crXXXXXXX\AC\Temp\,它包含在应用程序的沙箱中,您可以直接访问它。

var path = Path.GetTempPath();
var folder = await StorageFolder.GetFolderFromPathAsync(path);
var file = await folder.CreateFileAsync("info.text", CreationCollisionOption.OpenIfExists);
if (file != null)
{
    await Windows.Storage.FileIO.WriteTextAsync(file, str);
}

更多详情可以参考File access permissions

【讨论】:

  • 第一个代码在发布模式下不起作用。在第二个代码中,您没有将任何字符串写入文件?
  • 我已将path 字符串写入文件。
  • 感谢您的第二个代码完美运行,但应将其修改为 Windows.Storage.FileIO.WriteTextAsync(file,str);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-19
  • 2023-01-31
  • 2021-05-20
  • 2014-02-18
相关资源
最近更新 更多