private string GetProcessPath(Process process) { using (var searcher = new ManagementObjectSearcher("SELECT ProcessId, ExecutablePath FROM Win32_Process WHERE ProcessId = " + process.Id.ToString())) { using (var results = searcher.Get()) { var mo = results.Cast<ManagementObject>().FirstOrDefault(); if (mo != null) return (string) mo["ExecutablePath"]; } } return null; }
Get the color under cursor in hex
The following code gets the pixel color at position. HexConvert converts the color to web hex format.
Continue reading
Get the associated file icon as ico and png
How to get all topmost windows and the current one
The following code enumerates all topmost windows and marks the current one with “>”
How to get the current IE version?
There are basically two ways to get it. Using registry which requires elevation (administrator rights), or this simple one liner, which I recommend:
var ieVersion = new WebBrowser().Version.ToString();
And the registry way is to read the value of:
HKLM\SOFTWARE\Microsoft\Internet Explorer\Version
How to use HTML5 audio in the WebBrowser control?
No matter how new version of Windows you use, the WebBrowser control always uses older version of IE. That’s pretty awful solution since we all know how good IE is. Using meta tags it is possible to say to IE to use a specific version. The following example illustrates how to make a simple html5 audio player playing mp3 directly from some url.
// the obvious.. // webBrowser1 is a WebBrowser control // someUrl is an url to a mp3 file var source = "<html><head><meta http-equiv='X-UA-Compatible' content='IE=9'></head><body><audio id='player' style='position: absolute; left: 0; top: 0; width: 100%' controls autoplay='autoplay'><source src='{0}' type='audio/mpeg'>You need a newer version of IE to play this track.</audio></body></html>"; webBrowser1.DocumentText = string.Format(source, someUrl);
Get the currently focused control
It’s easy to get the currently foreground window (using the GetForegroundWindow API), but what about the currently focused child window?
Here’s one way.
[DllImport("user32.dll")] private static extern IntPtr GetForegroundWindow(); [DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr ProcessId); [DllImport("kernel32.dll")] static extern uint GetCurrentThreadId(); [DllImport("user32.dll")] static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach); [DllImport("user32.dll")] static extern IntPtr GetFocus(); [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)] static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount); public static IntPtr GetCurrentFocusWindow() { var remoteThreadId = GetWindowThreadProcessId(GetForegroundWindow(), IntPtr.Zero); var currentThreadId = GetCurrentThreadId(); // AttachThreadInput is needed so we can get the handle of a focused window in another app AttachThreadInput(remoteThreadId, currentThreadId, true); // Get the handle of a focused window IntPtr focussed = GetFocus(); // Now detach since we got the focused handle AttachThreadInput(remoteThreadId, currentThreadId, false); return focussed; }
Detect if a mouse is attached
I had to detect if a mouse is attached. So I googled a lot, but there was no solution, so I made one. I’m not sure how good it is, but it worked in my case. Comment if you find it wrong.
bool HasMouseAttached() { ManagementObjectCollection collection; using (var searcher = new ManagementObjectSearcher(@"Select * From Win32_PointingDevice")) collection = searcher.Get(); bool hasMouse = false; foreach (var device in collection) { var name = (string) device.GetPropertyValue("Caption"); name = name.ToLowerInvariant().Trim(); if (name == "hid-compliant mouse") continue; if (name.Contains("mouse") || name.Contains("input device")) { hasMouse = true; break; } } collection.Dispose(); return hasMouse; }
Get file shortcut’s target with C#
This method doesn’t use external references like IShellLink or the scripting host. Everything is raw and based on shortcut file format documentation by Jesse Hager. This reads paths for local files. It would need tweaking if the shortcut pointed to a UNC.
LAST EDIT: 01/01/2014
Fixed path names which contain username, thanks to Daan
How to get the system colors using JQuery?
Using CSS2 we can get the current system colors (the OS colors). Here‘s an article for that. CSS knows about the colors, but how can we use them from JavaScript? It’s tricky, because an element first needs to be created to know the actual color. Here’s a workaround that returns the color in hex format:
function GetSystemColor(name) { // create an invisible element, so we can get its color var el = $("<div style='display: none; color: " + name + "'></div>"); $("body").append(el); // it must be appended to make css know the actual color var color = el.css('color'); el.remove(); // convert rgb to hex color = color.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/); function hex(x) { return ("0" + parseInt(x).toString(16)).slice(-2).toUpperCase(); } return "#" + hex(color[1]) + hex(color[2]) + hex(color[3]); }