How can i get the PC Name of a client Machine [duplicate] - c#

This question already has answers here:
How to get client's computer name
(3 answers)
Closed 7 months ago.
The community reviewed whether to reopen this question 7 months ago and left it closed:
Original close reason(s) were not resolved
There is any way to get the PC Name of a client Machine in web application, who is working in different Network, in c# asp.net ?
"This is usually a temporary error during hostname resolution and means that the local server did not receive a response from an authoritative server. showing this error.
By doing the answer given in this
How to get client's computer name ,

I think this will help you:
string clienIp = Request.UserHostName;
string computenMame = CompName(clientIp);
public static string CompName(string clienIp)
{
IPAddress myIP = IPAddress.Parse(clienIp);
IPHostEntry GetIPHost = Dns.GetHostEntry(myIP);
List<string> compName =
GetIPHost.HostName.ToString().Split('.').ToList();
return compName.First();
}

Related

How to create a file relative to Program.cs [duplicate]

This question already has answers here:
Get the application's path
(21 answers)
Closed 3 months ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
I want to create a file relative to Program.cs. This code in case of VSCode works correct:
string myFile1 = #".\temp1.txt";
File.Create(myFile1);
string myFile2 = "./temp2.txt";
File.Create(myFile2);
but Visual Studio IDE 2022 creates file in:
`MyProject\bin\Debug\net6.0`
Is there any universal solution?
You can include the following method anywhere within your project:
using System.Runtime.CompilerServices
public static string GetSourceFilePathName( [CallerFilePath] string? callerFilePath = null )
=> callerFilePath ?? "";
Then, you can invoke that method from your Program.cs, and it will give you C:\Users\YOU\Documents\Projects\MyProject\Program.cs.
I suppose you know what to do from there.

Simplest Method of Encrypting a String in .NET without MachineKey [duplicate]

This question already has answers here:
Encrypt and decrypt a string in C#? [closed]
(29 answers)
Simple insecure two-way data "obfuscation"?
(17 answers)
Closed 7 years ago.
I am currently encrypting strings as follows:
public static string Encrypt(this string DecryptedValue)
{
if (string.IsNullOrWhiteSpace(DecryptedValue)) { return string.Empty; }
return HttpServerUtility.UrlTokenEncode(MachineKey.Protect(Encoding.UTF8.GetBytes(DecryptedValue.Trim())));
}
public static string Decrypt(this string EncryptedValue)
{
if (string.IsNullOrWhiteSpace(EncryptedValue)) { return string.Empty; }
return Encoding.UTF8.GetString(MachineKey.Unprotect(HttpServerUtility.UrlTokenDecode(EncryptedValue)));
}
However, when I move the production database to a development server (to test a new version before I deploy), these functions are unable to decrypt data already stored in the database (I assume that this is because of my usage of the MachineKey).
Is there some way that I could modify the above extensions so that I could encrypt/decrypt the same database on multiple machines without writing something complicated?
Assuming you are talking about asp.net, you can try keeping your code and change configuration in web.config instead.
Specify same machine key and algorithms as in production server.
See https://msdn.microsoft.com/en-us/library/w8h3skw9(v=vs.100).aspx for reference.
If the key is specified in web.config file, it should override the real machine key in your development enviroment just for your application.

how to check internet connection in c# [duplicate]

This question already has answers here:
Check internet connection in Silverlight
(3 answers)
Closed 7 years ago.
I created silverlight project.That is dashboard information project.
I created two types of dashboard, One is Dashboard.Xaml another one is Dashboard.Html.
Now requirement is If internet connection available means we show Dashboard.Html other wise if
internet connection is not available means we show Dashboard.Xaml
How to check Internet connection is connected or not connected in c#?
How to trigger automatically internet is Connected means-Dashboard.Html or not-connected means-Dashboard.Xaml?
Please Help Me...
Using
bool IsNetIn= NetworkInterface.GetIsNetworkAvailable();
You can ping valid address on the internet.
public bool PingHost(string nameOrAddress)
{
PingReply reply;
using (var pinger = new Ping())
{
reply = pinger.Send(nameOrAddress);
}
bool pingable=false;
if (reply != null) pingable = reply.Status == IPStatus.Success;
return pingable;
}

Updating password of user in active Directory using C# [duplicate]

This question already has answers here:
C#: code error while changing the active directory user's password
(3 answers)
Closed 8 years ago.
I am trying to change the password of a particular user
you can find code below
var directoryEntryObject = new DirectoryEntry("LDAP://<IP>","administraor", "password");
baseObject.directoryEntryObject.Invoke("SetPassword", new object[] { "test#123" });
baseObject.directoryEntryObject.Properties["LockOutTime"].Value = 0;
baseObject.directoryEntryObject.Close();
Now i am getting below Error ::
Unknown name. (Exception from HRESULT: 0x80020006
(DISP_E_UNKNOWNNAME))
administraor -> is this the real username you are trying? Check the spelling of the username, it is administrator.
If you just typed here with this mistake, try
var directoryEntryObject =
new DirectoryEntry("LDAP://<IP>/Dc=domain,DC=local","administrtaor", "password");

How to find SQL Server instance name and server name from the code behind using c#? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
SqlDataSourceEnumerator.Instance.GetDataSources() does not locate local SQL server 2008 instance
I am using code below to get all instance names and server names in my local machine, but it seems it is returning only one instance; how to get all list of instance name and server name in my local machine?
string myServer = Environment.MachineName;
DataTable servers = SqlDataSourceEnumerator.Instance.GetDataSources();
for (int i = 0; i < servers.Rows.Count; i++)
{
if (myServer == servers.Rows[i]["ServerName"].ToString())
{
if ((servers.Rows[i]["InstanceName"] as string) != null)
{
CmbServerName.Visibility = Visibility.Visible;
CmbServerName.Items.Add(servers.Rows[i]["ServerName"] + "\\" + servers.Rows[i]["InstanceName"]);
}
else
{
CmbServerName.Visibility = Visibility.Visible;
CmbServerName.Items.Add(servers.Rows[i]["ServerName"]);
}
}
}
Thanks in advance!
Another way to get the installed instance names of sql server is to read your machine registry. It will fetch all the instances of sql from your local machine.
RegistryKey rk = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\Microsoft\Microsoft SQL Server");
String[] instances = (String[])rk.GetValue("InstalledInstances");
if (instances.Length > 0)
{
foreach (String element in instances)
{
if (element == "MSSQLSERVER")
Console.WriteLine(System.Environment.MachineName);
else
Console.WriteLine(System.Environment.MachineName + #"\" + element);
}
Console.ReadLine();
}
This might not be it, but worth a try. Is the SQL Server Browser service running on your machine?
GetDataSource() retrieves all the instance visible from your machine. The ServerName is the name of the machine where the istance is hosted, and it appears you have only one istance on your machine.
What do you mean with the bolded part: "How to get all list of instance name and server name in my local machine" ? If you want to see every server visible from your machine you have to remove the if statement
if (myServer == servers.Rows[i]["ServerName"].ToString())
because there is only one server with your local machine name so you can't get any other server name.
Note that documentation reports
Due to the nature of the mechanism used by SqlDataSourceEnumerator to
locate data sources on a network, the method will not always return a
complete list of the available servers, and the list might not be the
same on every call. If you plan to use this function to let users
select a server from a list, make sure that you always also supply an
option to type in a name that is not in the list, in case the server
enumeration does not return all the available servers. In addition,
this method may take a significant amount of time to execute, so be
careful about calling it when performance is critical.
so it could be due to this problem.
A alternative solution:
Microsoft.SqlServer.Management.Smo.SmoApplication.EnumAvailableSqlServers

Categories

Resources