|
I have only begun to learn I started with vb but every time I search for an answer most of the results are in c# Is there a tut to un learn vb and convert my knowledge to C# BECAUSE I need relevant search results to learn from
Frazzle the name say's it all
|
|
|
|
|
Is shouldn't be too hard to learn C# if you already know VB.NET
The main difference is the syntax and naming conventions. Pretty much everything else is the same.
In the mean time there are some online converters you can use.
C# to VB.NET
VB.NET to C#
Here is a tut which will teach you the basics
After that just use the converters, enter a VB.NET statement and convert to see the C# syntax.
"You get that on the big jobs."
|
|
|
|
|
|
I will suggest you to refresh your C and C++ skills. As you know VB.Net, it will not be a big deal to get used to C#.
C# simply uses the syntaxes of C and C++ only.
Hope this helps.
All the best.
|
|
|
|
|
The code below works perfect with
removeUser("testuser")
but will not work with
removeUser("domain\test user name")
Does anyone have any idea as to why?
using System;
using System.IO;
using System.Text;
using Microsoft.Win32;
using System.Security;
using System.Threading;
using System.Reflection;
using System.Diagnostics;
using System.Configuration;
using System.Globalization;
using System.ComponentModel;
using System.DirectoryServices;
using System.Security.Principal;
using System.Collections.Generic;
using System.Security.Permissions;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Windows.Forms;
namespace AD
{
class ActiveDirectory
{
public static void removeUser(string userName)
{
try
{
string path = string.Format("WinNT://{0},computer", Environment.MachineName);
using (DirectoryEntry dirEntry = new DirectoryEntry(path))
{
DirectoryEntry groupEntry = dirEntry.Children.Find("Administrators", "group");
if (groupEntry != null)
{
DirectoryEntry userEntry = dirEntry.Children.Find(userName, "user");
groupEntry.Invoke("Remove", new object[] { userEntry.Path.ToString() });
groupEntry.CommitChanges();
userEntry.Dispose();
}
groupEntry.Dispose();
MessageBox.Show("Account Removed Successfully");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
|
|
|
|
|
|
Hi,
Assuming that was the correct link, the remove method is almost 100% identical to the code I posted ... am I missing something?
|
|
|
|
|
If you're in the Domain (the machine, I suppose, in the case of the local computer), prepending the Domain apparently doesn't work. Are you getting an exception, or is it just not doing anything?
..and water fell from the sky like rain.
|
|
|
|
|
I'm getting an error message
I've gotten a few when trying different things, one is that " the group could not be found (Exception from HRESULT:0x800708AC) " or " The user name could not be found (Exception from HRESULT:0x800708AD ")
|
|
|
|
|
Not finding a group or user is only an exception if you then try to use them.
Step through the code and check the values, see what you're really getting back from the searches.
..and water fell from the sky like rain.
|
|
|
|
|
Here is what I found that will work, with this code you can successfully remove a domain account from a local machine
The only problem with this is that I need to somehow enumerate through the names and only try and remove it if it matches a list? And maybe make it so that it is not case sensitive.
removeUser("domain", "group")
public static void removeUser(string domainValue, string adGroupAccount)
{
try
{
string path = string.Format("WinNT://{0},computer", Environment.MachineName);
using (DirectoryEntry dirEntry = new DirectoryEntry(path))
{
DirectoryEntry groupEntry = new DirectoryEntry("WinNT://" + System.Environment.MachineName + "/Administrators,group");
try
{
groupEntry.Invoke("Remove", "WinNT://" + domainValue + @"/" + adGroupAccount + ",user");
groupEntry.CommitChanges();
MessageBox.Show("Account Removed Successfully");
groupEntry.Dispose();
}
catch (Exception cex)
{
MessageBox.Show(cex.Message + "\n" + cex.StackTrace + "\n" + cex.Source);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
|
|
|
|
|
I don't think the username is case sensitive, anyway.
But to run it from a list, you'll have to iterate through the list and call the remove function for each name. As far as I know, there's no built-in way to do it.
..and water fell from the sky like rain.
|
|
|
|
|
You are right, I got a solution working and posted it as a reply to my original thread.
Thank you very much for your help.
|
|
|
|
|
It doesn't work because "domain est username" is invalid . You didn't escape your backslash. Plus, you also generally need 4 backslashes in front:
"\\\\domain\\test username"
Other issues could be your lack of permissions.
|
|
|
|
|
Hi,
Thanks for the response, I've added the slashes in front of the domain and the same thing happened, I do have permissions both locally and globally as I'm a local and a domain admin.
Any other thoughts?
|
|
|
|
|
Ok ...
Through the help of others here and my own due diligence, this is working now (I don't think you'll need any references added). While searching for an example, I saw many many questions on how to deal with domain accounts and local administrative groups, but no answers so I wanted to post this for googlers to save them some time.
Active directory class
using System;
using System.IO;
using System.Text;
using Microsoft.Win32;
using System.Security;
using System.Threading;
using System.Reflection;
using System.Diagnostics;
using System.Configuration;
using System.Globalization;
using System.ComponentModel;
using System.DirectoryServices;
using System.Security.Principal;
using System.Collections.Generic;
using System.Security.Permissions;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Windows.Forms;
using System.Collections;
namespace AD
{
class ActiveDirectory
{
public static void removeUser(string domainValue, string adGroupAccount)
{
try
{
string path = string.Format("WinNT://{0},computer", Environment.MachineName);
using (DirectoryEntry dirEntry = new DirectoryEntry(path))
{
DirectoryEntry groupEntry = new DirectoryEntry("WinNT://" + System.Environment.MachineName + "/Administrators,group");
try
{
groupEntry.Invoke("Remove", "WinNT://" + domainValue + @"/" + adGroupAccount + ",user");
groupEntry.CommitChanges();
MessageBox.Show("Account Removed Successfully");
groupEntry.Dispose();
}
catch (Exception cex)
{
MessageBox.Show(cex.Message + "\n" + cex.StackTrace + "\n" + cex.Source);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public static void addUser(string domainValue, string adGroupAccount)
{
try
{
string path = string.Format("WinNT://{0},computer", Environment.MachineName);
using (DirectoryEntry dirEntry = new DirectoryEntry(path))
{
DirectoryEntry groupEntry = new DirectoryEntry("WinNT://" + System.Environment.MachineName + "/Administrators,group");
try
{
groupEntry.Invoke("Add", "WinNT://" + domainValue + @"/" + adGroupAccount + ",user");
groupEntry.CommitChanges();
MessageBox.Show("Account Added Successfully");
groupEntry.Dispose();
}
catch (Exception cex)
{
MessageBox.Show(cex.Message + "\n" + cex.StackTrace + "\n" + cex.Source);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public static string userExists(string userName, string groupName)
{
string returnValue = "false";
try
{
DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
DirectoryEntry userGroup = localMachine.Children.Find(groupName, "group");
object members = userGroup.Invoke("members", null);
foreach (object groupMember in (IEnumerable)members)
{
DirectoryEntry member = new DirectoryEntry(groupMember);
if (member.Name.Equals(userName, StringComparison.CurrentCultureIgnoreCase))
{
returnValue = "true";
break;
}
}
}
catch (Exception ex)
{
returnValue = "false";
}
return (returnValue);
}
}
}
Click for an image of the form
Here is the code on how to run it with a simple form that has 3 buttons and 2 textbox input boxes. Put the group name and the domain into the boxes as so. I've hard coded the group I want to search through (Administrators) so you'll have to change that if you want to search through another group. The user exists is not case sensitive.
private void button2_Click(object sender, EventArgs e)
{
string domain = textBox1.Text;
string userName = textBox2.Text;
try
{
AD.ActiveDirectory.addUser(domain, userName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button3_Click(object sender, EventArgs e)
{
string domain = textBox1.Text;
string userName = textBox2.Text;
try
{
AD.ActiveDirectory.removeUser(domain, userName);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button4_Click(object sender, EventArgs e)
{
string domain = textBox1.Text;
string userName = textBox2.Text;
string result = AD.ActiveDirectory.userExists(userName, "Administrators");
MessageBox.Show("Exists equals " + result);
}
|
|
|
|
|
How is it possible to have a less than a second figure i.e. 0.5 or even less in the timespan.
At present I use every second as follows:
timer.Interval = new TimeSpan(0, 0, 0, 1);
Can I have a smaller figure than 1 in the interval ?
Thanks
|
|
|
|
|
Yes: just use the next overloaded constructor: MSDN[^]
timer.Interval = new TimeSpan(0, 0, 0, 0, 1);
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."
|
|
|
|
|
|
Who down voted you for a "thank you"? Compensated.
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
Digital man: "You are, in short, an idiot with the IQ of an ant and the intellectual capacity of a hose pipe."
|
|
|
|
|
Looks like whoever it was is a medium rep a***hole. I have countered the downvote you got for that. I wish whoever was targetting Akiboys would just grow up.
|
|
|
|
|
so you are aware of the TimeSpan type and need something from it you haven't needed before. Are you too lazy to look it up yourself? People should just stop answering lazy questions.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Luc Pattyn wrote: Are you too lazy to look it up yourself?
Why don't you form your own opinions! - This is exactly the kind of of thing I'm your talking about!
Illogical thoughts make me ill
|
|
|
|
|
I see the sense of humour black-hole as extended its gravity well to the C# forum. Tried to compensate, but not much effect.
|
|
|
|
|
Don't know why I bother sometimes... thanks thou
Illogical thoughts make me ill
|
|
|
|