C# 设置访问权限可以在调试模式下工作,但不能在发布模式下工作

C# 设置访问权限可以在调试模式下工作,但不能在发布模式下工作,c#,visual-studio,uwp,file-permissions,uwp-xaml,C#,Visual Studio,Uwp,File Permissions,Uwp Xaml,我正在开发一个UWP软件,我需要在其中写入位于临时目录中的“input.txt”文件。但是,在发布模式下授予此目录权限时,我遇到了问题,似乎权限未设置: string str = inputmessage.Text; string path = @"input.txt"; try { SetAccess(WindowsIdentity.GetCurrent().Name, Pat

我正在开发一个UWP软件,我需要在其中写入位于临时目录中的“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;
    }
是属于名称空间,而不是与uwp。您无法使用它来访问

如果要写入临时目录中的“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);
    }
}
也可以在uwp中工作,匹配的文件夹是
C:\Users\Administrator\AppData\Local\Packages\497f6a93-9de3-4985-b27e-C2215EBEBE72\U 75CRXXXXXX\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);
}

有关更多详细信息,请参阅。

是否已检查app.manifest的权限?第一个代码在发布模式下不起作用。在第二段代码中,您没有将任何字符串写入文件?我已将
路径
字符串写入文件。谢谢,您的第二段代码工作正常,但应将其修改为Windows.Storage.FileIO.WriteTextAsync(file,str);