[ad_1]
フォルダーを作成し、そのフォルダーを全員で共有して、全員に許可を与える必要があります。
共有のためにこれを試しましたが、機能しません(プロパティを確認したときにフォルダーが共有されていません)
How-to-Share-Windows-Folders-Using-C [^]
フォルダーのアクセス許可について、これを試しましたが、
private static void SetPermissions(string dirPath) { DirectoryInfo info = new DirectoryInfo(dirPath); WindowsIdentity self = System.Security.Principal.WindowsIdentity.GetCurrent(); DirectorySecurity ds = info.GetAccessControl(); ds.AddAccessRule(new FileSystemAccessRule(self.Name, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow)); info.SetAccessControl(ds); }
これが正しくない場合は、別のアプローチを支援してください。
前もって感謝します
解決策 1
フォルダーに ACL を設定しますが、共有は構成しません。
さらに、個々のアカウントでアクセス リストを構成することは、管理者にとって悪夢です。 適切な権限を持つグループを作成し、ユーザーをグループに追加してください。 次に、個人ではなくグループの ACL を設定するだけです。 誰かが去り、そのパーミッションを破棄する必要がある場合は、そのユーザーをグループから削除するだけです。
単純な古い net share コマンドを実行する以外に、C# から共有を作成する方法がわかりません。
net share [share_name]=[path]
次のように入力して、net share コマンドの詳細情報を取得できます。 net share /?
コンソールで。
解決策 4
private static void GrantAccess(文字列ファイル)
{
bool exists = System.IO.Directory.Exists(ファイル);
もし (! が存在する)
{
DirectoryInfo di = System.IO.Directory.CreateDirectory(ファイル);
Console.WriteLine(“フォルダが正常に作成されました”);
}
それ以外
{
Console.WriteLine(“フォルダは既に存在します”);
}
DirectoryInfo dInfo = 新しい DirectoryInfo(ファイル);
DirectorySecurity dSecurity = dInfo.GetAccessControl();
dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
dInfo.SetAccessControl(dSecurity);
}
解決策 2
参照: C#.NET を使用してフォルダに「Everyone」権限を追加する[^]
DirectorySecurity sec = Directory.GetAccessControl(path); // Using this instead of the "Everyone" string means we work on non-English systems. SecurityIdentifier everyone = new SecurityIdentifier(WellKnownSidType.WorldSid, null); sec.AddAccessRule(new FileSystemAccessRule(everyone, FileSystemRights.Modify | FileSystemRights.Synchronize, InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow)); Directory.SetAccessControl(path, sec);
お役に立てれば!
解決策 3
これを管理者として実行します。 プロジェクトにマニフェスト ファイルを追加します。
ShareFolder メソッドはディレクトリを作成し、それを共有しますが、共有権限は読み取り専用です。 この権限を変更するには、メソッド SetPermission を呼び出します。
System.Management.dll への参照を追加
using System; using System.Management; using System.IO; public string ShareFolder(string FolderPath, string ShareName, string Description) { string strSharePath = FolderPath; string strShareName = ShareName; string strShareDesc = Description; string msg = string.Empty; try { Directory.CreateDirectory(strSharePath); ManagementClass oManagementClass = new ManagementClass("Win32_Share"); ManagementBaseObject inputParameters = oManagementClass.GetMethodParameters("Create"); ManagementBaseObject outputParameters; inputParameters["Description"] = strShareDesc; inputParameters["Name"] = strShareName; inputParameters["Path"] = strSharePath; inputParameters["Type"] = 0x0;//disk drive inputParameters["MaximumAllowed"] = null; inputParameters["Access"] = null; inputParameters["Password"] = null; outputParameters = oManagementClass.InvokeMethod("Create", inputParameters, null); if ((uint)(outputParameters.Properties["ReturnValue"].Value) != 0) { msg = "There is a problem while sharing the directory."; throw new Exception("There is a problem while sharing the directory."); } else { msg = ("Share Folder has been created with the name :" + strShareName); } } catch (Exception ex) { msg = (ex.Message.ToString()); } return msg; }
このメソッドはパーミッションを設定します。
システム ドメインを取得します。
string Domain = Environment.UserDomainName;
AddPermission メソッドで、前のメソッドで設定した shareName と同じ sharedFolderName か、権限を変更するディレクトリの共有名を渡します。 userName フィールドには、権限を設定/変更するアカウントの名前を使用します。 例 – 全員、管理者など
using System; using System.Management; using System.Globalization; public void AddPermissions(string sharedFolderName, string domain, string userName) { // Step 1 - Getting the user Account Object ManagementObject sharedFolder = GetSharedFolderObject(sharedFolderName); if (sharedFolder == null) { System.Diagnostics.Trace.WriteLine("The shared folder with given name does not exist"); return; } ManagementBaseObject securityDescriptorObject = sharedFolder.InvokeMethod("GetSecurityDescriptor", null, null); if (securityDescriptorObject == null) { System.Diagnostics.Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, "Error extracting security descriptor of the shared path {0}.", sharedFolderName)); return; } int returnCode = Convert.ToInt32(securityDescriptorObject.Properties["ReturnValue"].Value); if (returnCode != 0) { System.Diagnostics.Trace.WriteLine(string.Format(CultureInfo.InvariantCulture, "Error extracting security descriptor of the shared path {0}. Error Code{1}.", sharedFolderName, returnCode.ToString())); return; } ManagementBaseObject securityDescriptor = securityDescriptorObject.Properties["Descriptor"].Value as ManagementBaseObject; // Step 2 -- Extract Access Control List from the security descriptor int existingAcessControlEntriesCount = 0; ManagementBaseObject[] accessControlList = securityDescriptor.Properties["DACL"].Value as ManagementBaseObject[]; if (accessControlList == null) { // If there aren't any entries in access control list or the list is empty - create one accessControlList = new ManagementBaseObject[1]; } else { // Otherwise, resize the list to allow for all new users. existingAcessControlEntriesCount = accessControlList.Length; Array.Resize(ref accessControlList, accessControlList.Length + 1); } // Step 3 - Getting the user Account Object ManagementObject userAccountObject = GetUserAccountObject(domain, userName); ManagementObject securityIdentfierObject = new ManagementObject(string.Format("Win32_SID.SID='{0}'", (string)userAccountObject.Properties["SID"].Value)); securityIdentfierObject.Get(); // Step 4 - Create Trustee Object ManagementObject trusteeObject = CreateTrustee(domain, userName, securityIdentfierObject); // Step 5 - Create Access Control Entry ManagementObject accessControlEntry = CreateAccessControlEntry(trusteeObject, false); // Step 6 - Add Access Control Entry to the Access Control List accessControlList[existingAcessControlEntriesCount] = accessControlEntry; // Step 7 - Assign access Control list to security desciptor securityDescriptor.Properties["DACL"].Value = accessControlList; // Step 8 - Assign access Control list to security desciptor ManagementBaseObject parameterForSetSecurityDescriptor = sharedFolder.GetMethodParameters("SetSecurityDescriptor"); parameterForSetSecurityDescriptor["Descriptor"] = securityDescriptor; sharedFolder.InvokeMethod("SetSecurityDescriptor", parameterForSetSecurityDescriptor, null); } /// <summary> /// The method returns ManagementObject object for the shared folder with given name /// </summary> /// <param name="sharedFolderName">string containing name of shared folder</param> /// <returns>Object of type ManagementObject for the shared folder.</returns> private static ManagementObject GetSharedFolderObject(string sharedFolderName) { ManagementObject sharedFolderObject = null; //Creating a searcher object to search ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * from Win32_LogicalShareSecuritySetting where Name = '" + sharedFolderName + "'"); ManagementObjectCollection resultOfSearch = searcher.Get(); if (resultOfSearch.Count > 0) { //The search might return a number of objects with same shared name. I assume there is just going to be one foreach (ManagementObject sharedFolder in resultOfSearch) { sharedFolderObject = sharedFolder; break; } } return sharedFolderObject; } /// <summary> /// The method returns ManagementObject object for the user folder with given name /// </summary> /// <param name="domain">string containing domain name of user </param> /// <param name="alias">string containing the user's network name </param> /// <returns>Object of type ManagementObject for the user folder.</returns> private static ManagementObject GetUserAccountObject(string domain, string alias) { ManagementObject userAccountObject = null; ManagementObjectSearcher searcher = new ManagementObjectSearcher(string.Format("select * from Win32_Account where Name = '{0}' and Domain='{1}'", alias, domain)); ManagementObjectCollection resultOfSearch = searcher.Get(); if (resultOfSearch.Count > 0) { foreach (ManagementObject userAccount in resultOfSearch) { userAccountObject = userAccount; break; } } return userAccountObject; } /// <summary> /// Returns the Security Identifier Sid of the given user /// </summary> /// <param name="userAccountObject">The user object who's Sid needs to be returned</param> /// <returns></returns> private static ManagementObject GetAccountSecurityIdentifier(ManagementBaseObject userAccountObject) { ManagementObject securityIdentfierObject = new ManagementObject(string.Format("Win32_SID.SID='{0}'", (string)userAccountObject.Properties["SID"].Value)); securityIdentfierObject.Get(); return securityIdentfierObject; } /// <summary> /// Create a trustee object for the given user /// </summary> /// <param name="domain">name of domain</param> /// <param name="userName">the network name of the user</param> /// <param name="securityIdentifierOfUser">Object containing User's sid</param> /// <returns></returns> private static ManagementObject CreateTrustee(string domain, string userName, ManagementObject securityIdentifierOfUser) { ManagementObject trusteeObject = new ManagementClass("Win32_Trustee").CreateInstance(); trusteeObject.Properties["Domain"].Value = domain; trusteeObject.Properties["Name"].Value = userName; trusteeObject.Properties["SID"].Value = securityIdentifierOfUser.Properties["BinaryRepresentation"].Value; trusteeObject.Properties["SidLength"].Value = securityIdentifierOfUser.Properties["SidLength"].Value; trusteeObject.Properties["SIDString"].Value = securityIdentifierOfUser.Properties["SID"].Value; return trusteeObject; } /// <summary> /// Create an Access Control Entry object for the given user /// </summary> /// <param name="trustee">The user's trustee object</param> /// <param name="deny">boolean to say if user permissions should be assigned or denied</param> /// <returns></returns> private static ManagementObject CreateAccessControlEntry(ManagementObject trustee, bool deny) { ManagementObject aceObject = new ManagementClass("Win32_ACE").CreateInstance(); aceObject.Properties["AccessMask"].Value = 0x1U | 0x2U | 0x4U | 0x8U | 0x10U | 0x20U | 0x40U | 0x80U | 0x100U | 0x10000U | 0x20000U | 0x40000U | 0x80000U | 0x100000U; // all permissions aceObject.Properties["AceFlags"].Value = 0x0U; // no flags aceObject.Properties["AceType"].Value = deny ? 1U : 0U; // 0 = allow, 1 = deny aceObject.Properties["Trustee"].Value = trustee; return aceObject; }
から実用的な解決策をダウンロードできます ここ.
[ad_2]
コメント