ファイルのアクセス権を取得する - C#
C#でファイルのアクセス権を取得するコードを紹介します。
概要
ファイルのアクセス権を取得するためには、.NET 5以降では、FileSystemAclExtensionsクラスのGetAccessControlメソッドを用います。
.NET Framework アプリケーションでは FileクラスのGetAccessControlメソッドを用います。
プログラム例:.NET 5以降
UI
下図のフォームを作成します。今回は button2 は利用しません。
コード
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;
namespace AccessPermissionGet
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string filePath = @"C:\develop\text.txt";
//見出し
string ace = "AccessControlType"
+ "\tAccountName"
+ "\tFileSystemRights"
+ "\tIsInherited";
System.Diagnostics.Debug.WriteLine(ace);
textBox1.Text += ace + "\r\n";
//アクセス権の列挙
FileInfo fi = new FileInfo(filePath);
FileSecurity security = FileSystemAclExtensions.GetAccessControl(fi);
/*
//または
FileStream fs = new FileStream(filePath,FileMode.Open);
FileSecurity security = FileSystemAclExtensions.GetAccessControl(fs);
*/
AuthorizationRuleCollection rules = security.GetAccessRules(true, true, typeof(NTAccount));
for (int i = 0; i < rules.Count; i++) {
FileSystemAccessRule fsar = (FileSystemAccessRule)rules[i];
ace = fsar.AccessControlType
+ "\t" + (fsar.IdentityReference as NTAccount).Value
+ "\t" + fsar.FileSystemRights.ToString()
+ "\t" + fsar.IsInherited.ToString();
textBox1.Text += ace + "\r\n";
}
/*
//foreach文を利用する場合
foreach (FileSystemAccessRule rule in security.GetAccessRules(true, true, typeof(NTAccount))) {
ace = rule.AccessControlType
+ "\t" + (rule.IdentityReference as NTAccount).Value
+ "\t" + rule.FileSystemRights.ToString()
+ "\t" + rule.IsInherited.ToString();
System.Diagnostics.Debug.WriteLine(ace);
textBox1.Text += ace +"\r\n";
}
*/
}
}
}
解説
アクセス権を取得するファイルのFileInfoオブジェクトを作成し、
FileSystemAclExtensions.GetAccessControl()
メソッドに
FileInfoオブジェクトを与えると、ファイルのアクセス権オブジェクトのFileSecurityオブジェクトが取得できます。
FileInfo fi = new FileInfo(filePath);
FileSecurity security = FileSystemAclExtensions.GetAccessControl(fi);
または、アクセス権を取得するファイルのファイルストリームを作成し、
FileSystemAclExtensions.GetAccessControl()
メソッドにファイルストリームを与える方法でも、
ファイルのアクセス権オブジェクトのFileSecurityオブジェクトが取得できます。
FileStream fs = new FileStream(filePath,FileMode.Open);
FileSecurity security = FileSystemAclExtensions.GetAccessControl(fs);
FileSecurityオブジェクトのGetAccessRulesメソッドを呼び出し、AuthorizationRuleCollection オブジェクトを取得し、
ループでコレクションの要素を取得すると、ファイルに設定されているアクセス権を取得できます。
AuthorizationRuleCollection rules = security.GetAccessRules(true, true, typeof(NTAccount));
for (int i = 0; i < rules.Count; i++) {
FileSystemAccessRule fsar = (FileSystemAccessRule)rules[i];
ace = fsar.AccessControlType
+ "\t" + (fsar.IdentityReference as NTAccount).Value
+ "\t" + fsar.FileSystemRights.ToString()
+ "\t" + fsar.IsInherited.ToString();
textBox1.Text += ace + "\r\n";
}
実行結果
プロジェクトを実行します。アプリケーションが起動し、下図のウィンドウが表示されます。
[button1]ボタンをクリックします。ファイルの権限が取得され、テキストボックスにファイルのアクセス権が表示されます。
プログラム例: .NET Framework
.NET Framework の Windows Formアプリケーションを作成します。
UI
下図のフォームを作成します。下のボタン(ディレクトリアクセス権の取得の[実行]ボタン)は今回のプログラムでは利用しません。
コード
下記コードを記述します。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AccessPermissionGetNetFramework
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void button_GetFileAccessPermission_Click(object sender, EventArgs e)
{
string filePath = @"C:\develop\text.txt";
//見出し
string ace = "AccessControlType"
+ "\tAccountName"
+ "\tFileSystemRights"
+ "\tIsInherited";
System.Diagnostics.Debug.WriteLine(ace);
textBox1.Text += ace + "\r\n";
//アクセス権の列挙
FileSecurity security = File.GetAccessControl(filePath);
AuthorizationRuleCollection rules = security.GetAccessRules(true, true, typeof(NTAccount));
for (int i = 0; i < rules.Count; i++) {
FileSystemAccessRule fsar = (FileSystemAccessRule)rules[i];
ace = fsar.AccessControlType
+ "\t" + (fsar.IdentityReference as NTAccount).Value
+ "\t" + fsar.FileSystemRights.ToString()
+ "\t" + fsar.IsInherited.ToString();
textBox1.Text += ace + "\r\n";
}
/*
//foreach文を利用する場合
foreach (FileSystemAccessRule rule in security.GetAccessRules(true, true, typeof(NTAccount))) {
ace = rule.AccessControlType
+ "\t" + (rule.IdentityReference as NTAccount).Value
+ "\t" + rule.FileSystemRights.ToString()
+ "\t" + rule.IsInherited.ToString();
System.Diagnostics.Debug.WriteLine(ace);
textBox1.Text += ace +"\r\n";
}
*/
}
}
}
解説
File.GetAccessControl()
メソッドを呼び出してFileSecurityオブジェクトを取得します。
FileSecurity security = File.GetAccessControl(filePath);
FileSecurityオブジェクトの
GetAccessRules()
メソッドを呼び出し、
AuthorizationRuleCollection
オブジェクトを取得します。
このコレクションにファイルへのアクセス権限の情報が記録されています。
AuthorizationRuleCollection rules = security.GetAccessRules(true, true, typeof(NTAccount));
ループ処理で取得した AuthorizationRuleCollection オブジェクトの要素にアクセスし、ファイルのアクセス権を取得してテキストボックスに表示します。
for (int i = 0; i < rules.Count; i++) {
FileSystemAccessRule fsar = (FileSystemAccessRule)rules[i];
ace = fsar.AccessControlType
+ "\t" + (fsar.IdentityReference as NTAccount).Value
+ "\t" + fsar.FileSystemRights.ToString()
+ "\t" + fsar.IsInherited.ToString();
textBox1.Text += ace + "\r\n";
}
実行結果
上記のプロジェクトを実行します。下図のウィンドウが表示されます。
[ファイルアクセス権取得]の[実行]ボタンをクリックします。ファイルのアクセス権を取得し結果がテキストボックスに表示されます。
著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
掲載日: 2010-11-21
改訂日: 2022-11-03