我正在寻找一种如何以编程方式创建本地用户组的方法.我发现了大量关于如何查询和添加用户的示例,但我无法理解如何创建新组.
var dirEntry = new DirectoryEntry(
"WinNT://" + Environment.MachineName + ",computer");
/* Code to test if the group already exists */
if (!found)
{
DirectoryEntry grp = dirEntry.Children.Add(groupName, "Group");
dirEntry.CommitChanges();
}
Run Code Online (Sandbox Code Playgroud)
这就是我所得到的,但我知道这是错误的,CommitChanges()只是抛出一个NotImplementedException.
我一直在使用它作为样本,但我甚至无法让它工作(感谢MS):
http://msdn.microsoft.com/en-us/library/ms815734
任何人都有一个我可以用来创建一个新的本地组的代码片段?
我有很多像这样的代码:
FileStream fs = File.Open(@"C:\Temp\SNB-RSS.xml", FileMode.Open);
using (XmlTextReader reader = new XmlTextReader(fs))
{
/* Some other code */
}
Run Code Online (Sandbox Code Playgroud)
这给了我以下代码分析警告:
CA2000 : Microsoft.Reliability : In method 'SF_Tester.Run()', object 'fs' is not disposed along all exception paths. Call System.IDisposable.Dispose on object 'fs' before all references to it are out of scope.
Run Code Online (Sandbox Code Playgroud)
如果我按照建议操作并将File.Open放在using语句中,我会得到:
CA2202 : Microsoft.Usage : Object 'fs' can be disposed more than once in method 'SF_Tester.Run()'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time …Run Code Online (Sandbox Code Playgroud)