当我在创建的文件夹中写入文件时始终访问被拒绝错误

时间:2016-10-26 10:57:06

标签: c# visual-studio-2015 create-directory directory-security

我正在使用Windows(在7和10中测试)

首先编程创建文件夹。之后,我想在创建的文件夹中写一个文件。但始终获得访问被拒绝错误。我试过操作系统中的每个目录。 C:,程序文件,文档和项目文件夹。总是我收到错误消息。每个代码都在下面,谢谢。

app.manifest

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

创建文件夹按钮方法

private void button2_Click(object sender, EventArgs e)
{
    string path = "FullControl";    // Or C:\\FullControl  > Doesn't matter same error
    bool exist = System.IO.Directory.Exists(path);
    if (!exist)
    {
        Directory.CreateDirectory(path);
    }
    var fInfo = new DirectoryInfo(path);
    var sid = new SecurityIdentifier(WellKnownSidType.WorldSid, null);
    var Security = fInfo.GetAccessControl(AccessControlSections.Access);
    Security.AddAccessRule(new FileSystemAccessRule(sid, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));

}

删除文件夹ReadOnly

    private void button3_Click(object sender, EventArgs e)
    {
        string path = "FullControl";
        var fInfo = new DirectoryInfo(path);
        FileAttributes f = fInfo.Attributes;
        DecipherAttributes(path, f);  //I dont now is it necessary ?
    }

创建和编写文件按钮方法

    private void button4_Click(object sender, EventArgs e)
    {
        string path = "FullControl";
        string filePath = path + "FullControl\\Example.html";

        if (!File.Exists(path))
        {
            try
            {
                StreamWriter tw = new StreamWriter(File.Open(path, FileMode.OpenOrCreate, FileAccess.Write), Encoding.UTF8);
                tw.WriteLine("The very first line!");
                tw.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        else if (File.Exists(path))
        {
            StreamWriter tw = new StreamWriter(File.Open(path, FileMode.Open, FileAccess.ReadWrite), Encoding.UTF8);
            tw.WriteLine("The next line!");
            tw.Close();
        }
    }

文件夹属性方法

    public static void DecipherAttributes(String path ,FileAttributes f)
    {
        // To set use File.SetAttributes

        File.SetAttributes(path, FileAttributes.ReadOnly);

        if ((f & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
            Console.WriteLine("ReadOnly");

        // To remove readonly use "-="
        f -= FileAttributes.ReadOnly;

        if ((f & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
            Console.WriteLine("ReadOnly");
        else
            Console.WriteLine("Not ReadOnly");
    }

1 个答案:

答案 0 :(得分:0)

我改变了这样的代码,并且它起了作用。我认为Path.Combine()是解决方案。感谢所有评论。

String FolderPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
FolderPath = Path.Combine(FolderPath,"TestFolder");
String file = Path.Combine(FolderPath,"test.html");

if (!File.Exists(file))
  {
    try
      {
          StreamWriter tw = new StreamWriter(File.Open(file, FileMode.OpenOrCreate, FileAccess.Write), Encoding.UTF8);
          tw.WriteLine("The very first line!");
          tw.Close();
          MessageBox.Show("File Created");
      }
      catch (Exception ex)
      {
          MessageBox.Show(ex.Message);
      }
   }