How can I get client PC Printer settings using C#, asp.net - c#

I have SSRS report runnig on a web application. I want to print SSRS report data directly on the client's printer. The application works well in my pc but when I publish the application on IIS web server, instead of retrieving the printer settings of client pc, the application is trying to look for printers that are installed in the server machine.
I am using System.Drawing.Printing, Is there any way to get the settings of installed printer in the client computer or can we print SSRS reports from Web using C# ?
I am using following code.
using System;
using System.IO;
using System.Text;
using System.Globalization;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.Collections.Specialized;
using System.Collections.Generic;
using Microsoft.Reporting.WebForms;
using SeaWorld.Common.Helper;
namespace SeaWorld.BusinessObjects.BAL.Generated
{
public class ReportPrintDocument : PrintDocument
{
private PageSettings m_pageSettings;
private int m_currentPage;
private List<Stream> m_pages = new List<Stream>();
public ReportPrintDocument(ServerReport serverReport): this((Report)serverReport)
{
RenderAllServerReportPages(serverReport);
}
//public ReportPrintDocument(LocalReport localReport): this((Report)localReport)
//{
// RenderAllLocalReportPages(localReport);
//}
private ReportPrintDocument(Report report)
{
// Set the page settings to the default defined in the report
ReportPageSettings reportPageSettings = report.GetDefaultPageSettings();
// The page settings object will use the default printer unless
// PageSettings.PrinterSettings is changed. This assumes there
// is a default printer.
m_pageSettings = new PageSettings();
m_pageSettings.PaperSize = reportPageSettings.PaperSize;
m_pageSettings.Margins = reportPageSettings.Margins;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
foreach (Stream s in m_pages)
{
s.Dispose();
}
m_pages.Clear();
}
}
protected override void OnBeginPrint(PrintEventArgs e)
{
base.OnBeginPrint(e);
m_currentPage = 0;
}
protected override void OnPrintPage(PrintPageEventArgs e)
{
base.OnPrintPage(e);
Stream pageToPrint = m_pages[m_currentPage];
pageToPrint.Position = 0;
// Load each page into a Metafile to draw it.
using (Metafile pageMetaFile = new Metafile(pageToPrint))
{
Rectangle adjustedRect = new Rectangle(
e.PageBounds.Left - (int)e.PageSettings.HardMarginX,
e.PageBounds.Top - (int)e.PageSettings.HardMarginY,
e.PageBounds.Width,
e.PageBounds.Height);
// Draw a white background for the report
e.Graphics.FillRectangle(Brushes.White, adjustedRect);
// Draw the report content
e.Graphics.DrawImage(pageMetaFile, adjustedRect);
// Prepare for next page. Make sure we haven't hit the end.
m_currentPage++;
e.HasMorePages = m_currentPage < m_pages.Count;
}
}
protected override void OnQueryPageSettings(QueryPageSettingsEventArgs e)
{
e.PageSettings = (PageSettings)m_pageSettings.Clone();
}
private void RenderAllServerReportPages(ServerReport serverReport)
{
string deviceInfo = CreateEMFDeviceInfo();
// Generating Image renderer pages one at a time can be expensive. In order
// to generate page 2, the server would need to recalculate page 1 and throw it
// away. Using PersistStreams causes the server to generate all the pages in
// the background but return as soon as page 1 is complete.
NameValueCollection firstPageParameters = new NameValueCollection();
firstPageParameters.Add("rs:PersistStreams", "True");
// GetNextStream returns the next page in the sequence from the background process
// started by PersistStreams.
NameValueCollection nonFirstPageParameters = new NameValueCollection();
nonFirstPageParameters.Add("rs:GetNextStream", "True");
string mimeType;
string fileExtension;
Stream pageStream = serverReport.Render("IMAGE", deviceInfo, firstPageParameters, out mimeType, out fileExtension);
// The server returns an empty stream when moving beyond the last page.
while (pageStream.Length > 0)
{
m_pages.Add(pageStream);
pageStream = serverReport.Render("IMAGE", deviceInfo, nonFirstPageParameters, out mimeType, out fileExtension);
}
}
private void RenderAllLocalReportPages(LocalReport localReport)
{
string deviceInfo = CreateEMFDeviceInfo();
Warning[] warnings;
localReport.Render("IMAGE", deviceInfo, LocalReportCreateStreamCallback, out warnings);
}
private Stream LocalReportCreateStreamCallback(
string name,
string extension,
Encoding encoding,
string mimeType,
bool willSeek)
{
MemoryStream stream = new MemoryStream();
m_pages.Add(stream);
return stream;
}
private string CreateEMFDeviceInfo()
{
PaperSize paperSize = m_pageSettings.PaperSize;
Margins margins = m_pageSettings.Margins;
// The device info string defines the page range to print as well as the size of the page.
// A start and end page of 0 means generate all pages.
return string.Format(
CultureInfo.InvariantCulture,
"<DeviceInfo><OutputFormat>emf</OutputFormat><StartPage>0</StartPage><EndPage>0</EndPage><MarginTop>{0}</MarginTop><MarginLeft>{1}</MarginLeft><MarginRight>{2}</MarginRight><MarginBottom>{3}</MarginBottom><PageHeight>{4}</PageHeight><PageWidth>{5}</PageWidth></DeviceInfo>",
ToInches(margins.Top),
ToInches(margins.Left),
ToInches(margins.Right),
ToInches(margins.Bottom),
ToInches(paperSize.Height),
ToInches(paperSize.Width));
}
private static string ToInches(int hundrethsOfInch)
{
double inches = hundrethsOfInch / 100.0;
return inches.ToString(CultureInfo.InvariantCulture) + "in";
}
}
}
On print button click
void ctrlSettingToolbar_btnPrintPressed(object sender, EventArgs e)
{
try
{
ReportPrintDocument rp = new ReportPrintDocument(rptBillOfLoadings.ServerReport);
rp.Print();
}
catch (Exception ex)
{
LogHelper.PrintError("Error:", ex);
}
}

You can use javascript function window.Print()
This will list you client machine printers and print your page.
Hope below code will help..try
<html>
<head>
<script>
function page()
{
window.print()
}
</script>
</head>
<body>
<input type="button" value="Print page" onclick="page()">
</body>
</html>
But this will print the whole page..in your case its a report to print
hope this link can guide you
print crystal report to client printer
print report to client default printer

Related

C# : Is there a way to save a Window as PDF?

I am using C# to generate a Window (scrollbar) with a lot of results: Window ResultsWindow = new Window();
At the bottom, there are two buttons, i.e. Cancel and Print. The first one does what it should. Nevertheless, the Print button should somehow convert the Window into a PDF File, or maybe one step inbetween where the user can save it afterwards.
private void Print_click(object sender, RoutedEventArgs e)
{
//add code to print the whole window??
ResultsWindow.Close();
}
Does anyone of you have an idea how this could work?
Best regards
This isn't particularly pretty (or tested) but uses information from this answer.
This creates an XPS file of your window, and converts it to PDF
using System.IO;
using System.IO.Packaging;
using System.Windows;
using System.Windows.Xps;
using System.Windows.Xps.Packaging;
namespace WpfApp8
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
/*
* Convert WPF -> XPS -> PDF
*/
MemoryStream lMemoryStream = new MemoryStream();
Package package = Package.Open(lMemoryStream, FileMode.Create);
XpsDocument doc = new XpsDocument(package);
XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
// This is your window
writer.Write(this);
doc.Close();
package.Close();
// Convert
MemoryStream outStream = new MemoryStream();
PdfSharp.Xps.XpsConverter.Convert(lMemoryStream, outStream, false);
// Write pdf file
FileStream fileStream = new FileStream("C:\\test.pdf", FileMode.Create);
outStream.CopyTo(fileStream);
// Clean up
outStream.Flush();
outStream.Close();
fileStream.Flush();
fileStream.Close();
}
}
}
It uses the PdfSharp nuget package and the kenjiuno.PdfSharp.Xps package to add XPS support to PdfSharp
Asuming you are using WPF, here is how I accomplished something similar:
private void Button_Click(object sender, RoutedEventArgs e)
{
var wasMax = this.WindowState == WindowState.Maximized;
UBlattWindow.WindowState = WindowState.Normal;
var initHeight = UBlattWindow.ActualHeight;
var initWidth = UBlattWindow.ActualWidth;
UBlattWindow.Width = 955;
UBlattWindow.Height = UBlattWindow.Height + (ScrollerContent.ActualHeight - Scroller.ActualHeight) + 20;
Print(printGrid);
UBlattWindow.Height = initHeight;
UBlattWindow.Width = initWidth;
if (wasMax)
{
UBlattWindow.WindowState = WindowState.Maximized;
}
}
private void Print(Visual v)
{
System.Windows.FrameworkElement e = v as System.Windows.FrameworkElement;
if (e == null)
return;
PrintDialog pd = new PrintDialog();
if (pd.ShowDialog() == true)
{
PageMediaSize pageSize = null;
pageSize = new PageMediaSize(PageMediaSizeName.ISOA4);
pd.PrintTicket.PageMediaSize = pageSize;
//store original scale
Transform originalScale = e.LayoutTransform;
//get selected printer capabilities
System.Printing.PrintCapabilities capabilities = pd.PrintQueue.GetPrintCapabilities(pd.PrintTicket);
//get scale of the print wrt to screen of WPF visual
double scale = Math.Min(capabilities.PageImageableArea.ExtentWidth / e.ActualWidth, capabilities.PageImageableArea.ExtentHeight /
e.ActualHeight);
//Transform the Visual to scale
e.LayoutTransform = new ScaleTransform(scale, scale);
//get the size of the printer page
System.Windows.Size sz = new System.Windows.Size(capabilities.PageImageableArea.ExtentWidth, capabilities.PageImageableArea.ExtentHeight);
//update the layout of the visual to the printer page size.
e.Measure(sz);
e.Arrange(new System.Windows.Rect(new System.Windows.Point(capabilities.PageImageableArea.OriginWidth, capabilities.PageImageableArea.OriginHeight), sz));
//now print the visual to printer to fit on the one page.
pd.PrintVisual(v, "My Print");
//apply the original transform.
e.LayoutTransform = originalScale;
}
}
Note that I use the method Print to scale the Window, so it will fit a ISOA4 format. I also set my window to a fixed width and height before the print and reset it afterwards.

Adding Page Numbers

I haven't really written anything outside of Powershell in a long time, and I know this is ugly, but I can't seem to figure out why my new PDF is not adding the page numbers. I pulled the example from this itext kb.
I tried to make this basic app so people in the office could add the page numbers to PDF's. Here's what I have so far. It will create the new PDF (duplicate of the original), but it's not adding the page numbers.
Basically they use button1 to find their PDF via the Windows File Explorer dialog. It just stores the filename in a textbox. The second button is the "save" and should take the src file and make a copy of the src with only adding the page number at the bottom of the file (or anywhere at this point).
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using iText.Kernel.Pdf;
using iText.Layout;
using iText.Layout.Element;
using iText.Layout.Properties;
namespace PDFManipulation
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int size = -1;
DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{
string file = openFileDialog1.FileName;
try
{
string text = File.ReadAllText(file);
size = text.Length;
textBox1.Text = file;
}
catch (System.IO.IOException)
{
}
}
Console.WriteLine(size); // <-- Shows file size in debugging mode.
Console.WriteLine(result); // <-- For debugging use.
}
private void button2_Click(object sender, EventArgs e)
{
Stream myStream;
//SaveFileDialog saveFileDialog1 = new SaveFileDialog();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
// Code to write the stream goes here.
myStream.Close();
string SRC = textBox1.Text;
string DEST = saveFileDialog1.FileName;
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
Document doc = new Document(pdfDoc);
int numberOfPages = pdfDoc.GetNumberOfPages();
for (int i = 1; i <= numberOfPages; i++)
{
// Write aligned text to the specified by parameters point
doc.ShowTextAligned(new Paragraph("page " + i + " of " + numberOfPages),559, 806, i, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
}
doc.Close();
}
}
MessageBox.Show("PDF Page Numbering Added!", "Pages Added",MessageBoxButtons.OK);
Application.Exit();
}
}
}
I'm a dumb dumb. The x,y coordinates were off as the value 812 for the height is off the page.

How to set number of copies from textbox to print To ptinter

I'm using this code for printing to printer to print reportviewer without showing dialoge,
but I can't set the number of copies to print
I need a textbox or numericUpDown or anything to put the number of copies
using System;
using System.IO;
using System.Text;
using System.Globalization;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using Microsoft.Reporting.WinForms;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace NewLabelPrinter
{
/// <summary>
/// The ReportPrintDocument will print all of the pages of a ServerReport or LocalReport.
/// The pages are rendered when the print document is constructed. Once constructed,
/// call Print() on this class to begin printing.
/// </summary>
class AutoPrintCls : PrintDocument
{
private PageSettings m_pageSettings;
private int m_currentPage;
private List<Stream> m_pages = new List<Stream>();
public AutoPrintCls(ServerReport serverReport)
: this((Report)serverReport)
{
RenderAllServerReportPages(serverReport);
}
public AutoPrintCls(LocalReport localReport)
: this((Report)localReport)
{
RenderAllLocalReportPages(localReport);
}
private AutoPrintCls(Report report)
{
// Set the page settings to the default defined in the report
ReportPageSettings reportPageSettings = report.GetDefaultPageSettings();
// The page settings object will use the default printer unless
// PageSettings.PrinterSettings is changed. This assumes there
// is a default printer.
m_pageSettings = new PageSettings();
m_pageSettings.PaperSize = reportPageSettings.PaperSize;
m_pageSettings.Margins = reportPageSettings.Margins;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (disposing)
{
foreach (Stream s in m_pages)
{
s.Dispose();
}
m_pages.Clear();
}
}
protected override void OnBeginPrint(PrintEventArgs e)
{
base.OnBeginPrint(e);
m_currentPage = 0;
}
protected override void OnPrintPage(PrintPageEventArgs e)
{
base.OnPrintPage(e);
Stream pageToPrint = m_pages[m_currentPage];
pageToPrint.Position = 0;
// Load each page into a Metafile to draw it.
using (Metafile pageMetaFile = new Metafile(pageToPrint))
{
Rectangle adjustedRect = new Rectangle(
e.PageBounds.Left - (int)e.PageSettings.HardMarginX,
e.PageBounds.Top - (int)e.PageSettings.HardMarginY,
e.PageBounds.Width,
e.PageBounds.Height);
// Draw a white background for the report
e.Graphics.FillRectangle(Brushes.White, adjustedRect);
// Draw the report content
e.Graphics.DrawImage(pageMetaFile, adjustedRect);
// Prepare for next page. Make sure we haven't hit the end.
m_currentPage++;
e.HasMorePages = m_currentPage < m_pages.Count;
}
}
protected override void OnQueryPageSettings(QueryPageSettingsEventArgs e)
{
e.PageSettings = (PageSettings)m_pageSettings.Clone();
}
private void RenderAllServerReportPages(ServerReport serverReport)
{
try
{
string deviceInfo = CreateEMFDeviceInfo();
// Generating Image renderer pages one at a time can be expensive. In order
// to generate page 2, the server would need to recalculate page 1 and throw it
// away. Using PersistStreams causes the server to generate all the pages in
// the background but return as soon as page 1 is complete.
NameValueCollection firstPageParameters = new NameValueCollection();
firstPageParameters.Add("rs:PersistStreams", "True");
// GetNextStream returns the next page in the sequence from the background process
// started by PersistStreams.
NameValueCollection nonFirstPageParameters = new NameValueCollection();
nonFirstPageParameters.Add("rs:GetNextStream", "True");
string mimeType;
string fileExtension;
Stream pageStream = serverReport.Render("IMAGE", deviceInfo, firstPageParameters, out mimeType, out fileExtension);
// The server returns an empty stream when moving beyond the last page.
while (pageStream.Length > 0)
{
m_pages.Add(pageStream);
pageStream = serverReport.Render("IMAGE", deviceInfo, nonFirstPageParameters, out mimeType, out fileExtension);
}
}
catch (Exception e)
{
MessageBox.Show("possible missing information :: " + e);
}
}
private void RenderAllLocalReportPages(LocalReport localReport)
{
try
{
string deviceInfo = CreateEMFDeviceInfo();
Warning[] warnings;
localReport.Render("IMAGE", deviceInfo, LocalReportCreateStreamCallback, out warnings);
}
catch (Exception e)
{
MessageBox.Show("error :: " + e);
}
}
private Stream LocalReportCreateStreamCallback(
string name,
string extension,
Encoding encoding,
string mimeType,
bool willSeek)
{
MemoryStream stream = new MemoryStream();
m_pages.Add(stream);
return stream;
}
private string CreateEMFDeviceInfo()
{
PaperSize paperSize = m_pageSettings.PaperSize;
Margins margins = m_pageSettings.Margins;
// The device info string defines the page range to print as well as the size of the page.
// A start and end page of 0 means generate all pages.
return string.Format(
CultureInfo.InvariantCulture,
"<DeviceInfo><OutputFormat>emf</OutputFormat><StartPage>0</StartPage><EndPage>0</EndPage><MarginTop>{0}</MarginTop><MarginLeft>{1}</MarginLeft><MarginRight>{2}</MarginRight><MarginBottom>{3}</MarginBottom><PageHeight>{4}</PageHeight><PageWidth>{5}</PageWidth></DeviceInfo>",
ToInches(margins.Top),
ToInches(margins.Left),
ToInches(margins.Right),
ToInches(margins.Bottom),
ToInches(paperSize.Height),
ToInches(paperSize.Width));
}
private static string ToInches(int hundrethsOfInch)
{
double inches = hundrethsOfInch / 100.0;
return inches.ToString(CultureInfo.InvariantCulture) + "in";
}
}
}
this is the Print button
private void btnPrint_Click_1(object sender, EventArgs e)
{
AutoPrintCls autoprintme = new AutoPrintCls(reportViewer1.LocalReport);
autoprintme.Print();
}
Your btnPrint_Click_1 event can refer to the following code:
private void btnPrint_Click_1(object sender, EventArgs e)
{
AutoPrintCls autoprintme = new AutoPrintCls(reportViewer1.LocalReport);
autoprintme.PrinterSettings.Copies = Convert.ToInt16(numericUpDown1.Value);
autoprintme.Print();
}

Symbol font is rasterized when printing from .NET application

I am creating a Postscript file from C# by printing to a Postscript printer set up to print to a file. I'm using the .NET PrintDocument class and Graphics.DrawString to render the text.
If I use a symbol or pictograph font such as Symbol or WingDings, the text is rasterized in the Postscript file. If I use e.g. Arial, then the text is not rasterized. My aim is to produce a Postscript file from my application where text using a symbol font is not rasterized.
If I print the same text using symbol font from e.g. notepad the text is not rasterized, so it doesn't at first glance appear to be a printer driver limitation.
What am I doing wrong/different that's causing the rasterization?
The printer's font substitution table is set to not substitute the font.
Symbol font is available on the printers I have tried.
Choosing another Postscript printer driver makes no difference.
Using the TextRenderer.DrawText produces the same result.
Printing using the Symbol font from other applications (Notepad, Word, etc) does not result in rasterized output.
#region Usings
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Windows.Forms;
#endregion
public class PrintingExample
{
private string _fontName;
private string _printerName;
private string _textToPrint;
//private bool endPrintFired = false;
// private bool keepGoing = true;
private Font printFont;
public PrintingExample(string fontName, string printerName, string textToPrint)
{
_fontName = fontName;
_printerName = printerName;
_textToPrint = textToPrint;
}
// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
const float fontSize = 14.0f;
var fFamily = new FontFamily(_fontName);
printFont = new Font(fFamily, fontSize, FontStyle.Regular, GraphicsUnit.Point);
var leftMargin = ev.MarginBounds.Left;
var topMargin = ev.MarginBounds.Top;
ev.Graphics.DrawString(_textToPrint, printFont, Brushes.Black, new Point(leftMargin, topMargin), new StringFormat());
// Only printing one page
ev.HasMorePages = false;
}
// Print the file.
public void Printing()
{
try
{
var outputFile = string.Empty;
try
{
outputFile = Path.Combine(#"c:\temp\print\", Path.GetRandomFileName() + ".ps");
var pd = new PrintDocument {PrinterSettings = {
PrinterName = _printerName,
PrintToFile = true,
PrintFileName = outputFile}
};
pd.PrintPage += pd_PrintPage;
PrintController pc = new StandardPrintController();
PrintController printController = new PrintControllerWithStatusDialog(pc);
pd.PrintController = printController;
// Print the document.
pd.Print();
}
finally
{
// For debugging, loads in default associated application
if (File.Exists(outputFile))
Process.Start(outputFile);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
// This is the main entry point for the application.
public static void Main(string[] args)
{
const string fontName = "Symbol";
const string printerName = "<Choose an installed Postscript printer here>";
const string textToPrint = "1234567890\r\nabcdefghijklmnopqrstuvwxyz";
var p = new PrintingExample(fontName, printerName, textToPrint);
p.Printing();
}
}
This turned out to be a bug in Windows 2012R2, 10, Server 2016, and Server 2019 (server 2008R2 was unaffected, which led us to open an MS case). Only some fonts were affected. It was resolved in the March 2021 OS updates for Windows 10, Server 2016 and Server 2019

How to change the permission level, download file

I try to create a web browser. Currently I try to realize a function that if the user wants to download some file an additional window is shown with a list of already downloaded files. If the file has already been loaded, a message is shown (just an idea).
So far, I get a link to the file location in the main form and send it to the other form:
DownLoadFile dlf = new DownLoadFile();
...
WebBrowser wb = new WebBrowser();
wb.Navigating += new WebBrowserNavigatingEventHandler(wb_Navigating);
...
private void wb_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
...
if (e.Url.ToString().EndsWith(".mp3"))
{
dlf.DownloadPath = e.Url;
dlf.Show();
}
}
In the new form I try to use this link for file downloading:
public Uri DownloadPath { get; set; }
...
private void DownLoadFile_Load(object sender, EventArgs e)
{
string filePath = null;
//get FileName from URL
string[] ArrayForName;
ArrayForName = DownloadPath.ToString().Split('/');
saveFileDialogFile.FileName =
ArrayForName[ArrayForName.Length-1].Replace("%"," ").Trim();
if (saveFileDialogFile.ShowDialog() == DialogResult.OK)
{
WebClient client = new WebClient();
//get Url
Uri url = new Uri(DownloadPath.ToString());
//get place where want to save with default name
filePath = saveFileDialogFile.FileName;
//event for result
client.DownloadFileCompleted +=
new System.ComponentModel.AsyncCompletedEventHandler (client_DownloadFileCompleted);
//download
client.DownloadFileAsync(url, filePath);
}
}
void client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
MessageBox.Show("Compleated");
}
My questions are:
Regarding if (e.Url.ToString().EndsWith(".mp3")) - How can i
change this for knowing not only when the user tries to download mp3 file,
but all types of files - maybe there is a better way
If i want to download a file using some link directly, I get the message "Currently you have not required permission for that" - How can I
change permission level for my web browser
If i finally get a link to the file and start to download it, as result just name of file (size of file 0 kb) - where i'm wrong.
my solution (maybe not the best one)
create event for webBrowser
wb.Navigating += new WebBrowserNavigatingEventHandler(wb_Navigating);
and in this event use next
if (GetWorkingWebBrowser().StatusText != null)
{
try
{
WebRequest request = WebRequest.Create(GetWorkingWebBrowser().StatusText);
request.Method = "HEAD";
using (WebResponse response = request.GetResponse())
{
if (response.ContentLength > 0 &&
!response.ContentType.ToString().ToLower().Contains("text/html"))
{
dlf.DownloadPath = e.Url; //move url to my form for dwnload
dlf.Show(); //show form
}
}
}
catch (UriFormatException)
{
}
catch (WebException)
{
}
}
GetWorkingWebBrowser() - method that return current active webBrowser on tab, meas webBrowser

Categories

Resources