簡體   English   中英

當我在創建的文件夾中寫入文件時,總是出現訪問被拒絕錯誤

[英]Always Getting Access Denied Error When I Write A File Inside Created Folder

我正在使用Windows(在7和10中測試)

然后程序首先創建文件夾。 在那之后,我想在創建的文件夾內寫一個文件。 但是總是會出現訪問被拒絕的錯誤。 我嘗試了操作系統中的每個目錄。 C:,程序文件,文檔和項目文件夾。 我總是收到該錯誤消息。 每個代碼都在下面,謝謝。

應用清單

<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));

}

只讀刪除文件夾

    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");
    }

我像這樣更改代碼,並且可以正常工作。 我認為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);
      }
   }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM