文件写入权限

时间:2012-02-07 17:49:06

标签: c# file-io file-permissions

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

流程,到目前为止:

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

问题是,一旦我达到我实际上必须在此文件中写内容的时间点我之前创建的我不能写一行,它就会引发我对“拒绝路径的访问”#34 ;

以下是与此问题相关的代码段:

        // 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是文件夹的路径,是的。

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

2 个答案:

答案 0 :(得分:2)

变化:

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

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

答案 1 :(得分:1)

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

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

相关问题