• 0

IsRunAsAdmin() (vb programing)


Question

Hi i just installed vb 2008 express edition and i am making a program to change the logon background for personal use.

anyway everything is working fine....

but there is one more thing...

my friends want to use the program but it is not perfectly coded and i dont want to damage there computers

so i need to check two things before the program starts

1-IsRunUnderWindows7() to see if the program is runing under windows 7.

2-IsRunAsAdmin() to see if the program is run with admin privilege.

If anyone know how to do any of these to checks it would be grate

Thanks

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Hi,

something like... (in c#)

		private void button1_Click(object sender, EventArgs e)
		{
			OperatingSystem os = Environment.OSVersion;

			if (os.Version.Major == 6 && os.Version.Minor == 1)
			{
				// Windows7
			}
		}

interestingly, if minor version is 0, then you are on Vista, windows 7 isn't really v7 :D

for the other part.. this should work to test if a local user has a role of admin..

		private void button2_Click(object sender, EventArgs e)
		{
			System.Security.Principal.WindowsIdentity u = System.Security.Principal.WindowsIdentity.GetCurrent();
			System.Security.Principal.WindowsPrincipal p = new System.Security.Principal.WindowsPrincipal(u);
			MessageBox.Show("local admin status " + p.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator).ToString());
		}

i don't think this will work if you are logged on as a domain user, that is in the local admin group... i'll have a think about that one.

Edited by BGM
Link to comment
Share on other sites

This topic is now closed to further replies.