[英]Writing to a file I get unauthorized access error.How can I do it?
Despite numerous post on the web I cannot find an answer to my problem.尽管 web 上有很多帖子,但我找不到我的问题的答案。 I am writing an application that writes csv files to folders.Users should be able to pick a directory.
我正在编写一个将 csv 文件写入文件夹的应用程序。用户应该能够选择一个目录。 I am developing in windows 7 using vs2010 running my app in Admin Mode.Regardless of all this I still get the "Unauthorized access exception" when I do
我正在 windows 7 中使用 vs2010 在管理员模式下运行我的应用程序进行开发。无论如何,当我这样做时,我仍然会收到“未经授权的访问异常”
var path=@"c:\" or c:\MyFolder
StringBuilder sb=new StringBuilder();
sb.AppendLine("Test");
var myFile=sb.ToString();
using (var writer=new StreamWriter(path))
{
writer.Write(myFile);
}
Am I missing something?我错过了什么吗? I have feeling that in window7 you can only write to designated folders.Is this what's happening?
我感觉在window7中你只能写入指定的文件夹。这是怎么回事?
any suggestions?有什么建议么?
EDITED已编辑
I have created few folders under "C:\MyFolder\"我在“C:\MyFolder\”下创建了几个文件夹
I am not using any credentials eg windows impersonation etc..我没有使用任何凭据,例如 windows 模拟等。
It does write if it writes to the bin\debug\ of my class library.如果它写入我的 class 库的 bin\debug\,它确实会写入。 but not to any designated folder.
但不是任何指定的文件夹。
Is your code snippet the real code causing the problem?您的代码片段是导致问题的真实代码吗?
On the face of it, you are trying to stream the text "Test" into a directory on the file system, not trying to write a file.从表面上看,您是在尝试将文本“Test”放入文件系统上的目录中,而不是尝试写入文件。 (
path
is just assigned to @"C:\"
). (
path
刚刚分配给@"C:\"
)。 I'm not surprised that you get an UnauthorizedAccessException
.我对您收到
UnauthorizedAccessException
并不感到惊讶。
Assign the full path of the file you want to write into your path
variable, and I imagine you'll succeed.将要写入的文件的完整路径分配到
path
变量中,我想你会成功。
Try running your app with "Run as Administrator".尝试使用“以管理员身份运行”运行您的应用程序。 The comments above will probably also steer you in the right direction.
上面的评论可能也会引导你朝着正确的方向前进。 You should definitely pick a directory that your windows users has access to edit.
您绝对应该选择您的 windows 用户有权编辑的目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.