I am writing a program which create some share folders on a 2012 server. I have a service account :
NTAccount serviceAccount = new NTAccount("myDomain", "SA_LiPAM");
This initialization work well.
if (folder.IndexOf('\\') == 0)
{
Directory.CreateDirectory("\\\\" + serveurName + "\\Test-Projects\\" + pPrjName.Text + folder);// Creation
fs = Directory.GetAccessControl("\\\\" + serveurName + "\\Test-Projects\\" + pPrjName.Text + folder);
fs.SetOwner(serviceAccount);// Set owner
Directory.SetAccessControl("\\\\" + serveurName + "\\Test-Projects\\" + pPrjName.Text + folde, fs);
}
On execution evrything seems ok, but, when I look on Security>advanced of the folder the owner is the localadmin...
Which way to definetly change the owner of this folder ?
Edit : I was using "File" instead of "Directory" thats why it doesn't work.
Please try the following code. I have used this link C# - How to use DirectorySecurity.SetOwner() ? I'm having troubles to create it
if (folder.IndexOf('\\') == 0)
{
string dir = "\\\\" + serveurName + "\\Test-Projects\\" + pPrjName.Text + folder;
Directory.CreateDirectory(dir);
DirectoryInfo di = new DirectoryInfo(dir);
DirectorySecurity ds = di.GetAccessControl();
ds.SetOwner(serviceAccount);
FileSystemAccessRule permissions = new FileSystemAccessRule(serviceAccount, FileSystemRights.FullControl, AccessControlType.Allow);
ds.AddAccessRule(permissions);
di.SetAccessControl(ds);
}