How do i get Location String as path using c#? - c#

enter image description here
https://i.stack.imgur.com/6KXds.png
I want to open Location String in Explorer.exe
try
{
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(#"SOFTWARE\Wilcom\Wilcom EmbroideryStudio\4.2H"))
{
if (key != null)
{
Object o = key.GetValue("Location");
if (o != null)
{
Version version = new Version(o as String); //"as" because it's REG_SZ...otherwise ToString() might be safe(r)
Process.Start(key.GetValue("Location").ToString());
}
}
}
}
catch (Exception ex) //just for demonstration...it's always best to handle specific exceptions
{
//react appropriately
}
It's not working. Please Help.

Process.Start("explorer.exe", "/select," + o.ToString());
This opens File Explorer with the location set as indicated using command-line arguments.
See Explorer.exe - Windows CMD - SS64.com

Related

Hide MS Office add-in from programs and feature after installation using c#

I don't want to show my powerpoint addin from programs and feature in control panel after installation.
For this I tried to add systemcomponent property in registry HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall after adding this manually Add-in was successfully hide from programs and features but when I tried to do this from my code, getting an exception of type ThrowUnauthorizedAccessException (exception error message "Cannot write to the registry key") althought I am running that .exe on Admin mode. I am not figuring it out how to add this property in "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" directory using C# code.
If this is not the right solution to do this so what else I can do?
Here is my code
RegistryKey registry = Registry.CurrentUser.OpenSubKey(#"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall", true);
foreach (var subkey in registry.GetSubKeyNames())
{
RegistryKey productKey = registry.OpenSubKey(subkey);
if (productKey != null)
{
foreach (var value in productKey.GetValueNames())
{
string keyValue = Convert.ToString(productKey.GetValue(value));
if (keyValue.ToLower().Contains("myaddin"))
{
try
{
productKey.SetValue("SystemComponent", 1, RegistryValueKind.DWord);
productKey.Close();
return;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
}

Programmatically open a page in IIS/IIS Express in the default browser on click of a button

I want to open a html page (in AppData/Roaming folder and not in the project/solution) with IIS/IIS Express in the default browser on click of a button. I have been able to find the default browser using the following:-
//This method is used to get the default browser
private static string GetDefaultBrowserPath()
{
string urlAssociation = #"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http";
string browserPathKey = #"$BROWSER$\shell\open\command";
RegistryKey userChoiceKey = null;
string absBrowserPath = "";
const string exeSuffix = ".exe";
FileInfo browserPath;
try
{
//reading default browser path from userChoiceLKey
userChoiceKey = Registry.CurrentUser.OpenSubKey(urlAssociation + #"\UserChoice", false);
//If user choice was not found, try machine default
if (userChoiceKey == null)
{
//reading default browser path from Win XP registry key
var browserKey = Registry.ClassesRoot.OpenSubKey(#"HTTP\shell\open\command", false);
//if browser path not found, try Win Vista (and newer) registry key
if (browserKey == null)
{
browserKey =
Registry.CurrentUser.OpenSubKey(
urlAssociation, false);
}
var path = browserKey.GetValue(null);
browserKey.Close();
return path.ToString();
}
else
{
//if user defined browser choice was found
string progId = (userChoiceKey.GetValue("ProgId").ToString());
userChoiceKey.Close();
//looking up the path of the executable
string concreteBrowserKey = browserPathKey.Replace("$BROWSER$", progId);
var kp = Registry.ClassesRoot.OpenSubKey(concreteBrowserKey, false);
try
{
absBrowserPath = kp.GetValue(null).ToString().ToLower().Replace("\"", "");
if (!absBrowserPath.EndsWith(exeSuffix))
{
absBrowserPath = absBrowserPath.Substring(0, absBrowserPath.LastIndexOf(exeSuffix, StringComparison.Ordinal) + exeSuffix.Length);
browserPath = new FileInfo(absBrowserPath);
}
}
catch (Exception)
{
throw;
}
kp.Close();
return absBrowserPath;
}
}
catch(Exception ex)
{
return "";
}
}
After locating the default browser, I want to open a html page located in AppData/Roaming folder with IIS. How can this be achieved?
Edit: I am thinking of first publishing the pages with the IIS using ServerManager class of Microsoft.Web.Administration and then trying to open it.
is this a right approach? Once published, how would it be possible to open that page(a locally published site now) on click of some button?

How to launch the user's default browser with search criteria?

I found this link:
C# Launch default browser with a default search query
But with FireFox as my default browser, it tries to find a file named as whatever is in the quotes in the selected Answer there.
Code;
//ToolStripMenu Click event to launch default browser and search internet for the value in a particular cell
private void tsmSearch_Click(object sender, EventArgs e)
{
int key = mp.GetRowAt(gdcErrorLogDefaultView, rowX, rowY);
if (key < 0)
return;
string ex = gdcErrorLogDefaultView.GetRowCellValue(key, "Exception").ToString();
string name = GetDefaultBrowser();
Process.Start(name, "\"?" + ex + "\"");
}
//Gets default browser from registry
private string GetDefaultBrowser()
{
string name;
RegistryKey regKey = null;
try
{
//set the registry key we want to open
regKey = Registry.ClassesRoot.OpenSubKey("HTTP\\shell\\open\\command", false);
//get rid of the enclosing quotes
name = regKey.GetValue(null).ToString().ToLower().Replace("" + (char)34, "");
//check to see if the value ends with .exe (this way we can remove any command line arguments)
if (!name.EndsWith("exe"))
//get rid of all command line arguments (anything after the .exe must go)
name = name.Substring(0, name.LastIndexOf(".exe") + 4);
}
catch (Exception ex)
{
name = string.Format("ERROR: An exception of type: {0} occurred in method: {1} in the following module: {2}", ex.GetType(), ex.TargetSite, this.GetType());
}
finally
{
//check and see if the key is still open, if so
//then close it
if (regKey != null)
regKey.Close();
}
return name;
}
I found the GetDefaultBrowser() code somewhere on StackOverflow yesterday but I can't find the link now. The weird thing is though, I have Chrome set as my default browser but that registry key still says FireFox.
Is there a more simple way to launch the default browser and use their default search provider to look up a term?
Try this:
string searchQuery = "this is a search";
Process.Start("https://www.google.com/search?q=" + Uri.EscapeDataString(searchQuery));
Edit: Now using correct Google link

How to read value of a registry key c#

At start up of my application I am trying to see if the user has a specific version of a software installed, specifically the MySQL connector, all using c#. In the registry, the MySQL contains a version entry. So what I am trying to accomplish is this.
My app starts up. Somewhere in the start up code I need to do the following things in order. Check to see if the user has the MySQL connector installed, which is located at...
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\MySQL AB\MySQL Connector/Net
If the user has the connector installed, I wanted to check what version they have, which is stored as Name = "Version" and Data = x.x.x (Picture below)
Now if the user has a specific version installed, then I will execute other code, which is where I can take from.
What would be the best way of going about this?
EDIT: Below is the code I currently have and I am getting an error on line 19 (It is commented). My error says "error CS1001: Identifier Expected" I wasnt able to figure out what that means. Any help?
using System;
using Microsoft.Win32;
using System.Data;
public class regTest
{
public static void Main()
{
try
{
RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Wow6432Node\\MySQL AB\\MySQL Connector\\Net");
if (key != null)
{
Object o = key.GetValue("Version");
if (o != null)
{
Version version = new Version(o as String); //"as" because it's REG_SZ...otherwise ToString() might be safe(r)
Version broken = new Version("6.7.4");
if (version.Equals.(broken)) //This is where the error is occuring
{
DataSet dataSet = ConfigurationManager.GetSection("system.data") as ystem.Data.DataSet;
DataView vi = dataSet.Tables[0].DefaultView;
vi.Sort = "Name";
if (vi.Find("MySql") == -1)
{
dataSet.Tables[0].Rows.Add("MySql"
, "MySql.Data.MySqlClient"
, "MySql.Data.MySqlClient"
,
typeof(MySql.Data.MySqlClient.MySqlClientFactory).AssemblyQualifiedName);
}
}
}
}
}
catch (Exception ex) //just for demonstration...it's always best to handle specific exceptions
{
//react appropriately
}
}
}
You need to first add using Microsoft.Win32; to your code page.
Then you can begin to use the Registry classes:
try
{
using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Wow6432Node\\MySQL AB\\MySQL Connector\\Net"))
{
if (key != null)
{
Object o = key.GetValue("Version");
if (o != null)
{
Version version = new Version(o as String); //"as" because it's REG_SZ...otherwise ToString() might be safe(r)
//do what you like with version
}
}
}
}
catch (Exception ex) //just for demonstration...it's always best to handle specific exceptions
{
//react appropriately
}
BEWARE: unless you have administrator access, you are unlikely to be able to do much in LOCAL_MACHINE. Sometimes even reading values can be a suspect operation without admin rights.
#DonBoitnott have a good code, but require admin rights. I use this (only need Read Rights)
try
{
var subKey = "Software\\Wow6432Node\\MySQL AB\\MySQL Connector\\Net";
using (var key = Registry.LocalMachine.OpenSubKey(subKey, false)) // False is important!
{
var s = key?.GetValue("Version") as string;
if (!string.IsNullOrWhiteSpace(s))
{
var version = new Version(s);
}
}
}
catch (Exception ex) //just for demonstration...it's always best to handle specific exceptions
{
//react appropriately
}
Change:
using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Wow6432Node\\MySQL AB\\MySQL Connector\\Net"))
To:
using (RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\Wow6432Node\MySQL AB\MySQL Connector\Net"))

What is the common way to throw exception if my exe file that i need to start does not exist on the machine

in my application i am start capinfos.exe that is part of Wireshark.
in the constructor i am check if Wireshark install on the machine:
private string _filePath = "";
public Capinfos(string capturePath)
{
if (Directory.Exists(#"C:\Program Files (x86)\Wireshark"))
{
_capInfos = #"C:\Program Files (x86)\Wireshark\capinfos.exe";
}
else if (Directory.Exists(#"C:\Program Files\Wireshark"))
{
_capInfos = #"C:\Program Files\Wireshark\capinfos.exe";
}
_filePath = capturePath;
}
what is the best way to do it and throw an exception if the file does not exist on the machine: please install Wireshark
private string _filePath = "";
public Capinfos(string capturePath) throws FileNotFoundException
{
if (Directory.Exists(#"C:\Program Files (x86)\Wireshark"))
{
_capInfos = #"C:\Program Files (x86)\Wireshark\capinfos.exe";
}
else if (Directory.Exists(#"C:\Program Files\Wireshark"))
{
_capInfos = #"C:\Program Files\Wireshark\capinfos.exe";
} else
{
throw new FileNotFoundException(#"Wireshark installation not found");
}
_filePath = capturePath;
}
You can then catch the exception by using this code:
try
{
Capinfos("path");
}
catch (FileNotFoundException ex)
{
Messagebox.Show("Please install wireshark.");
}
I don't have C# installed, this was written by hand. Hope it's fine!
Here's an excellent resource to learn on exceptions: http://msdn.microsoft.com/en-us/library/0yd65esw(v=vs.80).aspx
Something like:
throw new FileNotFoundException("Could not find " + _capInfos, _capInfos);
Not sure if this is what you want, but try to use a try-catch block. You could attempt to start the .exe in the try block and if it fails, throw a FileNotFoundException and create a popup box in the catch block that will alert the user of what they need to do.

Categories

Resources