Get path of current active window using window handle - c#

I'd like to know how to grab the Path of the current active window using C#.
i get handle of currnet active window
const int nChars = 256;
int handle = 0;
StringBuilder Buff = new StringBuilder(nChars);
handle = GetForegroundWindow();
now how do i get path of this window?
i.e: Path of "my document" window is
C:\Users\User\Documents
-=-=-==-=-=edit-=-=-=-=-=-
i want to wirte program to monitor "windows explorer" and see Where the user goes?
(i.e:user go to c:\ and then go to program files and then go to Internet Explorer and i want to get this path:C:\Program Files\Internet Explorer.

Add a reference (COM) to "Microsoft Internet Controls"
var explorer = new SHDocVw.ShellWindowsClass().Cast<SHDocVw.InternetExplorer>().Where(hwnd => hwnd.HWND == handle).FirstOrDefault();
if (explorer != null) {
string path = new Uri(explorer.LocationURL).LocalPath;
Console.WriteLine("name={0}, path={1}", explorer.LocationName, path);
}
Prints the title/path of the explorer.exe instance with the main window handle in handle.

use a thread...
Exception threadEccezione = null;
System.Threading.Thread staThread = new System.Threading.Thread(
delegate()
{
try
{
//SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindows();
var explorer = new SHDocVw.ShellWindowsClass().Cast<SHDocVw.InternetExplorer>().Where(hwnd => hwnd.HWND == handle).FirstOrDefault();
if (explorer != null)
{
string path = new Uri(explorer.LocationURL).LocalPath;
MessageBox.Show(path);
}
}
catch (Exception ex)
{
threadEccezione = ex;
}
}
);
;
staThread.Start();
staThread.Join();

Related

Kill a process that opened a file, after the file is closed by the user [WPF application]

I am trying to efficiently open-close-reopen a power bi file (.pbix) from a WPF application button click. My method starts by creating a process that opens the pbix file then kills the process when the file is closed and then when the button is clicked again creates a new process to re-open the file.
Kindly find below the code I use to execute the steps above.
namespace TestApp
{
public class MainWindowViewModel : INotifyPropertyChanged
{
public int CheckFileIsOpen(string filenamepath)
{
try
{
using FileStream fs = new FileStream(filenamepath, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
return 0;
}
catch (Exception)
{
WindowEffect = new BlurEffect();
Mouse.OverrideCursor = null;
bool? Result = new CustomMessageBox($"File: {filenamepath.Split(#"\").Last()} in use!\nClose it and try again.", "File used by another process", MessageType.Error, MessageButtons.Ok).ShowDialog(); //this is a MessageBox object
if (Result.Value)
{
WindowEffect = null;
return 1;
}
else
{
WindowEffect = null;
return 2;
}
}
}
private void OpenOnlineLocally(bool open_local)
{
Process p = new Process();
string copy_name = "File_Copy.pbix";
string path = AppDomain.CurrentDomain.BaseDirectory; //the directory the .exe file runs.
try
{
Mouse.OverrideCursor = Cursors.Wait;
if (open_local == true)
{
int IsPBIFileOpen = CheckFileIsOpen($#"{path}{copy_name}");
if (new[] { 1, 2 }.Contains(IsPBIFileOpen))
{
return;
}
//Open the file using the system process
p.StartInfo = new ProcessStartInfo($"{path}{copy_name}")
{
UseShellExecute = true
};
p.Start();
}
else
{
OpenUrl("https://app.powerbi.com/...");
}
}
finally
{
if (p.HasExited) { p.Kill(); } //kill the process if the user closed the .pbix file
}
}
public ICommand ExportPowerBICommand //binded to a button click command in xaml
{
get { return new DelegateCommand<object>(FuncExportPowerBI); }
}
public void FuncExportPowerBI(object parameter)
{
Mouse.OverrideCursor = Cursors.Wait;
try
{
OpenOnlineLocally(true);
}
finally
{
Mouse.OverrideCursor = null;
}
}
}
}
The above code generates this error in the finally statement:
System.InvalidOperationException: 'No process is associated with this object.'
Some notes after experimentation:
The process should be killed when the user closes the .pbix file (i.e. clicks the X icon on top right corner of the desktop app). If the process is not killed and the user re-clicks the button to re-open the file then I get an error that the file is already opened and used by another process.
I prefer to avoid a solution that uses process.WaitForExit(), for two reasons. First, the application freezes while the file is used by the user. Second, it takes a couple of seconds for the desktop to realize that the process has exited so it can kill() it (not time efficient).
Since you're running .NET 5, there's an asynchronous method Process.WaitForExitAsync(). Async operation will not block the UI.
I've made the changes to two methods
private async Task OpenOnlineLocally(bool open_local)
{
Process p = new Process();
string copy_name = "File_Copy.pbix";
string dir = AppDomain.CurrentDomain.BaseDirectory; //the directory the .exe file runs.
string path = Path.Combine(dir, copy_name);
try
{
if (open_local == true)
{
int IsPBIFileOpen = CheckFileIsOpen(path);
if (IsPBIFileOpen != 0)
{
return;
}
//Open the file using the system process
p.StartInfo = new ProcessStartInfo(path)
{
UseShellExecute = true
};
p.Start();
await p.WaitForExitAsync();
}
else
{
OpenUrl("https://app.powerbi.com/...");
}
}
finally
{
if (!p.HasExited) { p.Kill(); } //kill the process if the user closed the .pbix file
}
}
public async void FuncExportPowerBI(object parameter)
{
Mouse.OverrideCursor = Cursors.Wait;
try
{
await OpenOnlineLocally(true);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message); // handle possible exception here
}
Mouse.OverrideCursor = null;
}

Can't save the session of Whatsapp web with Selenium c#

I want to save session of whatsapp web so I do not have to scan qr-code every time I open whatsapp web. I use:
options.AddArgument("--user-data-dir=" + FolderPathToStoreSession)
but the qr-code appear again.
Here is the method to open whatsapp web for first time to scan qr code and save it to folder:
public static int OpenNewChrome(
string Website,
int TimeToWaitInMinutes,
string FolderPathToStoreSession)
{
ChromeOptions options = null;
ChromeDriver driver = null;
try
{
//chrome process id
int ProcessId = -1;
//time to wait until open chrome
var TimeToWait = TimeSpan.FromMinutes(TimeToWaitInMinutes);
ChromeDriverService cService = ChromeDriverService.CreateDefaultService();
//hide dos screen
cService.HideCommandPromptWindow = true;
options = new ChromeOptions();
//session file directory
options.AddArgument("--user-data-dir=" + FolderPathToStoreSession);
driver = new ChromeDriver(cService, options, TimeToWait);
//set process id of chrome
ProcessId = cService.ProcessId;
driver.Navigate().GoToUrl(Website);
FRM_MSG f2 = new FRM_MSG();
DialogResult r = f2.ShowDLG(" ",
"Did you successfully finish scan bardcode?",
FRM_MSG.MSGIcon.Question,
FRM_MSG.BTNS.Two,
new string[] { "Yes Finish", "Cannot scan qr-code" });
if (driver != null)
{
driver.Close();
driver.Quit();
driver.Dispose();
}
if (r == DialogResult.Yes)
return ProcessId;
return -1;
}
catch (Exception ex)
{
if (driver != null)
{
driver.Close();
driver.Quit();
driver.Dispose();
}
driver = null;
throw ex;
}
}
and here is method to restore session:
public static int OpenOldChrome(
string Website,
int TimeToWaitInMinutes,
string FolderPathToStoreSession)
{
ChromeOptions options = null;
ChromeDriver driver = null;
try
{
//chrome process id
int ProcessId = -1;
//time to wait until open chrome
var TimeToWait = TimeSpan.FromMinutes(TimeToWaitInMinutes);
ChromeDriverService cService = ChromeDriverService.CreateDefaultService();
//hide dos screen
cService.HideCommandPromptWindow = true;
options = new ChromeOptions();
//session file directory
options.AddArgument("--user-data-dir=" + FolderPathToStoreSession);
driver = new ChromeDriver(cService, options, TimeToWait);
//set process id of chrome
ProcessId = cService.ProcessId;
Thread.Sleep(50000);
FRM_MSG f2 = new FRM_MSG();
DialogResult r = f2.ShowDLG(" ",
"Did you wnat to exit?",
FRM_MSG.MSGIcon.Question,
FRM_MSG.BTNS.Two,
new string[] { "Yes", "No" });
if (driver != null)
{
driver.Close();
driver.Quit();
driver.Dispose();
}
if (r == DialogResult.Yes)
return ProcessId;
return -1;
}
catch (Exception ex)
{
if (driver != null)
{
driver.Close();
driver.Quit();
driver.Dispose();
}
driver = null;
throw ex;
}
}
The problem as I said the qr-code appear again, I want to scan qr-code only once
I use google chrome version 74, web driver v 3.141.0.
Do check if the profile folder is correct . An old thread here mention that you need to add \Default to the profile path.
Have you try adding this to see if this helps
options.addArguments("chrome.switches", "--disable-extensions")

How to implement a wpf launcher with SetForegroundWindow

I'm trying to implement a button command that launches a new WPF application the first time the user clicks the button and then (when the user clicks the button again) sends it to foreground, if it's already running. The whole thing is running on .Net v4.0
What I've tried to do is working fine, as expected, when the launched process is a normal WPF application, but it doesn't play nice if the launched WPF application has a splash screen. The problem is that SetForegroundWindow fails, because I'm unable to retrieve the correct window handle in that specific case. Can you suggest a fix or a work-around? Assume you can modify the source of both the launcher and the launched WPF.
The relevant code from the View Model of the launcher
private void ClaimRptLogic()
{
if (ClaimRptHandle != IntPtr.Zero)
{
ShowWindow(ClaimRptHandle, SW_RESTORE);
LaunchState = SetForegroundWindow(ClaimRptHandle)? "" : "can't set to foreground";
return;
}
Process rpt = new Process();
rpt.StartInfo = new ProcessStartInfo()
{
WorkingDirectory = ConfigurationManager.AppSettings["ClaimRptPath"],
FileName = ConfigurationManager.AppSettings["ClaimRptexe"]
};
rpt.Start();
BackgroundWorker bg = new BackgroundWorker();
bg.DoWork += new DoWorkEventHandler((o, e) => {
rpt.WaitForExit();
});
bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler((o, e) => {
ClaimRptHandle = IntPtr.Zero;
LaunchState = "ClaimRpt closed";
});
bg.RunWorkerAsync();
Thread.Sleep(3000);
ClaimRptHandle = rpt.MainWindowHandle;
}
Assume you can modify the source of both the launcher and the launched
WPF.
Based on this assumption, I could determine the correct handle in the Loaded event of the launched WPF application and send it back to the launcher using a Named Pipe.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var callback = new WindowInteropHelper(this).Handle;
BackgroundWorker bg = new BackgroundWorker();
bg.DoWork += (s, a) =>
{
WritePipe("at loaded evt: " + callback);
};
bg.RunWorkerAsync();
}
private void WritePipe(string line)
{
using (NamedPipeServerStream server =
new NamedPipeServerStream(Environment.UserName, PipeDirection.InOut))
{
server.WaitForConnection();
using (StreamWriter sw = new StreamWriter(server))
{
sw.WriteLine(line);
}
}
}
and read the correct window handle from the same Named Pipe in another background worker of the launcher
bg.RunWorkerAsync();
Thread.Sleep(3000);
if (rpt.HasExited)
{
return;
}
LaunchedHandle = rpt.MainWindowHandle;
BackgroundWorker bgPipe = new BackgroundWorker();
bgPipe.DoWork += new DoWorkEventHandler((o, e) => {
while (!rpt.HasExited)
{
string testHandle = ReadPipe();
if (testHandle.StartsWith("at loaded evt: "))
{
Debug.WriteLine(testHandle);
Debug.WriteLine("CallBack from Launched Process!");
var handle = testHandle.Replace("at loaded evt: ","");
LaunchedHandle = new IntPtr(int.Parse(handle));
return;
}
LaunchedHandle = rpt.MainWindowHandle;
Thread.Sleep(500);
}
Debug.WriteLine("Process exited!");
});
bgPipe.RunWorkerAsync();
CanLaunchCmd = true;
with
private string ReadPipe()
{
string line = "";
using (NamedPipeClientStream client =
new NamedPipeClientStream(".", Environment.UserName, PipeDirection.InOut))
{
client.Connect();
using (StreamReader sr = new StreamReader(client))
{
line = sr.ReadLine();
}
return line;
}
}
Of course, I'm open to different ideas.
Just another option, if you can't modify the launched WPF app, but you know the title caption of its main window, besides the process id, of course.
In that case the background search would be
LaunchedHandle = rpt.MainWindowHandle;
mainWin = rpt.MainWindowHandle;
BackgroundWorker bgTitle = new BackgroundWorker();
bgTitle.DoWork += new DoWorkEventHandler((o, e) => {
while (!rpt.HasExited)
{
LaunchedHandle = MainWindowHandle(rpt);
Thread.Sleep(500);
}
Debug.WriteLine("Process exited!");
});
bgTitle.RunWorkerAsync();
using a filter based on the process id
private IntPtr MainWindowHandle(Process rpt)
{
EnumWindowsProc ewp = new EnumWindowsProc(EvalWindow);
EnumWindows(ewp, new IntPtr(rpt.Id));
return mainWin;
}
and a callback testing the title caption (in this example it's Launched)
private bool EvalWindow(IntPtr hWnd, IntPtr lParam)
{
int procId;
GetWindowThreadProcessId(hWnd, out procId);
if (new IntPtr(procId) != lParam)
{
return true;
}
StringBuilder b = new StringBuilder(50);
GetWindowText(hWnd, b, 50);
string test = b.ToString();
if (test.Equals("Launched"))
{
mainWin = hWnd;
}
return true;
}

C# Arguments for a specific process, open browser with url

I am writing an application that is supposed to open a certain process on the click of a button. However, the user has the ability to add new buttons. I'm using the following code for the action that occurs that starts the process on button click:
private void StartProcess(string path)
{
ProcessStartInfo StartInformation = new ProcessStartInfo();
StartInformation.FileName = path;
Process process = Process.Start(StartInformation);
process.EnableRaisingEvents = true;
}
private void ClickFunc(object sender, RoutedEventArgs e)
{
if (File.Exists(ProgramPath))
{
StartProcess(ProgramPath);
}
else
{
MessageBox.Show("Specified path does not exist, please try again.", "Bad File Path Error", MessageBoxButton.OK);
}
}
What I'm trying to accomplish is, when the user creates a button for a webpage, it opens the browser, then the webpage. Any ideas?
Thank you in advance!
To start a process to open the browser with a specific url you can try this:
string url = "http://www.stackoverflow.com";
var process = System.Diagnostics.Process.Start(url);
But sometimes if you have problems with the path of your browser, it cannot work properly. The function bellow gives you the path of the browser in the machine.
public static string GetDefaultBrowserPath()
{
string urlAssociation = #"Software\Microsoft\Windows\Shell\Associations\UrlAssociations\http";
string browserPathKey = #"$BROWSER$\shell\open\command";
RegistryKey userChoiceKey = null;
string browserPath = “”;
try
{
//Read default browser path from userChoiceLKey
userChoiceKey = Registry.CurrentUser.OpenSubKey(urlAssociation + #"\UserChoice", false);
//If user choice was not found, try machine default
if (userChoiceKey == null)
{
//Read default browser path from Win XP registry key
var browserKey = Registry.ClassesRoot.OpenSubKey(#"HTTP\shell\open\command", false);
//If browser path wasn’t found, try Win Vista (and newer) registry key
if (browserKey == null)
{
browserKey =
Registry.CurrentUser.OpenSubKey(
urlAssociation, false);
}
var path = CleanifyBrowserPath(browserKey.GetValue(null) as string);
browserKey.Close();
return path;
}
else
{
// user defined browser choice was found
string progId = (userChoiceKey.GetValue("ProgId").ToString());
userChoiceKey.Close();
// now look up the path of the executable
string concreteBrowserKey = browserPathKey.Replace(“$BROWSER$”, progId);
var kp = Registry.ClassesRoot.OpenSubKey(concreteBrowserKey, false);
browserPath = CleanifyBrowserPath(kp.GetValue(null) as string);
kp.Close();
return browserPath;
}
}
catch(Exception ex)
{
return "";
}
}
And you can use the path of the browser and the url of website, for sample:
string url = "http://www.stackoverflow.com";
var process = System.Diagnostics.Process.Start(GetDefaultBrowserPath(), url);
In the url string you can pass the webpage link. It will open the browser with the url.
See more:
http://www.seirer.net/blog/2014/6/10/solved-how-to-open-a-url-in-the-default-browser-in-csharp

AutomationElement.FromHandle(IntPtr Handle) stops the application

private string GetCurrentChromeUrl()
{
try
{
string url = null;
int handle = GetForegroundWindow();
AutomationElement elm = AutomationElement.FromHandle((IntPtr)handle);
AutomationElement elmUrlBar = elm.FindFirst(TreeScope.Descendants,
new PropertyCondition(AutomationElement.NameProperty, "Address and search bar"));
if (elmUrlBar != null)
{
AutomationPattern[] patterns = elmUrlBar.GetSupportedPatterns();
if (patterns.Length > 0)
{
ValuePattern val = (ValuePattern)elmUrlBar.GetCurrentPattern(patterns[0]);
url = val.Current.Value;
//break;
}
}
return url;
}
catch (Exception e1)
{
return "";
}
}
I'm trying to find the URL from google chrome browser.
And I'm using above code. It is work good in other application but in my case it stops my application.
But the main issue is that it will work fine when I'm debugging it, so what wrong when no debugging.
please give your solutions.
thanx in advance
I'm passing through this, it happen cause of the intptr type, it depends of the build of the machine, sometime the windowhandle is too big for an intptr of a 32bits machine.

Categories