UWP 应用不会将文件复制到 AppData 文件夹 [英] UWP app does not copy file to AppData folder

查看:37
本文介绍了UWP 应用不会将文件复制到 AppData 文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个 C# 可执行文件,它制作了一个 test 文件夹并将 test.txt 文件从它的执行文件夹复制到 AppData 文件夹.这是我的代码:

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);
}

然后我使用本文中的模板制作 appxmanifest.xml 文件 手动打包应用.我使用 makeappxsigntool 制作了一个 UWP 包.但该可执行文件不会创建 test 文件夹,也不会将 test.txt 文件复制到 AppData 文件夹.我不想在 Visual Studio 中使用 UWP 项目来实现它.我应该在 appxmanifest.xml 文件中添加一些额外的行吗?

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?

推荐答案

实际上该应用程序按预期工作!UWP 桌面桥的目标是将 UWP 的主要优势带入经典桌面应用程序.其中一项好处是安全性和易于卸载的能力.

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.

经典桌面应用程序存在以下问题:能够访问磁盘上任何位置的文件,尤其是能够在用户不知情的情况下在任何地方写入.卸载此类应用程序后在硬盘驱动器和注册表中留下许多不必要的痕迹,然后 PC 逐渐变得越来越杂乱.

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.

UWP 应用的目标是当它们被卸载时,它们完全消失,不会在磁盘上留下任何痕迹.

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

为了实现这一点,UWP 桌面桥虚拟化了一些文件系统路径.请参阅 Desktop Bridge 文档中的文件系统部分,您可以在其中阅读以下内容:

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:

为了包含应用状态,桥会尝试捕获应用对 AppData 所做的更改.所有写入用户 AppData 文件夹(例如,C:\Users\user_name\AppData)的内容,包括创建、删除和更新,都会在写入时复制到每个用户、每个应用程序的私有位置.这会产生一种错觉,即打包的应用在实际修改私有副本时正在编辑真实的 AppData.

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.

您在代码中执行的写入确实有效,但它们并未写入 AppData\Roaming 文件夹,而是写入此文件夹的虚拟副本,您可以在以下位置找到:>

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\

您的应用 ID 由包名称和生成的 ID 组成.您通常可以通过按修改日期对文件夹进行排序来更快地找到文件夹.

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.

LocalCache\Roaming 文件夹中,您将找到您创建的 test\test.txt 文件.如果您尝试从该文件读取,读取将再次从此位置虚拟化.

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.

如果您想使用完整路径访问文件,您可以使用 StorageFile API 检索它:

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" ));

但是,此操作的先决条件是您添加对 UWP API 的引用.这很好地描述了 在这篇博文中.

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

这篇关于UWP 应用不会将文件复制到 AppData 文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆