|
If you want to interrogate Active Directory through the .NET framework then you can use either System.DirectoryServices or the System.Management namespaces. System.Management uses WMI to query AD. For either technique you need to supply a username and password to gain access to AD with and to perform queries.
You might need to store the username and password of a user who has enough previleages to access and update Active Directory in an encrypted format in some configuration file.
To give an example of using System.DirectoryServices here is a sample code which determines which users are part of the Domain Admins group.
DirectoryEntry de= new DirectoryEntry();
de.Path = "LDAP://clown.acme.com"; //clown is the server name hosting the domain controller & acme.com is the domain name.
de.Username = "administrator";
de.Password = "passw$rd";
de.AuthenticationType = AuthenticationTypes.Secure
//now lets search for a user using the above credentials
try
{
DirectorySearcher searcher = new DirectorySearcher();
searcher .SearchRoot = de;
searcher .Filter = "(cn=" + "Domin Admins" + ")";
SearchResultCollection results = searcher .FindAll();
Console.Writeline(results.Count.ToString())
}
catch (Exception exception)
{
Console.Writeline(exception.Message);
}
Further refer to this MSDN reference for more information:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sds/sds/quick_list_for_c__code_examples.asp[^]
Just a warning Active Directory can be a pain at times.
|
|
|
|
|
Hello everyone,
I am trying to pass an instance of a serial port to an instance of a class declared in the same scope. When I pass the port in, either by reference or value, I keep getting a null reference exception. The serial port works fine when used outside of the target object, but I can't get it to work from within that object.
Here is what I mean...
private System.IO.Ports.SerialPort serialPort1; //the serial port declared within Form1
private InterfaceCommuncation Communicator; //the object that needs to use the port
//port initialization stuff (abridged)
serialPort1.PortName = "COM1";
serialPort1.BaudRate = 38400;
this.serialPort1.Open(); //this line works fine, no errors
this.serialPort1.Close(); //this line works fine, no errors
//both of these lines cause the same error
//Communicator.initialize(serialPort1);
//Communicator.initialize(this.serialPort1);
******
//This is how the serial port is used in the communicator class
public void initialize(System.IO.Ports.SerialPort sp)
{
sp.Open();
sp.Close();
}
The null reference error occurs inside this function, wether I pass by reference or value. What is the proper way to pass a serial port to another object?
Thanks in advance!
|
|
|
|
|
Hi,
SerialPort is a class, hence a reference type.
You can declare SerialPort references as much as you like, and copy them,
and pass them from one object to another; but you can only create SerialPort
instances (i.e. real objects) by using the "new" keyword.
"new" is not present in the code you have shown, if it is not there at all
then anything you try to do to serialPort1 is doomed to fail with a
NullReferenceException.
Also relevant could be the following MSDN remark on SerialPort.Open:
"The best practice for any application is to wait for some amount of time after
calling the Close method before attempting to call the Open method, as the port
may not be closed instantly."
Since they don't specify "some amount of time" this is not very practical;
the better approach may be to open the port only once.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Luc,
Thank you for the reply. I have tried using new in the following way, with the same error...
1. making a new instance, and passing it by reference
2. making a new instance, and passing it by value
3. making a new instance, and passing it by both ways to another new instance inside of the target class
Any more suggestions? I greatly appreciate your help.
I also noted the MSDN remark and changed the open/close stuff.
Regards,
Chris
|
|
|
|
|
murr007 wrote: Any more suggestions?
Sure, these two: stop wild experiments and:
1. show the code for one of them, preferably the one you consider the most likely
2. buy a book on C# and work your way through it; books tend to teach you stuff
in a logical order, with simple examples.
BTW if you show code, put it inside PRE tags so we can enjoy the indentation.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Hi all,
I have a small problem with keyboard input. I have a textbox and I would like to prohibit writing characters(only numbers are allowed).
Do you have any ideas?
Thanks.
David
|
|
|
|
|
Add a Keypress event handler to the textbox, and do this:
if (!char.IsDigit(e.KeyChar))
{
e.Handled = true;
} This assumes that the event args is called e.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
It works!!!
e.Handled = true; what that means?
Thank you.
David
|
|
|
|
|
It tells the textbox that the key was handled (was taken care of) and no need for the textbox to handled it
|
|
|
|
|
Perfect!!
Thank you very much.
David
|
|
|
|
|
I've been writing a chat client, which uses an extended RichTextBox as the chat area.
The RichTextBox takes simple XHTML code (including styles) and converts it into Rtf and displays it. This works great for almost everything.
The problem arises when you try to use extended UTF-8 characters, most notably Japanese Hiragana and Katakana, but also things such as the musical Sharp symbol or the interrobang symbol. These are being displayed as question marks in the RichTextBox.
They exist without problems in the input XHTML; and using a standard RichTextBox I am able to display them correctly. The problem arises when I insert them via the translated XHTML.
Does anyone know of a way that I can preserve the characters when converting to Rtf?
|
|
|
|
|
Hey all I have developed an application that makes use of the upnp.dll, but for some reason my application works fine on some computers but there are a select few that it doesn't work on and I get this error message:
System.Runtime.InteropServices.COMException (0x80070422): Creating an instance of the COM component with CLSID {E2085F28-FEB7-404A-B8E7-E659BDEAAA02} from the IClassFactory failed due to the following error: 80070422.
at [ApplicationName].ServerDetection.StartAsyncTypeSearch()
Can anyone shed some light on what is happening I can't seem to pin this error down.
Any help on this will be highly valued.
Freedom is the right to say that 2+2=5 if this is so everything else will follow.
|
|
|
|
|
Have you made sure the upnp.dll is registered when you install your app on the other computers. Try using regsvr32 on your problem machines.
|
|
|
|
|
Thanks for your reply Mark,
I've check and the DLL has definitely been registered on the problem PCs
Having a hard time tracking down this problem
Freedom is the right to say that 2+2=5 if this is so everything else will follow.
|
|
|
|
|
how can i design a custom Typed Dataset? i create new class from System.Data.dataset and add datatable to it, but it doesn't add datatable list in DataMember!!!
<br />
using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.Data;<br />
<br />
namespace MultiTier<br />
{<br />
class MyDataset : System.Data.DataSet<br />
{<br />
public MyDataset()<br />
: base()<br />
{<br />
Table1 = new DataTable();<br />
}<br />
<br />
private DataTable Table1;<br />
public DataTable Table1DataTable<br />
{<br />
get { return Table1; }<br />
}<br />
}<br />
}<br />
|
|
|
|
|
Hi. Can anyone help me with understanding how to make a trial version of a program, e.g. it works for 10 days and then refuses to work, until user enters a serial key.
Is there an article about how to do that with examples in c# ? By "Examples" i mean pieces of code where the whole mechanism is written. E.g. how to generate keys. How to disalow user to use program for more than 10 days by simply reinstalling it ?
Thanks in advance
|
|
|
|
|
Most people do these though their distribution system such as Install Shield which take your applications binary files and wraps it's usage control around it...
I don't have an example of how this is done in C# but the methood would be to set a obscure reg key when it is first run that encodes the date...
Easy to crack though with a tool like "procmon" from sysinternals (you can see the reg key entry and delete or reset it to when it worked)
You could call it to goto the web but that can be spoofed (easy with %systemroot%/system32/etc/host) too...
Install shield isn't the best method, but will be harder to crack than most home made stuff...
as there are tools to "unwrap" install shield from a program...
best way to limit use on a program is have two versions. One with limited functionally, and another that is paid and have the program "track back" with in-program registration to verify the identity of the user. (CPU SN, Volume ID, and WMP user ID are good to build a GUID off of)
|
|
|
|
|
In my win-form, I am binding a textbox to a property, populated through my object. The property returns a number. Let's say 55.5
I want to add a % sign at the end of this 55.5 without changing the decimal or any of the significant figures.
How can i do it?
Here is what i know so far:
In the binding panel in the text property, under the "Format type" menu, there is a "Custom" option. It has Custom format textbox. If we put the % sign in here in a correct way, it will just be added to the end of the value of the property. So far, 0.0% or 0.00% has not worked as it changes the decimal and significant figures.
Any insight will be appreciated.
|
|
|
|
|
Are you adding this for a wildcard in SQL?
Or do you just want it to "display" with a percentage when they are done with the text box?
Do you want to show a confirmation with a percent sign?
-----------------------------------------------------------
Completion Deadline: two days before the day after tomorrow
|
|
|
|
|
I just want a "%" sign with the number to identify it as a percentage value.
|
|
|
|
|
Put a label next to the text box that says "%"
|
|
|
|
|
well this way, % would be always displayed even if the values have not come back. If, you have some method based on the form control, then that would be helpful.
Thanks
|
|
|
|
|
In your original post you wrote, "The property returns a number. Let's say 55.5" Would it possible to change your property from whatever "number" type it is (double , decimal , etc.) to string type? That way you could control the significant digits and add the '%' character to the end of the string.
BDF
|
|
|
|
|
You want to add a % sign as a suffix to the content of a textbox?
yourTextBox.Text += "%";
Cheers,
Vıkram.
Be yourself, no matter what they say.
- Sting, Englishman in New York.
|
|
|
|
|
Thanks Vikram,
textBox name is txtPVSav
In the textbox property definitions, I added the following after the textbox binding code.
this.txtPVSav.Text += "%";
But the textbox content only displays the numerical value.
|
|
|
|