【问题标题】:File Writing Permission文件写入权限
【发布时间】:2012-02-29 04:06:40
【问题描述】:

我正在编写一个应用程序来将内容转储到文件中。

流程,到目前为止是这样的:

1 - User enters a Path

2 - App checks if the Directory Exists and if i have Writting Permissions

 2.a - If if exists, creates a empty file inside and closes it
 2.b - If it does not, creates the directory and the file inside it, and closes the file

问题是,一旦我实际必须在我之前创建的这个文件中写入内容时,我不能写一行,它会抛出“拒绝访问路径”。

这是与问题相关的代码sn-p:

        // Writting ImgPixels to file

        if (BDC.BDCCommons.CommonUtils.DirectoryHasWritePermissions(folderPath))
        {
            using (StreamWriter filePointer = File.AppendText(folderPath))
            {
                filePointer.WriteLine(imgPixels);
            }
        }
        else
        {
            LogWriter.Error ("Permissão Negada", "Diretorio [ " + folderPath + "] nao tem permissao de escrita");
        }

这可能是什么原因?! 先谢谢各位了

改进问题:

@500 - 内部服务器错误:folderPath 是文件夹的路径,是的。

我想我知道我的错误在哪里。没有要写入的文件,只是一个文件夹。我的坏

【问题讨论】:

  • Hmm - "folderPath" 是文件夹的名称(仅)还是整个文件名?
  • 您在文件夹上调用File.AppendText()
  • 是using语句还是filePointer.WriteLine语句出错?
  • @EdwardThomson 正确,我传递的是文件夹路径而不是文件路径。谢谢

标签: c# file-io file-permissions


【解决方案1】:

变化:

using (StreamWriter filePointer = File.AppendText(folderPath))

using (StreamWriter filePointer = File.AppendText(Path.Combine(folderPath, fileName)))

【讨论】:

  • 最好使用Path.Combine(folderPath, fileName)
【解决方案2】:

根据您尝试写入文件的位置,您可能需要管理员权限才能执行此操作。我在一个项目中工作,我必须将文件写入 Program Files 文件夹的子目录,但遇到了权限问题。

我找到了this 的解决方案。它描述了如何编辑项目的清单文件,以便在启动时授予应用程序的访问权限。

【讨论】:

  • 这不是问题,我也编写了自己的代码来在需要时获取这些权限,以便检查我是否对路径具有写权限的代码。谢谢你,我会读一读的;D
猜你喜欢
  • 2015-12-31
  • 2011-01-20
  • 2011-08-21
  • 2016-12-24
  • 1970-01-01
  • 1970-01-01
  • 2011-11-14
  • 1970-01-01
  • 2012-06-06
相关资源
最近更新 更多