Blog Archives

Getting Installed Browsers and Version C#

I ran into a few posts that solved the problem of getting the installed browsers on a Windows machine and getting the version that were very helpful.

Getting Chrome and Firefox version locally, C#

Finding All Installed Browsers in Windows XP and Vista – beware 64bit!

These snippets were very helpful. So now I will hand it combined to you on a silver platter as a downloadable Console App.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Win32;


namespace GetAllInstalledBrowsers
{
class Program
{
static void Main(string[] args)
{
foreach (Browser browser in GetBrowsers())
{
Console.WriteLine(string.Format("{0}: \n\tPath: {1} \n\tVersion: {2} \n\tIcon: {3}", browser.Name, browser.Path, browser.Version, browser.IconPath));
}
Console.ReadKey();
}


public static List<Browser> GetBrowsers()
{
RegistryKey browserKeys;
//on 64bit the browsers are in a different location
browserKeys = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\WOW6432Node\Clients\StartMenuInternet");
if (browserKeys == null)
browserKeys = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Clients\StartMenuInternet");
string[] browserNames = browserKeys.GetSubKeyNames();
var browsers = new List<Browser>();
for (int i = 0; i < browserNames.Length; i++)
{
Browser browser = new Browser();
RegistryKey browserKey = browserKeys.OpenSubKey(browserNames[i]);
browser.Name = (string)browserKey.GetValue(null);
RegistryKey browserKeyPath = browserKey.OpenSubKey(@"shell\open\command");
browser.Path = (string)browserKeyPath.GetValue(null).ToString().StripQuotes();
RegistryKey browserIconPath = browserKey.OpenSubKey(@"DefaultIcon");
browser.IconPath = (string)browserIconPath.GetValue(null).ToString().StripQuotes();
browsers.Add(browser);
if (browser.Path != null)
browser.Version = FileVersionInfo.GetVersionInfo(browser.Path).FileVersion;
else
browser.Version = "unknown";
}
return browsers;
}
}


internal static class Extensions
{
///
/// if string begins and ends with quotes, they are removed
///
internal static String StripQuotes(this String s)
{
if (s.EndsWith("\"") && s.StartsWith("\""))
{
return s.Substring(1, s.Length - 2);
}
else
{
return s;
}
}
}


class Browser
{
public string Name { get; set; }
public string Path { get; set; }
public string IconPath { get; set; }
public string Version { get; set; }
}
}

If you run this code you will get results that resemble this:

Mozilla Firefox:
Path: C:\Program Files (x86)\Mozilla Firefox\firefox.exe
Version: 31.0
Icon: C:\Program Files (x86)\Mozilla Firefox\firefox.exe,0
Google Chrome:
Path: C:\Program Files (x86)\Google\Chrome\Application\chrome.exe
Version: 37.0.2062.124
Icon: C:\Program Files (x86)\Google\Chrome\Application\chrome.exe,0
Internet Explorer:
Path: C:\Program Files\Internet Explorer\iexplore.exe
Version: 10.00.9200.16521 (win8_gdr_soc_ie.130216-2100)
Icon: C:\Program Files\Internet Explorer\iexplore.exe,-9

Happy coding!

Advertisements

Master Page DataBind() will clear data on your page – remember!

I was working on a problem with a drop down list getting un-initialized and had run across this page:

c# – .Net Webform losing data – Stack Overflow.

This simple post is about a coder who was getting their ddl control reset every time a postback occurred. They had not yet learned about putting the initialization inside an if statement that only runs when the page is first loaded.

I had done this, and had not changed the code on this page for days and yet it stopped working.

Then I realized — I had been fighting with the links on the site.master.cs page that were causing the link to come up blank. I learned that when you have a reference in your head element on the master page like so:

<script src='<%# Helper.ResolveMyUrl(“/Scripts/libs/jquery-1.7.1.min.js”) %>’ type=”text/javascript”></script>

You need this:

Page.DataBind(); // (yes, but not quite…)

in your Page_Load so that the function call will be resolved. I added that line and my links all started working — and my drop down list started clearing on a postback. But there were enough hours between these discoveries, that I hadn’t made the connection.

What I really needed was:

if (! Page.IsPostBack)  // (better, but not yet done…)
Page.DataBind();

since the DataBind affects all of the data bound controls on all of the pages that use it as a master page.

But wait, there’s more!

I thought this had cleared up my issue, but then I found that another control that I initialized lost its value. It turns out that this DataBind on the entire page is not a good idea. Instead you need to be specific about the stuff on the master page. So I did this:

if (! Page.IsPostBack)
{
  Page.Header.DataBind(); // initializes the script references
  LoginView1.DataBind(); // initializes a link inside LoginView1
}

Ok, I think that does it. I feel better about a more qualified / constrained approach too.

With this qualification, I got the initialization on the links I needed, and didn’t interfere with the drop down lists that were initialized once and then needed to remain populated. I didn’t find this anywhere, and so I am hoping that it will save someone the couple hours I spent.

Happy coding!

Email with attachment from byte array

How to Send Email with Attachment using ASP.NET 2.0 and C#

This article tells how to send an email with an attachment. I made a few modifications to make it work because I had to attach a byte array that came from a database. So this is how you do that:


void SendEmail()
{
List<String> recipientList = GetEmailAddressList();

SmtpClient client = new SmtpClient(GetSMTPServer(), GetSMTPPort());

MailAddress from = new MailAddress(ConfigurationManager.AppSettings["EmailContacts.From"]);

 if (recipientList.Count == 0)
 {
 AddError("no email addresses found.");
 return;
 }

 MailMessage message = new MailMessage();
 message.From = from;
 foreach (string to in recipientList)
 {
 message.To.Add(to);
 }

 //create the message
 message.Body = "A new file is attached for your review.";

 message.BodyEncoding = Encoding.UTF8;

 // Get pdf binary data and save the data to a memory stream
 System.IO.MemoryStream ms = new System.IO.MemoryStream(GetPdf());

 // Create the attachment from a stream. Be sure to name the data with a file and
 // media type that is respective of the data
 message.Attachments.Add(new Attachment(ms, "myFile.pdf", "application/pdf"));

 message.Subject = "New File for Review";

 //send it
 client.Send(message);
}

Tony Sneed’s Blog -> Michael C. Kennedy’s Weblog -> Facebook C# SDK

This:

Using Open Source? Get NuGet. | Tony Sneed’s Blog

has a reference to this:

Michael C. Kennedy’s Weblog – 11 Killer Open Source Projects I Found with NuGet

which has a reference to this:

Facebook C# SDK

which looks extermely cool and useful. More as I dig in.

Thanks Tony!