UWP app does not copy file to AppData folder

时光毁灭记忆、已成空白 提交于 2020-01-03 03:23:12

问题


I made a C# executable which make a test folder and copy test.txt file from it's execution folder to AppData folder. Here is my code:

static void Main() 
{
    string fullPath = $"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}\\test";
    string destination = $"{fullPath}\\test.txt";

    Directory.CreateDirectory(fullPath);
    string location = System.Reflection.Assembly.GetExecutingAssembly().Location;
    int index = location.LastIndexOf("\\");
    string source = $"{location.Substring(0, index)}\\test.txt";
    File.Copy(source, destination);
}

Then I make appxmanifest.xml file using the template from this article Package an app manually. I make a UWP package with makeappx and signtool. But that executable does not make the test folder nor copy the test.txt file to AppData folder. I don't want to make it with a UWP project in Visual Studio. Should I add some extra lines in appxmanifest.xml file?


回答1:


Actually the app works as intended! Goal of UWP Desktop Bridge is to bring the main benefits of UWP to classic desktop apps. One of those benefits is safety and ability to uninstall easily.

Classic desktop apps have had the problem of being able to access files anywhere on the disk and especially being able to write anywhere without the user's knowledge. Uninstalling such apps then left behind many unnecessary traces on the hard drive and in registry and the PC then gradually became more and more cluttered.

Goal of UWP apps is that when they are uninstalled, they are gone completely, without a trace left on the disk.

To achieve this, UWP Desktop Bridge virtualizes some File System paths. See the File System section in documentation of Desktop Bridge where you can read the following:

In order to contain app state, the bridge attempts to capture changes the app makes to AppData. All write to the user's AppData folder (e.g., C:\Users\user_name\AppData), including create, delete, and update, are copied on write to a private per-user, per-app location. This creates the illusion that the packaged app is editing the real AppData when it is actually modifying a private copy.

The writes you performed in your code did actually work, but they did not write into the AppData\Roaming folder, but to a virtualized counterpart of this folder, which you can find in:

AppData\Local\Packages\{your app's ID}\LocalCache\Roaming\

Where your app's ID consists of the Package name and generated ID. You can usually find the folder faster by sorting folders by date of modification.

In the LocalCache\Roaming folder you will find the test\test.txt file you created. If you try to read from that file the read will again be virtualized from this location.

If you want to access the file using full path, you can retrieve it using the StorageFile APIs:

var filePath = Path.Combine( ApplicationDate.Current.LocalCacheFolder.Path, 
          "Roamingtest\test.exe" ));

The prerequisite for this is however that you add references to the UWP APIs. This is well described in this blogpost.



来源:https://stackoverflow.com/questions/48849076/uwp-app-does-not-copy-file-to-appdata-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!