【发布时间】:2021-03-07 15:57:51
【问题描述】:
您好,我正在尝试在 UWP 中创建一个 txt 文件,但出现此错误:
System.UnauthorizedAccessException:获取路径……
当我在控制台应用程序中使用代码时,它可以正常工作,但在 UWP 中却不行。 我尝试了 3 个位置来创建 txtfile。 AppData文件夹、U盘、项目文件夹
代码如下:
private void TextMethod()
{
//string file = @"E:\Logfiles\test.txt";
//string file = @"C:\Users\deeja\source\repos\UwpTxtTest\UwpTxtTest\bin\x86\Debug\test.txt";
string file = @"C:\Users\deeja\AppData\test.txt";
if (!File.Exists(file))
{
try
{
using (StreamWriter sw = new StreamWriter(file, true))
{
sw.WriteLine("Hello");
sw.WriteLine();
Debug.WriteLine("File created");
}
}
catch (Exception e)
{
Debug.WriteLine("Error = " + e);
}
}
else Debug.WriteLine("File already exists");
}
【问题讨论】:
-
这是help吗?
-
您好。我已经尝试了一点,它可以很好地创建文件。但它将文件放在某个随机文件夹中。如果我不能让其他代码工作,我会再次接近这个。谢谢
标签: c# uwp streamwriter