【发布时间】:2021-05-30 04:37:46
【问题描述】:
我知道这是这里很多相关主题的常见问题。但它们似乎都不适合我。
我的代码可以在生产系统上运行,我已将其复制到本地家用计算机:
private static void WriteToLog(string logText, string logPath)
{
try
{
using (StreamWriter outputFile = File.AppendText(logPath))
{
outputFile.WriteLine(DateTime.Now.ToString() + "|| " + Regex.Replace(logText, @"\t|\n|\r", ""));
}
}
catch (IOException ex)
{
//what do?
throw ex;
}
}
using (StreamWriter outputFile = File.AppendText(logPath)) 行抛出经典异常:
System.UnauthorizedAccessException: '访问路径 'C:\Users\Jaso\Documents\DataChecker_Logs\schema_a-academic_attainment.txt' 被拒绝。'
在运行时路径变量包含"C:\\Users\\Jaso\\Documents\\DataChecker_Logs\\schema_a-academic_attainment.txt"
相关文件夹的安全性如下所示:
当我找到使用WindowsIdentity.GetCurrent().Name; 运行进程的用户时,返回的值为"DESKTOP-LMMBET3\\Jaso",根据文件夹的设置(屏幕截图上方),它是具有完全控制权的主体!!
Windows 10 机器。
GRRR!!!
【问题讨论】:
-
“但它们似乎都不适合我。” ...所以您尝试在管理员模式下运行 VS 并且仍然收到此异常?你到底尝试了什么?
-
yup 尝试在管理员模式下运行,尝试授予访问权限,但进程的用户已经拥有访问权限,我发现的其余解决方案与人们错误地尝试覆盖整个文件夹或不包括路径中的文件名有关等不适用于我@BrettCaswell
-
你怎么打电话给
WriteToLog?您的程序是在创建Shell还是在执行Process来调用该方法? -
@BrettCaswell 不确定 Shell 是什么,但代码中的一个方法调用了这个方法,所以我认为做一个调用的 Process
标签: c# windows file io file-permissions