First of all take the wpf project from. <VisualStudio> File>NewProject>WPfApplication. Give it name as MyWebBrowser and OK. Now take a textbox in main window(Used to enter url) And five buttons like( GO, Back, Forward, SavePAge, Reload)
And a webbroser control.
Or paste this code inplace of your grid(indide window tag) in main window:
<grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="573*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="35*"/>
<RowDefinition Height="272*" />
</Grid.RowDefinitions>
<Grid Background="YellowGreen" Margin="0,0,0,6">
<TextBox Height="31" HorizontalAlignment="Left" Margin="130,6,0,0" Name="txtUrl" VerticalAlignment="Top" Width="434"
TextChanged="txtUrl_TextChanged" KeyUp="txtUrl_KeyUp" />
<Button Content="Go" Height="32" HorizontalAlignment="Left" Margin="570,6,0,0" Name="btnGo" VerticalAlignment="Top"
Width="29" Click="btnGo_Click" />
<Button Content="B" Height="31" HorizontalAlignment="Left" Margin="6,4,0,0" Name="btnBack" VerticalAlignment="Top" Width="28"
Click="btnBack_Click" FontSize="20" AllowDrop="False">
</Button>
<Button Content="F" Height="30" HorizontalAlignment="Left" Margin="38,5,0,0" Name="btnForward" VerticalAlignment="Top" Width="29" Click="btnForward_Click" FontSize="20">
</Button>
<Button Content="Re" Height="30" HorizontalAlignment="Left" Margin="73,5,0,0" Name="btnReload" VerticalAlignment="Top"
Width="51" Click="btnReload_Click" FontSize="20">
</Button>
<Button Content="Save" Height="33" HorizontalAlignment="Left" Margin="605,5,0,0" Name="button1" VerticalAlignment="Top"Width="57" Click="button1_Click" />
</Grid>
<WebBrowser HorizontalAlignment="Left" Margin="0,0,0,0" Name="webBrowser1" Grid.Row="1" Grid.Column="1" Navigated="webBrowser1_Navigated" LoadCompleted="webBrowser1_LoadCompleted" />
</grid>
AND THEN IN THE CODE BEHIND USE THIS CODE:
string error = string.Empty;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
//webBrowser1.ContextMenu = browser;
//this.Title = "Shalini";
webBrowser1.ObjectForScripting = true;
error = "Don't do this";
}
private void txtUrl_TextChanged(object sender, TextChangedEventArgs e)
{
}
private void btnGo_Click(object sender, RoutedEventArgs e)
{
try
{
string Url = string.Empty;
string input = (txtUrl.Text);
if (input.Contains("https://www.") != true && input.Contains("http://www.") != true)
{
Url = "http://www." + input;
}
else
{
Url = input;
}
//Url = txtUrl.Text;
webBrowser1.Navigate(Url);
}
catch
{
MessageBox.Show(error);
}
}
private void btnBack_Click(object sender, RoutedEventArgs e)
{
try
{
webBrowser1.GoBack();
}
catch
{
MessageBox.Show(error);
}
}
private void btnForward_Click(object sender, RoutedEventArgs e)
{
try
{
webBrowser1.GoForward();
}
catch
{
MessageBox.Show(error);
}
}
private void btnReload_Click(object sender, RoutedEventArgs e)
{
try
{
webBrowser1.Refresh();
}
catch
{
MessageBox.Show(error);
}
}
private void webBrowser1_Navigated(object sender, NavigationEventArgs e)
{
try
{
if (txtUrl.Text != e.Uri.ToString())
{
txtUrl.Text = e.Uri.ToString();
}
}
catch
{
MessageBox.Show(error);
}
}
private void txtUrl_KeyUp(object sender, KeyEventArgs e)
{
try
{
if (e.Key == Key.Enter)
{
btnGo_Click(sender, e);
}
}
catch
{
MessageBox.Show(error);
}
}
private void webBrowser1_LoadCompleted(object sender, NavigationEventArgs e)
{
}
private void button1_Click(object sender, RoutedEventArgs e)
{
try
{
dynamic doc = webBrowser1.Document;
if (doc != null)
{
var htmlText = doc.documentElement.InnerHtml;
string name = htmlText;
string input = name;
if (name.Contains("<title>") == true)
{
name = GetSubstringByString("<title>", "</title>", name);
}
else
{
if (name.Contains("<TITLE>") == true)
{
name = GetSubstringByString("<TITLE>", "</TITLE>", name);
}
}
File.WriteAllText(@"Set the path here to save the downloaded file" + name + ".htm", htmlText);
MessageBox.Show("File Downloaded Successfully");
}
else
{
MessageBox.Show("Not any webpage to show");
}
}
catch
{
MessageBox.Show(error);
}
public string GetSubstringByString(string a, string b, string c)
{
string s = string.Empty;
try
{
s=c.Substring((c.IndexOf(a) + a.Length), (c.IndexOf(b) - c.IndexOf(a) - a.Length));
}
catch
{
MessageBox.Show(error);
}
return s;
}