-1

私はウィンドウを使用しています(7と10でテスト)作成されたフォルダ内にファイルを書き込むと常にアクセス拒否エラーが発生する

最初にプログラムを作成するフォルダです。その後、作成したフォルダの中にファイルを書きたい。しかし、常にアクセス拒否エラーが発生します。私はオペレーティングシステムのすべてのディレクトリを試しました。 C:、プログラムファイル、ドキュメント、プロジェクトフォルダ。常に私はそのエラーメッセージを受け取ります。すべてのコードは以下のとおりです。ありがとうございます。

app.manifest

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

フォルダの読み取り専用

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 ? 
    } 

Fileボタン方式

を作成し、書く
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(); 
     } 
    } 
0123を削除するフォルダボタン方式

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

} 

を作成します。

フォルダは、私は、このようなコードを変更する方法

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

ファイルを開くときにfilePathに正しい変数を使用するだけです。 –

+0

私はそれを前に試して、私は同じエラーを取得しました。でもありがとう。 @AlexeiLevenkov –

+0

ユーザやアプリケーションは、悪意のあるコードやひどく書かれたコードが重要なシステムやアプリケーションファイルを上書きしないように、 'C:'や 'Program Files'に書き込むべきではありません。ユーザーは、ユーザーのDocumentsフォルダに文書を書き込むか、ProgramDataフォルダに文書を書き込む必要があります。あなたは何を保存しようとしていますか? –

答えて

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); 
     } 
    } 
+0

@HenkHoltermanああそうです。それはテストコードですが、私は今変更しています。 CommonApplicationDataフォルダを使用しています。ありがとうございました。 –

関連する問題