C# web browser save search engine settings - c#

I'm working on a web browser where the default search engine is Google until the user clicks on the Yahoo (or another search engine) button. The problem is, after you start a new session, it opens with Google again. How to you save that?
I'm aware how to save settings with Properties.Settings.Default.Save(); however it doesn't seem to work with a click event. (Where the code is to change the search engine)
I'm using GeckoFX. Code for the Yahoo setting is:
goo.Enabled = true;
y.Enabled = false;
bin.Enabled = true;
ba.Enabled = true;
ya.Enabled = true;
sear.Text = "Yahoo!";
And is performed with:
if (y.Enabled == false)
{
W.Navigate("search.yahoo.com/search?p=" + q.Text);
W.Select();
}

Related

c# AutoUpdater.NET - Application should not close, if update available

I'm using AutoUpdater.NET for updating my applications. It's a great library. My program collects data for inventory system-data, sent it to database and than close the application.
this.Close();
The problem is, that the program also close, if an update is available. I want, that the application stay open, until the user update or cancel. I know that I have to check, if update is available or update-form is open, but I don't know how. Could someone please help? (Sorry, I'm beginner)
AutoUpdater.Start(updatePath);
AutoUpdater.ShowSkipButton = false;
AutoUpdater.ShowRemindLaterButton = false;
AutoUpdater.Mandatory = true;
//AutoUpdater.UpdateMode = Mode.Forced;
AutoUpdater.RunUpdateAsAdmin = false;
var currentDirectory = new DirectoryInfo(Environment.CurrentDirectory);
if (currentDirectory.Parent != null)
{
AutoUpdater.InstallationPath = currentDirectory.FullName;
}
The solution (in my case) was to replace Environment.CurrentDirectory with Application.StartupPath

Download file from opensubtitles.org

I need to download files from opensubtitles.org trough my application which is written in GTK# and C#. The app is based on .NET 4.0 framework.
At first this was the code I was using:
var tZip = new FastZip();
try {
var tRequest = (HttpWebRequest)HttpWebRequest.Create(tDownloadUrl);
var tZipResponse = (HttpWebResponse)tRequest.GetResponse();
using (var tStream = tZipResponse.GetResponseStream()) {
using (var tMemStream = new MemoryStream()) {
tStream.CopyTo(tMemStream);
var tTempPath = Globals.video_location + "OSD";
Directory.CreateDirectory(tTempPath);
tZip.ExtractZip(tMemStream, tTempPath, FastZip.Overwrite.Always, null, #"\.srt$", null, false, true);
var tDirInfo = new DirectoryInfo(tTempPath);
var tFileInfo = new FileInfo(Globals.location_video);
var tSrtFile = tDirInfo.EnumerateFiles().FirstOrDefault();
if (tSrtFile == null) {
writeLog("No .srt file found in zip..");
goto text;
}
writeLog("Downloaded and unpacked: " + tSrtFile.Name);
File.Copy(tSrtFile.FullName, Globals.video_location+Globals.video_name+".srt", true);
Globals.savedTitle = Globals.video_location+Globals.video_name+".srt";
// clean up..
Directory.Delete(tTempPath, true);
writeLog("Deleted temp folder.");
return true;
}
}}
And that worked really well up until few days ago, now it is returning a bunch of html code instead of .zip file. I tried even something like this:
WebClient client = new WebClient();
client.DownloadFile(link, #"OSD\test.zip");
But everything just keeps returning bunch of html code.
The link I am usually trying to download is something like this:
http://dl.opensubtitles.org/en/download/subad/4287952
If you click on the link above it will just redirect you to the opensubtitles.org page of that particular subtitle. But if you right mouse click on that link and then select "open in new tab" or "open in new window" it will automatically start the download. (Tested in Firefox)
Also as soon as I paste that link in "Internet Download Manager" application, it will start the download of the zip file automatically.
If you can help me to resolve this problem I will truly be grateful.
Kind Regards.
I got into this problem because I was filtering the website xml directly. Like from a link such as this one: opensubtitles.org example
And in the beginning it used to work well, but then they changed something on the website and it stopped working. So what I did was build on top of this: OSHandler
That handler library is using XML-RPC so I believe there won't be any problems in the future.

Coded UI failing on the exception "Another control is blocking the control. Please make the blocked control visible ...." on a dialog window

I can't seem to get past this exception on my coded UI project for a window dialog box. I've tried using always search and some other options. I was trying to use the option of open a window dialog button but that didn't seem to work since i did not see the open button only "OK" and a few ones that didn't pertain to what i was trying to do.
I see one of my window is returning 5 windows, I'm trying to use the code Order Of overcation but that doesn't seem to work.
Currently here is my latest code which is still not working and still giving me the same exception error
WinWindow myDialogBox = new WinWindow();
myDialogBox.FilterProperties.Add(WinWindow.PropertyNames.OrderOfInvocation, "5");
myDialogBox.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);
myDialogBox.SearchProperties[WinWindow.PropertyNames.ClassName] = "DirectUIHWND";
myDialogBox.SearchProperties[WinWindow.PropertyNames.ControlId] = "0";
myDialogBox.DrawHighlight();
WinWindow MyOpbenButton = new WinWindow(myDialogBox);
// myDialogBox.SearchProperties.Add(WinWindow.PropertyNames.OrderOfInvocation, "0");
MyOpbenButton.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);
MyOpbenButton.SearchProperties[WinWindow.PropertyNames.Name] = "&Open";
MyOpbenButton.SearchProperties[WinWindow.PropertyNames.ClassName] = "Button";
//MyOpbenButton.SearchProperties[WinWindow.PropertyNames.ControlId] = "0";
MyOpbenButton.DrawHighlight();
//Testing this window produces 5 window with the same name and
control ID.
WinWindow matchingWindows = new WinWindow(myDialogBox);
matchingWindows.SearchProperties.Add("Name", "&Open");
// matchingWindows.SearchProperties.Add("ControlId", "0");
matchingWindows.SearchProperties.Add("ClassName", "Button");
UITestControlCollection windowsFound = matchingWindows.FindMatchingControls();
WinPane MyOpbenPane = new WinPane(MyOpbenButton);
MyOpbenPane.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);
//MyOpbenPane.SearchProperties[WinPane.PropertyNames.Name] = "Open";
MyOpbenPane.SearchProperties[WinPane.PropertyNames.ClassName] = "Button";
// var myDialog = sharedControls.UIWindowsInternetExploWindow.UIWindowsInternetExplo_Open_Save_Dialog_LinksPage.UIItemWindow;
MyOpbenPane.DrawHighlight();
return MyOpbenPane;
The error message that this is producing is the following:
Another control is blocking the control. Please make the blocked
control visible and retry the action. Additional Details:
\r\nTechnologyName: 'MSAA'\r\nName: '&Open'\r\nClassName:
'Button'\r\nControlType: 'Window'\r\n
I used the test builder to get around this issue.
I found that the dialog screen was being captured with the
classname of 32770. I also found that i could not pass off my browser window to window from there i hand coded the remaining screen using the test builder.
here is the code below.
WinWindow window = new WinWindow();
window.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);
window.SearchConfigurations.Add(SearchConfiguration.VisibleOnly);
window.SearchProperties[WinWindow.PropertyNames.ClassName] = "#32770";
window.SearchProperties[WinWindow.PropertyNames.Name] = "Windows Internet Explorer";
window.WindowTitles.Add("Windows Internet Explorer");
//window.DrawHighlight();
WinCustom myDialogBox = new WinCustom(window);
myDialogBox.SearchConfigurations.Add(SearchConfiguration.VisibleOnly);
myDialogBox.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);
myDialogBox.SearchProperties[WinCustom.PropertyNames.ControlType] = "Dialog";
myDialogBox.SearchProperties[WinCustom.PropertyNames.Name] = "Windows Internet Explorer";
myDialogBox.WindowTitles.Add("Windows Internet Explorer");
//myDialogBox.DrawHighlight();
WinWindow myDialogwindow = new WinWindow(window);
myDialogwindow.SearchConfigurations.Add(SearchConfiguration.VisibleOnly);
myDialogwindow.SearchConfigurations.Add(SearchConfiguration.AlwaysSearch);
myDialogwindow.SearchProperties[WinWindow.PropertyNames.AccessibleName] = "Windows Internet Explorer";
myDialogwindow.SearchProperties[WinWindow.PropertyNames.ClassName] = "DirectUIHWND";
myDialogwindow.WindowTitles.Add("Windows Internet Explorer");
return myDialogwindow;
I run into this a lot. I have an application on which the tabs always get reported as hidden, even though they're clearly visible. What has worked well for me so far has been to use keyboard.sendkeys to send keystrokes that select the tab.

Stop process if webBrowser control hangs

I am using the WebBrowser control.
This works fine most of the time however wehn navigating to a new page or waiting for a new page to load can sometimes hangs.
Is there a way to catch this? i.e. if the page is failing to navigate or load after a certain amount of time then kill the process?
I am using the - webBrowser1_DocumentCompleted event to pick up ertain behaviours when the page loads/navigates as expected however not sure how to catch if a page is hanging??
Maby you should try to implement some kind of timeout logic? There are quite many samples in web about this. F.e. this one
Also you might be interested in this event of WebBrowserControl ProgressChanged
This is due to that webbrowser component is very basic model of internet explorer, and it get stuck at ajax pages. You can fix this problem explicitly to use latest version of internet explorer... Using this code...
try
{
string installkey = #"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION";
string entryLabel = "YourExe.exe";
string develop = "YourExe.vshost.exe";//This is for Visual Studio Debugging...
System.OperatingSystem osInfo = System.Environment.OSVersion;
string version = osInfo.Version.Major.ToString() + '.' + osInfo.Version.Minor.ToString();
uint editFlag = (uint)((version == "6.2") ? 0x2710 : 0x2328); // 6.2 = Windows 8 and therefore IE10
Microsoft.Win32.RegistryKey existingSubKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(installkey, false); // readonly key
if (existingSubKey.GetValue(entryLabel) == null)
{
existingSubKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(installkey, true); // writable key
existingSubKey.SetValue(entryLabel, unchecked((int)editFlag), Microsoft.Win32.RegistryValueKind.DWord);
}
if (existingSubKey.GetValue(develop) == null)
{
existingSubKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(installkey, true); // writable key
existingSubKey.SetValue(develop, unchecked((int)editFlag), Microsoft.Win32.RegistryValueKind.DWord);
}
}
catch
{
MessageBox.Show("You Don't Have Admin Previlege to Overwrite System Settings");
}
}
Right Click Both your Exe. And vshost.exe and Run as Administrator To Update Registry for this Application....

How to intercept when user click on a link in a webbrowser

I'm trying to intercept tapping on a link in a WebBrowser control.
My HTML page contains custom links, for some starting with shared:// I'd like to intercept when the user tap on it.
On iPhone I would use the webView:shouldStartLoadWithRequest:navigationType: method, and look at the URL that is selected.
I haven't managed to reproduce a similar behaviour with Silverlight for Windows Phone.
I do something like:
{
webBrowser1.Navigating += new EventHandler<NavigatingEventArgs>(webBrowser1_Navigating);
}
void webBrowser1_Navigating(object sender, NavigatingEventArgs e)
{
string scheme = null;
try
{
scheme = e.Uri.Scheme; // <- this is throwing an exception here
}
catch
{
}
if (scheme == null || scheme == "file")
return;
// Not going to follow any other link
e.Cancel = true;
if (scheme == "shared")
{
}
But I guess an exception when reading some properties of the Uri, when it's a standard Uri with a default file:// URL
Additionally, the Navigating event isn't even triggered for links starting with shared://
Now that I'm able to capture tapping on a shared:// I do not care much, but at least I'd like to be able to retrieve the URL we're going to navigate to, and cancel the default operation for a particular URL.
Any ideas what's going on?
Thanks
Edit:
It turned out that the problem is that the Navigating event is only generated for the following links: file://, http:// or mailto://
The scheme attributes of the Uri is only available for the http:// and mailto:// links
so what I did in the end is replace the shared:// link with http://shared/blah ... And I look at the URL... This works for my purpose. I can now have links that have a different action (like opening an extra window) depending on the links in the html.
Here is my final code, in case this is useful for someone in the future:
For an about screen, I use an html file displayed in a WebBrowser component.
The about page has a "tell your friend about this app" link as well as links to external web site.
It also has local subpages.
Local sub-pages are linked to using a file:// link. Those can be navigated within the WebBrowser component.
External links are opened externally with Internet Explorer.
Tell your friend link is made of a http://shared link, that opens an email with a pre-set subject and body. Unfortunately, no other scheme than the standard ones are usable as they do not trigger a Navigating event
There's also a support link which is a mailto:// link and opens an EmailComposeTask
void webBrowser1_Navigating(object sender, NavigatingEventArgs e)
{
String scheme = null;
try
{
scheme = e.Uri.Scheme;
}
catch
{
}
if (scheme == null || scheme == "file")
return;
// Not going to follow any other link
e.Cancel = true;
if (scheme == "http")
{
// Check if it's the "shared" URL
if (e.Uri.Host == "shared")
{
// Start email
EmailComposeTask emailComposeTask = new EmailComposeTask();
emailComposeTask.Subject = "Sharing an app with you";
emailComposeTask.Body = "You may like this app...";
emailComposeTask.Show();
}
else
{
// start it in Internet Explorer
WebBrowserTask webBrowserTask = new WebBrowserTask();
webBrowserTask.Uri = new Uri(e.Uri.AbsoluteUri);
webBrowserTask.Show();
}
}
if (scheme == "mailto")
{
EmailComposeTask emailComposeTask = new EmailComposeTask();
emailComposeTask.To = e.Uri.AbsoluteUri;
emailComposeTask.Show();
}
}

Categories

Resources