Open a default browser window using c# and Process.Start - c#

I am trying to open a web page using the default browser when someone hits an API endpoint.
I have this working on my local test machine:
[HttpGet("Http/{classId}")]
public void OpenWebLink(Guid classId)
{
string target = "http://astrolab.meeting.trl.edu/class/details.aspx?classId=" + classId;
System.Diagnostics.Process.Start("C:\\Program Files\\Mozilla Firefox\\firefox.exe", target);
}
But when I publish to a server that has IIS, it can't find firefox.exe
The problem is, I had to put the full path to firefox just to get it to work on my machine. If I didn't include the path like that I'd get this error:
System.Diagnostics.Process.Start Win32Exception: 'The system cannot find the file specified.'
I also tried this:
[HttpGet("Http")]
public void OpenWebLink(Guid classId)
{
try
{
var ps = new ProcessStartInfo("http://astrolab.meeting.trl.edu/class/details.aspx?classId=" + classId;)
{
Verb = "open"
};
Process.Start(ps);
}
catch (Win32Exception w32Ex)
{
throw w32Ex;
}
}
But it still fails when I hit the endpoint on the IIS server with this:
System.ComponentModel.Win32Exception (2): The system cannot find the file specified.
Is there a way to set it up so that it will find the default browser on any machine?
Thanks!

It's as if dotnet developers are unaware of other operating systems besides Windows...
#Blindy's answer only works on Windows, but nowhere did they indicate this. And even if #SkyeBoniwell's question mentions IIS which implies Windows, neither the title of the question, nor the body, nor the tags explicitly mention Windows. As such, keeping in mind stackoverflow answers are meant to be used by everyone and not only the OP of a question, a correct answer to this thread should theoretically be os-agnostic. In practice, it should take into account as many operating systems as possible, i.e. at the very least the main three ones.
Here is a solution that works for 99% of end-user systems:
public static void OpenBrowser(string url)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Process.Start(new ProcessStartInfo(url) { UseShellExecute = true });
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Process.Start("xdg-open", url);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
Process.Start("open", url);
}
else
{
// throw
}
}
taken from here

Either use ShellExecute to launch your url (the more correct way), or pipe it through explorer (the lazier, less portable way).
Process.Start(new()
{
UseShellExecute = true,
FileName = "http://google.ca",
});
Process.Start("explorer.exe", "http://google.ca");

Related

Programmatically check if a windows service is running c#

I am writing an app to check to see if certain software is installed. One of my cases im looking for a service. I know the full path of the service. i.e. "c:\some folder\MyService.exe" I want to check to see if the service is installed and running. I have tried process.GetProcessbyName, but running into issues with 64 bit vs 32 bit processes. I've also tried ManagementObject but i keep getting invalid object path. Is it possible to get a service knowing only the path to the executable?
I know only the name and path of the executable. There may be more than one version of the executable as well, each with a different service name, which i do not have.
Here is how you can check if the service is installed or not , also get the status of the service
public static string CheckService(string ServiceName)
{
//check service
var services = ServiceController.GetServices();
string serviceStatu = string.Empty;
bool isServiceExist = false;
foreach (var s in services)
{
if (s.ServiceName == ServiceName)
{
serviceStatu = "Service installed , current status: " + s.Status;
isServiceExist = true;
}
}
if (!isServiceExist)
{
serviceStatu= "Service is not installed";
}
return serviceStatu;
}
Console.WriteLine(CheckService("Service name"));
you need to add System.ServiceProcess to the project reference
Try looking into the ServiceController / Management object for the executable path. Then based the executable path determine whether the service is running.
How to get executable path : [1] [2] [3]
Borrowed from an answer above
ManagementClass mc = new ManagementClass("Win32_Service");
foreach(ManagementObject mo in mc.GetInstances())
{
if(mo.GetPropertyValue("PathName").ToString().Trim('"') == "<your executable path>")
{
return mo.GetPropertyValue("Name").ToString(); // or return true;
}
}
I haven't tested this, and a comment suggested PathName may return command line arguments as well, so you may need to write another method to separate the path from the arguments (I'm assuming it'll just be a split on the string), and pass PathName to it in If statement..

How can I find the SID for an app pool via Microsoft.Web.Administration?

I am working on a configuration tool that needs to set some permissions on a directory based on the identity that a specific web application runs under. The original code simply built the login name based on IIS APPPOOL\<ApppoolName> or based on the well know SID if it was a built in account.
Some similar code failed in a localized environment so I am now trying to get rid of the baked in string.
My solution was this:
public static SecurityIdentifier GetApplicationPoolSid(string name)
{
ApplicationPool pool = Manager.ApplicationPools[name];
if (pool != null)
{
var sddlForm = pool.GetAttributeValue("applicationPoolSid") as string;
if (!String.IsNullOrEmpty(sddlForm))
return new SecurityIdentifier(sddlForm);
}
return null;
}
The problem is that I found "applicationPoolSid" by poking around in the debugger and I cannot find any documentation that says that I am not taking advantage of an undocumented implementation feature that will go away in the future. (This means it won't pass code review.)
I would love to know the approved way of doing what I am looking at here. I would also be happy to know that IIS APPPOOL\<ApppoolName> is guaranteed to never be localized so then we could go back to the old way.
I can find no reference to IIS APPPOOL being localized, which would make sense since neither IIS nor APPPOOL are words in any language, so there's nothing to translate to. Although they are acronyms in English and possibly other languages, I think they are still called IIS and AppPool even in other languages.
You can, by the way, get the SID officially like this:
NTAccount f = new NTAccount(#"IIS AppPool\DefaultAppPool");
SecurityIdentifier s = (SecurityIdentifier)f.Translate(typeof(SecurityIdentifier));
String sidString = s.ToString();
You don't even really need the SID if you're going to pass it to FileSystemAccessRule(), since it has an overload that takes an IdentityReference, which NTAccount is. Just pass it the NTAccount.
This will get you the SID, but does not use the function you asked about and is written in powershell not C#. I am posting this to hopefully assist in getting on the right track regarding the generation using SHA1 rather than actually querying from Windows. Also some systems do not have some SID and user account modules installed so this can be used in those situations.
Function to generate SID based off apppool username (explanation in link at the bottom)
function Get-SID ([String]$winver, [String]$username) {
$sidPrefix = switch ($winver) {
'6.0' { 'S-1-5-80' }
default { 'S-1-5-82' }
}
$userToString = switch ( $sidPrefix ) {
'S-1-5-82' { $username.ToLower() }
default { $username.ToUpper() }
}
$userBytes = [Text.Encoding]::Unicode.GetBytes($userToString)
$sha = New-Object System.Security.Cryptography.SHA1CryptoServiceProvider
$hash = $sha.ComputeHash($userBytes)
$sid = $sidPrefix
for ( $i=0; $i -lt 5; $i++ ) {
$sid += '-' + [BitConverter]::ToUInt32($hash, $i*4)
}
return $sid
}
Example usage
Get-SID -winver "6.0" -username "DefaultAppPool"
User 6.0 for Vista or equivalent release, use 6.1/6.2 etc for after that.
Partial credit to Tomas Restrepo from winterdom.com for the powershell, although that script does not actually work because some functions are not included in the code.

What is the best practice for ensuring permissions are correct during the launch of an ASP.NET application

I have an ASP.NET application which requires write access on the App_Data subfolder. The MSI used to deploy the application tries to set the permissions correctly, but in spite of this, it seems the permissions are sometimes wrong. Most of the application works fine without this permission. I would prefer that the application fails to start if the permissions are wrong.
What is the best practice for ensuring that the necessary permissions are correct for the IIS user context? Ideally I want to display some simple instructions for fixing whatever is wrong. And I want the message to appear in as many incorrect configurations as possible.
The following describes what I've tried so far, until I realised there's a probably a better or standard way.
I tried putting this in Application_Start()
protected void Application_Start(Object sender, EventArgs e)
{
// Assert permissions on writeable folders are correct
var permissionsChecker = new AppDataPermissionsChecker();
permissionsChecker.AssertFolderIsWriteable(
HttpContext.Current.Server.MapPath("~/App_Data"));
// remainder of Application_Start()...
}
where AppDataPermissionsChecker is defined as follows:
public class AppDataPermissionsChecker
{
private bool CanWriteAccessToFolder(string folderPath)
{
try
{
// Attempt to get a list of security permissions from the folder.
// This will raise an exception if the path is read only or do not have access to view the permissions.
DirectorySecurity directorySecurity = Directory.GetAccessControl(folderPath);
return true;
}
catch (UnauthorizedAccessException)
{
return false;
}
}
public void AssertFolderIsWriteable(string folderPath)
{
if (!Directory.Exists(folderPath))
throw new Exception(String.Format("The {0} folder does not exist.", folderPath));
if (!CanWriteAccessToFolder(folderPath))
throw new Exception(String.Format("The ASPNET user does not have "
+ "access to the {0} folder. Please ensure the ASPNET user has "
+ "read/write/delete access on the folder. See 'The App_Data folder' "
+ "here: http://msdn.microsoft.com/en-us/library/06t2w7da.aspx'",
folderPath));
}
}
I thought this would throw an ugly exception if the rights are incorrect (which is better than nothing), but in some situations I just get an HTTP Error 503.
I found this implementation of a diagnostics page which does exactly what I was looking for (and more besides).

How to launch a Google Chrome Tab with specific URL using C#

Is there a way I can launch a tab (not a new Window) in Google Chrome with a specific URL loaded into it from a custom app? My application is coded in C# (.NET 4 Full).
I'm performing some actions via SOAP from C# and once successfully completed, I want the user to be presented with the end results via the browser.
This whole setup is for our internal network and not for public consumption - hence, I can afford to target a specific browser only. I am targetting Chrome only, for various reasons.
As a simplification to chrfin's response, since Chrome should be on the run path if installed, you could just call:
Process.Start("chrome.exe", "http://www.YourUrl.com");
This seem to work as expected for me, opening a new tab if Chrome is already open.
// open in default browser
Process.Start("http://www.stackoverflow.net");
// open in Internet Explorer
Process.Start("iexplore", #"http://www.stackoverflow.net/");
// open in Firefox
Process.Start("firefox", #"http://www.stackoverflow.net/");
// open in Google Chrome
Process.Start("chrome", #"http://www.stackoverflow.net/");
For .Net core 3.0 I had to use
Process process = new Process();
process.StartInfo.UseShellExecute = true;
process.StartInfo.FileName = "chrome";
process.StartInfo.Arguments = #"http://www.stackoverflow.net/";
process.Start();
UPDATE: Please see Dylan's or d.c's anwer for a little easier (and more stable) solution, which does not rely on Chrome beeing installed in LocalAppData!
Even if I agree with Daniel Hilgarth to open a new tab in chrome you just need to execute chrome.exe with your URL as the argument:
Process.Start(#"%AppData%\..\Local\Google\Chrome\Application\chrome.exe",
"http:\\www.YourUrl.com");
If the user doesn't have Chrome, it will throw an exception like this:
//chrome.exe http://xxx.xxx.xxx --incognito
//chrome.exe http://xxx.xxx.xxx -incognito
//chrome.exe --incognito http://xxx.xxx.xxx
//chrome.exe -incognito http://xxx.xxx.xxx
private static void Chrome(string link)
{
string url = "";
if (!string.IsNullOrEmpty(link)) //if empty just run the browser
{
if (link.Contains('.')) //check if it's an url or a google search
{
url = link;
}
else
{
url = "https://www.google.com/search?q=" + link.Replace(" ", "+");
}
}
try
{
Process.Start("chrome.exe", url + " --incognito");
}
catch (System.ComponentModel.Win32Exception e)
{
MessageBox.Show("Unable to find Google Chrome...",
"chrome.exe not found!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

Use C# to interact with Windows Update

Is there any API for writing a C# program that could interface with Windows update, and use it to selectively install certain updates?
I'm thinking somewhere along the lines of storing a list in a central repository of approved updates. Then the client side applications (which would have to be installed once) would interface with Windows Update to determine what updates are available, then install the ones that are on the approved list. That way the updates are still applied automatically from a client-side perspective, but I can select which updates are being applied.
This is not my role in the company by the way, I was really just wondering if there is an API for windows update and how to use it.
Add a Reference to WUApiLib to your C# project.
using WUApiLib;
protected override void OnLoad(EventArgs e){
base.OnLoad(e);
UpdateSession uSession = new UpdateSession();
IUpdateSearcher uSearcher = uSession.CreateUpdateSearcher();
uSearcher.Online = false;
try {
ISearchResult sResult = uSearcher.Search("IsInstalled=1 And IsHidden=0");
textBox1.Text = "Found " + sResult.Updates.Count + " updates" + Environment.NewLine;
foreach (IUpdate update in sResult.Updates) {
textBox1.AppendText(update.Title + Environment.NewLine);
}
}
catch (Exception ex) {
Console.WriteLine("Something went wrong: " + ex.Message);
}
}
Given you have a form with a TextBox this will give you a list of the currently installed updates. See http://msdn.microsoft.com/en-us/library/aa387102(VS.85).aspx for more documentation.
This will, however, not allow you to find KB hotfixes which are not distributed via Windows Update.
The easiest way to do what you want is using WSUS. It's free and basically lets you setup your own local windows update server where you decide which updates are "approved" for your computers. Neither the WSUS server nor the clients need to be in a domain, though it makes it easier to configure the clients if they are. If you have different sets of machines that need different sets of updates approved, that's also supported.
Not only does this accomplish your stated goal, it saves your overall network bandwidth as well by only downloading the updates once from the WSUS server.
If in your context you're allowed to use Windows Server Update Service (WSUS), it will give you access to the Microsoft.UpdateServices.Administration Namespace.
From there, you should be able to do some nice things :)
P-L right. I tried first the Christoph Grimmer-Die method, and in some case, it was not working. I guess it was due to different version of .net or OS architecture (32 or 64 bits).
Then, to be sure that my program get always the Windows Update waiting list of each of my computer domain, I did the following :
Install a serveur with WSUS (may save some internet bandwith) : http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=5216
Add all your workstations & servers to your WSUS server
Get SimpleImpersonation Lib to run this program with different admin right (optional)
Install only the administration console component on your dev workstation and run the following program :
It will print in the console all Windows updates with UpdateInstallationStates.Downloaded
using System;
using Microsoft.UpdateServices.Administration;
using SimpleImpersonation;
namespace MAJSRS_CalendarChecker
{
class WSUS
{
public WSUS()
{
// I use impersonation to use other logon than mine. Remove the following "using" if not needed
using (Impersonation.LogonUser("mydomain.local", "admin_account_wsus", "Password", LogonType.Batch))
{
ComputerTargetScope scope = new ComputerTargetScope();
IUpdateServer server = AdminProxy.GetUpdateServer("wsus_server.mydomain.local", false, 80);
ComputerTargetCollection targets = server.GetComputerTargets(scope);
// Search
targets = server.SearchComputerTargets("any_server_name_or_ip");
// To get only on server FindTarget method
IComputerTarget target = FindTarget(targets, "any_server_name_or_ip");
Console.WriteLine(target.FullDomainName);
IUpdateSummary summary = target.GetUpdateInstallationSummary();
UpdateScope _updateScope = new UpdateScope();
// See in UpdateInstallationStates all other properties criteria
_updateScope.IncludedInstallationStates = UpdateInstallationStates.Downloaded;
UpdateInstallationInfoCollection updatesInfo = target.GetUpdateInstallationInfoPerUpdate(_updateScope);
int updateCount = updatesInfo.Count;
foreach (IUpdateInstallationInfo updateInfo in updatesInfo)
{
Console.WriteLine(updateInfo.GetUpdate().Title);
}
}
}
public IComputerTarget FindTarget(ComputerTargetCollection coll, string computername)
{
foreach (IComputerTarget target in coll)
{
if (target.FullDomainName.Contains(computername.ToLower()))
return target;
}
return null;
}
}
}

Categories

Resources