简体   繁体   中英

Create file during mstest - System.UnauthorizedAccessException

I have a UWP C# app, with a unit testing project. In these unit test, I want to be able to write to a text file in order to make something like snapshots in Jest .

Directory.GetCurrentDirectory() returns C:\\path\\to\\project\\bin\\x64\\Debug\\AppX , so I made a folder in the project directory and am navigating to it, then attempting to create a file there.

[TestMethod]
public void Test()
{
    var folder = Path.Combine(Directory.GetCurrentDirectory(), "../../../../Snapshots");
    string data = "example data";
    string filename = Path.Combine(folder, "Test.snap");
    File.WriteAllText(filename, json);
}

However, this test produces a System.UnauthorizedAccessException . I went into the folder in windows and gave Everyone read/write permissions, but that didn't make any difference.

I don't want to have to run Visual Studio as an administrator. Is this possible?

Please have a look at Rob's blog here: https://blogs.msdn.microsoft.com/wsdevsol/2012/12/04/skip-the-path-stick-to-the-storagefile/

Here is the answer from Rob:

Windows Store apps run sandboxed and have very limited access to the file system. For the most part, they can directly access only their install folder and their application data folder. They do not have permission to access the file system elsewhere (see File access and permissions for more details).

Access to other locations is available only through a broker process. This broker process runs with the user's full privileges, and it can use these privileges on the app's behalf for locations the app has requested via capabilities, locations requested by the user via file pickers, etc. The StorageItem encapsulates this brokerage procedure so the app doesn't need to deal with it directly."

In a UWP app we do not recommend path anymore. There are permission problems so broker is required when access some paths. I'm not familar with Unit Test. But if you are still using UWP function you should consider using StorageFile related API instead.

I use Path.GetTempPath() to create temporary directories and files in unit tests that require physical disk access. The unit tests can run from an unknown context/location, so I found using the temp directory as a guaranteed way to create disposable files.

[TestMethod]
public void Test()
{
    var folder = Path.Combine(Path.GetTempPath(), "Snapshots");
    string data = "example data";
    string filename = Path.Combine(folder, "Test.snap");
    File.WriteAllText(filename, json);
}

How about checking if you gave permissions to the right folder?

        var folder = Path.Combine(Directory.GetCurrentDirectory(), "../../../../Snapshots");
        string data = "example data";

        // this variable will contain the actual folder; add a watch
        // or bookmark it to check it
        var actualPath = Path.GetFullPath(folder);

        string filename = Path.Combine(folder, "Test.snap");
        File.WriteAllText(filename, data);

Just in case, add the line below too (before File.WriteAllText ); perhaps your file already exists as, I don't know, read-only:

        File.SetAttributes(filename, FileAttributes.Temporary);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM