当我调用下面的函数时,它应该创建一个 .txt 文件并在其中写一个句子。它似乎没有任何错误地执行。但是我找不到这个文件在创建后存储/定位的位置。我运行 Windows 搜索来查找该文件,但没有任何结果。该文件位于何处?另外,放置程序使用的 .txt 文件的最佳文件夹/位置是什么?我应该把它放在 Visual Studio 的 Solution Explorer 还是 Debug 文件夹中?
private async void CreateFile() {
try {
// Create sample file, replace if exists.
StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
StorageFile sampleFile = await storageFolder.CreateFileAsync("sample.txt", CreationCollisionOption.ReplaceExisting);
sampleFile = await storageFolder.GetFileAsync("sample.txt");
await FileIO.WriteTextAsync(sampleFile, "Swift as a shadow!");
} catch (Exception ex) {
textBox.Text = ex.ToString();
}
}
最佳答案
您可以让代码显示路径:
sampleFile = await storageFolder.GetFileAsync("sample.txt");
await new MessageDialog(sampleFile.Path).ShowAsync();
在 Windows 10 上,路径如下:
C:\Users\[username]\AppData\Local\Packages\[Package family name]\LocalState\sample.txt
其中 username 是登录用户的名称,package family name 是您的应用程序的 package family name
关于c# - 使用 CreateFileAsync 创建的文件在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57308865/