File.WriteAllText()方法抛出类型为'System.UnauthorizedAccessException'的异常 [英] File.WriteAllText() method is throwing an exception of type 'System.UnauthorizedAccessException'

查看:65
本文介绍了File.WriteAllText()方法抛出类型为'System.UnauthorizedAccessException'的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi,

以下是为下载网页源代码而编写的一段代码,其uri作为参数传递,然后将此源代码复制到硬盘上创建的文件中。但是, 
File.WriteAllText()方法是投掷 - " 类型的未处理的异常"System.UnauthorizedAccessException的'发生在mscorlib.dll中。对"D:\ Temp"路径的访问被拒绝。我尝试过对该文件夹的完全控制访问权以及以管理员身份运行Visual Studio的
,但没有用。能提前帮助你吗?

Following is the piece of code written to download page source of a website whose uri is passed as parameter and then copy this source code to file created on hard-drive. However,  File.WriteAllText() method is throwing - "An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll. Access to the path 'D:\Temp' is denied." I have tried giving full control access to the folder and also running Visual Studio as administrator, but no use. Could you please help, thanks in advance.

       WebClient客户端=新WebClient();

        string pageSource = client.DownloadString(uri);

        File.WriteAllText(filePath,pageSource);

       WebClient client = new WebClient();
        string pageSource = client.DownloadString(uri);
        File.WriteAllText(filePath, pageSource);

推荐答案

阿联酋表示许可问题。尝试使用您应该有权写入的路径并查看它是否有效。 

A UAE indicates permission issues. Try using a path that you should have permission to write to and see if it works. 

您没有提供有关创建filePath本身的任何信息,因此其他问题包括是否路径格式正确,如果您的应用程序在沙盒中运行,其中事物被锁定等等。此外,D:表示您有2个硬
驱动器和第二个上的Temp文件夹。你确定路径是否正确?

You didn't provide any information around the creation of filePath itself so other questions include whether the path is properly formatted, if your app is running in a sandbox where things are locked down, etc. Also, the D: would indicate you have 2 hard drives and a Temp folder on the second one. Are you sure the path is correct?

我要看的其他内容包括使用资源管理器中的有效权限来验证帐户是否具有权限,然后在.NET代码中尝试更简单的方法只是尝试先读取该文件夹中的文件。

Other things I'd look at include using the effective permissions in Explorer to verify the account has permissions and then in the .NET code trying something simpler like simply trying to read a file in that folder first.


这篇关于File.WriteAllText()方法抛出类型为'System.UnauthorizedAccessException'的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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