using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Principal;
using System.Windows.Forms;
using WmiExplorer.Properties;
namespace WmiExplorer.Classes
{
public static class Utilities
{
///
/// Checks if Application is running as Administrator
///
/// True if running as Administrator.
public static bool CheckIfElevated()
{
try
{
WindowsIdentity userIdentity = WindowsIdentity.GetCurrent();
WindowsPrincipal userPrincipal = new WindowsPrincipal(userIdentity);
if (userPrincipal.IsInRole(WindowsBuiltInRole.Administrator))
return true;
return false;
}
catch (Exception ex)
{
MessageBox.Show("Failed to determine if Application is running as Administrator: " + ex.Message);
//Log("Unable to determine whether Application is running Elevated. Error: " + ex.Message);
return false;
}
}
///
/// To search and highlight text in Rich Text Box
/// http://www.dotnetcurry.com/showarticle.aspx?ID=146
///
/// Text to Search in RichTextBox rtb
/// Start index for search
/// Index of Last Search result
/// RichTextBox to search in
///
public static int FindTextInRichTextBox(string txtToSearch, int searchStart, int indexOfSearchText, RichTextBox rtb)
{
// Set the return value to -1 by default.
int retVal = -1;
int searchEnd = rtb.Text.Length;
// A valid starting index should be specified.
// if _indexOfSearchText = -1, the end of search
if (searchStart >= 0 && indexOfSearchText >= 0)
{
// A valid ending index
if (searchEnd > searchStart || searchEnd == -1)
{
// Find the position of search string in RichTextBox
indexOfSearchText = rtb.Find(txtToSearch, searchStart, searchEnd, RichTextBoxFinds.None);
// Determine whether the text was found in rtb.
if (indexOfSearchText != -1)
{
// Return the index to the specified search text.
retVal = indexOfSearchText;
}
}
}
return retVal;
}
///
/// Returns all Application settings
///
/// String value containing name & value of all settings
public static string GetSettings()
{
string settings = String.Empty;
foreach (SettingsProperty p in from SettingsProperty p in Settings.Default.Properties
orderby p.Name
select p)
{
// Exclude UpdateCheckUrl and WindowPlacement settings
if (p.Name.StartsWith("UpdateCheckUrl", StringComparison.InvariantCultureIgnoreCase) || p.Name.Equals("WindowPlacement", StringComparison.InvariantCultureIgnoreCase))
continue;
// Settings containing a string array
if (Settings.Default[p.Name] is StringCollection)
{
settings += p.Name + " = ";
foreach (var s in Settings.Default[p.Name] as StringCollection)
settings += s + ", ";
settings += "\r\n";
continue;
}
// Settings without default values
if (p.DefaultValue == null)
{
settings += p.Name + " = " + Settings.Default[p.Name] + "\r\n";
continue;
}
// Other settings with indicator whether value is the default value
if (p.DefaultValue.ToString() == Settings.Default[p.Name].ToString())
settings += p.Name + " = " + Settings.Default[p.Name] + " (Default)\r\n";
else
settings += p.Name + " = " + Settings.Default[p.Name] + "\r\n";
}
return settings;
}
///
/// Launch the specified program
///
/// Command to run
public static void LaunchProgram(string sCmd)
{
try
{
Process.Start(sCmd);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error launching program", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
///
/// Launch the specified program with arguments
///
/// Command to run
/// Argument for the command
/// Wait for the command to Exit
public static void LaunchProgram(string sCmd, string sArgument, bool bWaitForExit)
{
try
{
if (bWaitForExit)
Process.Start(sCmd, sArgument).WaitForExit();
else
Process.Start(sCmd, sArgument);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error launching program", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
///
/// Returns a string from the source SecureString
/// http://blogs.msdn.com/b/fpintos/archive/2009/06/12/how-to-properly-convert-securestring-to-string.aspx
///
/// SecureString to convert to String
/// String
public static string SecureStringToString(this SecureString secureString)
{
if (secureString == null)
throw new ArgumentNullException("secureString");
IntPtr unmanagedString = IntPtr.Zero;
try
{
unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(secureString);
return Marshal.PtrToStringUni(unmanagedString);
}
finally
{
Marshal.ZeroFreeGlobalAllocUnicode(unmanagedString);
}
}
///
/// Returns a Secure string from the source string
/// http://msdn.microsoft.com/en-us/library/system.security.securestring(v=vs.110).aspx
///
/// String to convert to SecureString
/// SecureString
public static SecureString StringToSecureString(this string source)
{
if (String.IsNullOrWhiteSpace(source))
return null;
SecureString result = new SecureString();
foreach (char c in source.ToCharArray())
result.AppendChar(c);
return result;
}
///
/// Update User Settings on Application version update
/// http://www.ngpixel.com/2011/05/05/c-keep-user-settings-between-versions/
///
public static void UpdateSettings()
{
try
{
Settings.Default.Upgrade();
Settings.Default.bUpgradeSettings = false;
Settings.Default.bUpdateAvailable = false;
Settings.Default.Save();
}
catch (Exception ex)
{
MessageBox.Show("Failed to upgrade user settings. Error: " + ex.Message);
}
}
}
}