Hi
In a single server Windows domain, does anyone know how to get the servername.
For example, in my test environment, we have a single Windows Small Business Server 2008 called horatio, it is the domain controller for longtest.local.
I have played around with the system.dns (with no positive results) :
Dim myIP As IPHostEntry = Dns.GetHostEntry(My.Computer.Name)
Dim IPAddress As String = myIP.AddressList.GetValue(0).ToString
Dim myFullDomainName = Dns.GetHostEntry(IPAddress).HostName
Dim DomainParts() As String = myFullDomainName.Split("."c)
Dim DomainName As String = DomainParts(1)
However, I cannot seem to grab the servername, I need a function like :
GetDomainServer() that will return horatio (from the example above.
Driving me a bit mad this one.
Regards
Mark Harby
Nottingham. UK
There's an environment variable called LOGONSERVER that you can use:
var domainServerName = Environment.GetEnvironmentVariable("LOGONSERVER");
Ask the server. Like environment variable. The local machine name is available in one environment variable.
Related
I am trying to run a C# program to determine if a directory exists on multiple servers, so I need to run it as %system Variable%, rather than making a drive letter call, since not every server will have the same drive letter. This is what I have:
If My.Computer.FileSystem.DirectoryExists("D:\backup") Then
This code will work, as I define the drive
If My.Computer.FileSystem.DirectoryExists("%BCK_DRV%\backup") Then
This will not, I get my else error when running it. The %BCK_DRV% is defined in the environment variables, and I can navigate to the folder without issue using %BCK_DRV%\backup. Is there a special way to set and define a %drive% in C#?
Environment.GetEnvironmentVariable?
Code sample:
Dim backupDrive As String = Environment.GetEnvironmentVariable("BCK_DRV") & "\backup"
If My.Computer.FileSystem.DirectoryExists(backupDrive) Then
Try Environment.ExpandEnvironmentVariables():
string raw = #"%BCK_DRV%\backup" ;
string expanded = Environment.ExpandEnvironmentVariables( path_raw ) ;
Naturally, it's up to you to ensure that your process inherits the correct environment.
To expand "%BCK_DRV%\backup" to it's real value you need
Environment.ExpandEnvironmentVariables();
Example:
Environment.ExpandEnvironmentVariables("%winDir%\test")
will expand to "C:\Windows\test" (on my system).
I am currently still beginning to understand Active Directory, I am able to add domain groups into local administrator group but I would like to understand how to approach this : I need to create a computer into the right container in my domain, which I understand how to achieve this, but I do not understand how I can be able to achieve the 'Everyone' ACL whenever I create a new computer in the domain by code.
Note :
1. I am not asking for code, I want to understand how to achieve this so any guidance/articles will be greatly appreciated as I just seem to be unable to find any articles related to my case.
2. I am using Visual Basic to achieve this in Visual Studio 2008.
Thanks for any replies in advance, looking forward to learning this. :)
Edit 1 : Found several ways that might achieve this : powershell or group policy
I found an alternative for VB.net
The following Visual Basic example shows how to read a security descriptor on an object.
Imports ActiveDS
Imports System.Collections
Dim ent As New DirectoryEntry("LDAP://CN=ComputerName,OU=container,DC=domain,DC=com")
Dim sd As SecurityDescriptor = CType(ent.Properties("ntSecurityDescriptor").Value,SecurityDescriptor)
Dim acl As AccessControlList = CType(sd.DiscretionaryAcl, AccessControlList)
Dim ace As AccessControlEntry
For Each ace In CType(acl, IEnumerable)
Console.WriteLine("Trustee: {0}", ace.Trustee)
Console.WriteLine("AccessMask: {0}", ace.AccessMask)
Console.WriteLine("Access Type: {0}", ace.AceType)
Next ace
The following Visual Basic example shows you how to write a security descriptor to an object.
Import ActiveDS
Dim usr As New DirectoryEntry("LDAP://CN=ComputerName,OU=container,DC=domain,DC=com")
Dim newAce = New AccessControlEntryClass()
Dim usrSD As SecurityDescriptor = CType(usr.Properties("ntSecurityDescriptor").Value, SecurityDescriptor)
Dim usrAcl As AccessControlList = CType(usrSD.DiscretionaryAcl, AccessControlList)
newAce.Trustee = "Anything"
newAce.AccessMask = - 1
newAce.AceType = 0
usrAcl.AddAce(newAce)
usrSD.DiscretionaryAcl = usrAcl
usr.Properties("ntSecurityDescriptor").Value = usrSD
usr.CommitChanges()
You have to add reference into the solution. Add Reference --> COM --> Active DS Type Library
This is my first post to this precious website. I am a new learner of vb.net. I am working on a simple purchase project, where i got some errors. But the first thing is which baffled me is:
This is my connection string at module level, on the developed machine.
Public strCn As String = "Data Source = (local); Initial Catalog = PSys; Integrated Security = false; User ID = sa; Password = 123;"
Is it mandatory that Data Source must be the original name of the System Name. I mean If i use (local) or using ( . ), so will it work or not? Because when i copy my project to any other system for further development so every time i need to change the Data source, otherwise i get the error that: "Network-related or instance-specific error occurred......."
Kindly guide me that what i need to do.
When you are developing an application which uses a database server such as MsSQL it is not wise to install the server along with your application in every pc which is installed to. For example what are you going to do if a customer has a local network with 10 computers? Are you going to install SQL server in all 10 of them? And if so what if they need to share data?
So your best approach (based on common practice by other applications) will be to allow the user to install the SQL server where he wants and let him configure your application and point it to the server's location. If you follow that path then the configuration of your application can be in the setup application or in the application itself.
Now about the development phase, I had a similar situation in which I needed to develop the same application in two different computers. What I did was to install the SQL server in both of them with a named instance "sqlexpress" then in the application I used the
Data.SqlClient.SqlConnectionStringBuilder
class to build the connection string. I did something like this:
Public Function getDevConnectionString() As String
Dim csb As New Data.SqlClient.SqlConnectionStringBuilder(My.Settings.dbConnectionString) '<-My original cs in app settings
csb.DataSource = My.Computer.Name & "\sqlexpress"
Return csb.ConnectionString
End Function
Whenever I need a connection string I simply call getDevConnectionString() which returns the connection string based on the computer name plus the sql server instance name. For example:
Dim cs As String
#If DEBUG Then
cs = getDevConnectionString()
#Else
cs = getReleaseConnectionString()
#End If
where getReleaseConnectionString() is the function that returns your connection string configured by the customer.
Hope this point you the right direction...
I am trying to interact with a Notes database (my personal mail file), via C# and am stuck right out of the gate, trying to open the database with the following code:
string sNotesSourceFolder = "$Inbox";
string sUserName = "scady";
string sMailFile = #"NotesMail_FDrive\ARchives\" + sUserName + ".nsf";
string sServerName = #"Answers1/Answers";
string sPassword = "xxxxxx";
//Creating the notes session
NotesSession session = new NotesSession();
session.Initialize(sPassword);
NotesDatabase NotesDb = session.GetDatabase(sServerName, sMailFile, false);
//If the database is not already open then open it.
if (!NotesDb.IsOpen)
{
NotesDb.Open();
}
calling session.GetDatabase() with the server name and path the the nsf file returns null which, apparently means it could not open the db.
I am running this code locally, against a Notes server called "Answers1/Answers". I am now thinking based on From the Notes server, the nsf files are located on a network fileshare of "NotesMail_FDrive\ARchives\xyz.nsf".
Is the server on which the file resides a Notes server or just a network share?
If it's a network share, you need the drive mapping, but sServerName would be "" to get to a "local" drive. I'm assuming Notesdata1 is a directory on X, so if it's the drive name, remove it.
string sMailFile = #"X:\Notesdata1\NotesMail\ARchives\scady.nsf";
string sServerName = #"";
If it's a Notes server AND Notesdata1 is the data directory for the Notes server, then
string sMailFile = #"NotesMail\ARchives\scady.nsf";
string sServerName = #"Answers1/Answers";
Sorry I didn't notice this on the first pass...
The value for sMailFile should not be "\10.10.10.71\notesdata1\NotesMail\ARchives\scady.nsf". The other two values that you have commented out are also wrong. The arguments for getDatabase are the Domino server's distinguished name, and a relative path. No server IP, no volume, and no "file:" prefix. Just something like this: "archives\scady.nsf". It should be exactly what you see for the path when you open up the database in your Notes client and bring up the database properties dialog.
Previous Answer
Your sServername variable contains the Domino server's distinguished name, as it should.
But why are you using File.Exists? The Domino server wants exclusive access to all of the files in its data tree. If you are actually successfully accessing the file on the Domino server via a filesystem call, then perhaps you are locking the Domino server out of accessing it.
But also: where are you running this code? Are you running it on the same computer, and under the same account that you normally use to read your email? Or on a different computer and account?
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to get local ip address using vb?
I am using Silverlight3 with VB.NET. I have written the follwing code in my service file. When I run my project locally, it gives the IP address of my system as 192.168.1.12 successfully. But, when I host my project it wont return the IP adress of the system which I am using.
ie: If i host my project in system A which is of ip 192.168.1.12 and if I access the project from system B which is of ip 192.168.1.7, the output of my project will gives the IP of system A (where i have hosted my project). It should give the IP of system B where am running it. Similarly, if am accessing the project from system C means it should give the IP of system C, but here it is returning the IP of system A.
Kindly help me please.
Thanks
Private Function fnLocalIp() As String
Try
Dim h As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName)
Dim strLocalIp As String = h.AddressList.GetValue(0).ToString
Return strLocalIp
Catch ex As Exception
End Try
End Function
You can get local ip using below code:
in vb.net:
Dim host As String = System.Net.Dns.GetHostName()
Dim LocalHostaddress As String = System.Net.Dns.GetHostByName(host).AddressList(1).ToString()
In c#:
string host = System.Net.Dns.GetHostName();
string LocalHostaddress = System.Net.Dns.GetHostByName(host).AddressList[1].ToString();
use dns Class , more information here
http://msdn.microsoft.com/it-it/library/system.net.dns.aspx
Regards.