简体   繁体   English

写入文件我收到未经授权的访问错误。我该怎么做?

[英]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.

相关问题 为什么我在以编程方式使用键盘时会获得未经授权的访问? - Why do I get unauthorized access while using keyboard programmatically? 写入文件时未经授权的访问 - Unauthorized access when writing to file 如何搜索具有未授权访问权限的GitHub API存储库? - How do I search GitHub API repositories with unauthorized access? 如何保护WCF服务免受未经授权的访问? - How can I protect WCF service from unauthorized access? 如何获取目录中的所有文件以跳过未经授权的文件? - How can I get all files in a directory skipping unauthorized files? 删除FILE时出现“进程无法访问文件…”错误 - I get “Process can not access the file…” error while deleting the FILE .exe的未经授权的访问异常写入文件 - Unauthorized Access Exception writing file from .exe 写入文件以在C#中提供未经授权的访问 - Writing to file giving unauthorized access in c# 为什么我在通过 Azure AD App-Only 授予访问权限时收到 401 Unauthorized? - Why do I get 401 Unauthorized when granting access via Azure AD App-Only? 如何将数据放入 Json 文件中以获得构造函数属性? 我收到一个错误,名称、讲师、学分无法访问 - How I Put Data in Json FIle for a constractor property ? I get an error that Name ,Instructor, Credit can't access
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM