简体   繁体   English

UWP应用程序不会将文件复制到AppData文件夹

[英]UWP app does not copy file to AppData folder

I made a C# executable which make a test folder and copy test.txt file from it's execution folder to AppData folder. 我制作了一个C#可执行文件,该文件可以创建一个test文件夹,并将test.txt文件从其执行文件夹复制到AppData文件夹。 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 . 然后,使用本文中的模板制作appxmanifest.xml文件。 手动打包应用程序 I make a UWP package with makeappx and signtool . 我用makeappxsigntool制作了一个UWP软件包。 But that executable does not make the test folder nor copy the test.txt file to AppData folder. 但是该可执行文件不会创建test文件夹,也不test.txt文件复制到AppData文件夹。 I don't want to make it with a UWP project in Visual Studio. 我不想用Visual Studio中的UWP项目来实现。 Should I add some extra lines in appxmanifest.xml file? 我应该在appxmanifest.xml文件中添加一些额外的行吗?

Actually the app works as intended! 实际上,该应用程序可以按预期工作! Goal of UWP Desktop Bridge is to bring the main benefits of UWP to classic desktop apps. UWP Desktop Bridge的目标是将UWP的主要优点带给经典的桌面应用程序。 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. 卸载此类应用程序后,会在硬盘驱动器和注册表中留下许多不必要的痕迹 ,然后PC变得越来越混乱。

Goal of UWP apps is that when they are uninstalled, they are gone completely , without a trace left on the disk. UWP应用程序的目标是,在卸载它们时,它们会完全消失 ,而磁盘上不会留下任何痕迹。

To achieve this, UWP Desktop Bridge virtualizes some File System paths. 为此,UWP Desktop Bridge虚拟化了一些文件系统路径。 See the File System section in documentation of Desktop Bridge where you can read the following: 请参阅Desktop Bridge文档中的“ 文件系统”部分 ,您可以在其中阅读以下内容:

In order to contain app state, the bridge attempts to capture changes the app makes to AppData. 为了包含应用程序状态,网桥尝试捕获应用程序对AppData所做的更改。 All write to the user's AppData folder (eg, C:\\Users\\user_name\\AppData), including create, delete, and update, are copied on write to a private per-user, per-app location. 所有写入用户的AppData文件夹(例如C:\\ Users \\ user_name \\ AppData),包括创建,删除和更新,都将在写入时复制到每个用户,每个应用程序的私有位置。 This creates the illusion that the packaged app is editing the real AppData when it is actually modifying a private copy. 这会产生一种幻觉,即打包的应用实际上在修改私有副本时正在编辑实际的AppData。

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 \\ Roaming文件夹,而是写入了该文件夹的虚拟副本,您可以在以下文件夹中找到它们:

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

Where your app's ID consists of the Package name and generated ID. 应用的ID由程序包名称和生成的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. LocalCache \\ Roaming文件夹中,您将找到创建的test \\ test.txt文件。 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: 如果要使用完整路径访问文件,则可以使用StorageFile API进行检索:

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. 但是,前提条件是您必须添加对UWP API的引用。 This is well described in this blogpost . 这个博客文章对此进行了很好的描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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