Alternative way to “open command prompt here”–windows hint

I was shown this at an MSDN event and use it regularly since. You can open a CMD prompt at the current location from windows explorer, simply by typing in the folder address bar, then hit return.

Navigate to location

Windows explorer showing folder contents

Type “cmd” into the address bar as shown below

explorer folder address bar has cmd typed into it

The cmd window will open up at the same folder location

cmd window opened at location of folder

This is equivalent to the more familiar [Shift]+Right Clicking in the explorer window and Open command window here

cmd4

Running SumatraPDF from windows service

I can use Sumatra to print PDF from a windows service but the printer must be installed for the user the service is running as. If the user running the service does not have access to the printer, nothing will print.
 
To test you may log into the machine and go browse to the printer on the network, double click it to install the printer, now start the Sumatra service again, it will now print.
 
Dim printer_name As String = $"""{oPick.GetPickPDFPrinterName()}"""
Dim startInfo As System.Diagnostics.ProcessStartInfo = New ProcessStartInfo()
startInfo.FileName = IO.Path.Combine(My.Application.Info.DirectoryPath, "SumatraPDF.exe")
startInfo.Arguments = $"-print-to {printer_name} {FilenameToPrint} -exit-on-print -silent"

startInfo.RedirectStandardError = True
startInfo.RedirectStandardOutput = True
startInfo.UseShellExecute = False

startInfo.CreateNoWindow = True
startInfo.WindowStyle = ProcessWindowStyle.Hidden
Using proc As System.Diagnostics.Process = Process.Start(startInfo)
Dim soutput As String = proc.StandardOutput.ReadToEnd()
Dim eoutput As String = proc.StandardError.ReadToEnd()

Dim ReturnCode As Integer = 0
If proc.WaitForExit(15000) Then
If proc.ExitCode = 0 Then
oPick.MarkPickPDFAsPrinted(PickListVersion)
Else
Throw New Exception($"SumatraPDF.exe returned a non-zero return code,…
End If
Else
Debug.Write($"Sending pick PDF to printer possibly failed,SumatraPDF.exe timed out..
End If
End Using

Prevent “Service has been marked for deletion” while installing windows services

A reminder, if the above message is encountered when installing a windows service after removing a previous version, then

  • Close all instances of Event Log viewer
  • Close all instances of Services MMC (Microsoft Management Console) for all users on server
  • Use task manager to ensure the old service process is not running

 

  • Sign other users out of server if above fails

Since understanding the above the message no longer haunts me.

 

Stackoverflow post: How to solve “The specified service has been marked for deletion” error

Exception.ToString() and capturing inner exceptions

It is often worth going back to basics, it turns out quite a few people don’t realise that calling “.ToString” on an exception object in .NET, will provide a nicely formatted output suitable for logging. It will also recursively extract the inner exceptions, if there are any too.  It includes the stack trace. I’ve seen quite a few times now code that replicates the same functionality as this built in function. Here is the description from MSDN.

The default implementation of ToString obtains the name of the class that threw the
current exception, the message, the result of calling ToString on the inner exception,
and the result of calling Environment.StackTrace.
If any of these members is null, its value is not included in the returned string.

[MSDNException.ToString Method ()] 

This is quite helpful as it means no traversing the exception objects extracting the inner exceptions, if they exist as its already implemented in this method.