UWP 应用程序不会将文件复制到 AppData 文件夹
UWP app does not copy file to AppData folder
我制作了一个 C# 可执行文件,它创建了一个 test
文件夹,并将 test.txt
文件从它的执行文件夹复制到 AppData 文件夹。这是我的代码:
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);
}
然后我使用本文 Package an app manually 中的模板制作 appxmanifest.xml 文件。我用 makeappx
和 signtool
制作了一个 UWP 包。但是该可执行文件不会创建 test
文件夹,也不会将 test.txt
文件复制到 AppData 文件夹。我不想在 Visual Studio 中使用 UWP 项目来实现它。我应该在 appxmanifest.xml 文件中添加一些额外的行吗?
实际上,该应用程序按预期运行! UWP Desktop Bridge 的目标是将 UWP 的主要优势引入经典桌面应用程序。这些好处之一是安全和易于卸载的能力。
经典桌面应用程序存在能够访问磁盘上任何位置的文件的问题,尤其是能够在用户不知情的情况下在任何地方写入。卸载这些应用程序然后 在硬盘驱动器和注册表中留下许多不必要的痕迹,然后 PC 逐渐变得越来越混乱。
UWP 应用程序的目标是当它们被卸载时,它们完全消失,在磁盘上没有留下任何痕迹。
为此,UWP Desktop Bridge 虚拟化了一些文件系统路径。请参阅 Desktop Bridge 文档中的 File System section,您可以在其中阅读以下内容:
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 文件夹,而是写入此文件夹的虚拟化副本,您可以在以下位置找到:
AppData\Local\Packages\{your app's ID}\LocalCache\Roaming\
您的应用 ID 由程序包名称和生成的 ID 组成。您通常可以通过按修改日期对文件夹进行排序来更快地找到文件夹。
在 LocalCache\Roaming 文件夹中,您会找到您创建的 test\test.txt 文件。如果您尝试从该文件读取,读取将再次从该位置虚拟化。
如果您想使用完整路径访问文件,您可以使用 StorageFile
API 检索它:
var filePath = Path.Combine( ApplicationDate.Current.LocalCacheFolder.Path,
"Roamingtest\test.exe" ));
然而,这样做的先决条件是您添加对 UWP API 的引用。这很好描述in this blogpost。
我制作了一个 C# 可执行文件,它创建了一个 test
文件夹,并将 test.txt
文件从它的执行文件夹复制到 AppData 文件夹。这是我的代码:
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);
}
然后我使用本文 Package an app manually 中的模板制作 appxmanifest.xml 文件。我用 makeappx
和 signtool
制作了一个 UWP 包。但是该可执行文件不会创建 test
文件夹,也不会将 test.txt
文件复制到 AppData 文件夹。我不想在 Visual Studio 中使用 UWP 项目来实现它。我应该在 appxmanifest.xml 文件中添加一些额外的行吗?
实际上,该应用程序按预期运行! UWP Desktop Bridge 的目标是将 UWP 的主要优势引入经典桌面应用程序。这些好处之一是安全和易于卸载的能力。
经典桌面应用程序存在能够访问磁盘上任何位置的文件的问题,尤其是能够在用户不知情的情况下在任何地方写入。卸载这些应用程序然后 在硬盘驱动器和注册表中留下许多不必要的痕迹,然后 PC 逐渐变得越来越混乱。
UWP 应用程序的目标是当它们被卸载时,它们完全消失,在磁盘上没有留下任何痕迹。
为此,UWP Desktop Bridge 虚拟化了一些文件系统路径。请参阅 Desktop Bridge 文档中的 File System section,您可以在其中阅读以下内容:
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 文件夹,而是写入此文件夹的虚拟化副本,您可以在以下位置找到:
AppData\Local\Packages\{your app's ID}\LocalCache\Roaming\
您的应用 ID 由程序包名称和生成的 ID 组成。您通常可以通过按修改日期对文件夹进行排序来更快地找到文件夹。
在 LocalCache\Roaming 文件夹中,您会找到您创建的 test\test.txt 文件。如果您尝试从该文件读取,读取将再次从该位置虚拟化。
如果您想使用完整路径访问文件,您可以使用 StorageFile
API 检索它:
var filePath = Path.Combine( ApplicationDate.Current.LocalCacheFolder.Path,
"Roamingtest\test.exe" ));
然而,这样做的先决条件是您添加对 UWP API 的引用。这很好描述in this blogpost。