C# merge PDF solution without using iTextSharp - c#

I have some PDFs which I would like to fill out automatically using C#. I know about iTextSharp, but I am unsure about licensing issues for business use, and would rather find a different solution.
Basically, I would like to open a PDF, specify the field (or give coordinates for a textbox) and be able to insert text (& possibly small images?). Then I need to merge and save the pdf.
Any suggestions for a good way to accomplish this where I don't have to purchase / worry about licenses?

Not sure what the advantages of iTextSharp are, but I found a solution that is working perfectly so far using PDFSharp! As documentation seems to be a little on the light side for PDFSharp, I also found this thread to be very helpful...
If this helps anybody, here is my sample program:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using PdfSharp.Fonts;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Pdf.AcroForms;
namespace PDFSharpTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void goButton_Click(object sender, EventArgs e)
{
//TestPDF(); //uncomment this to find out whether acroform will work correctly
//open file
PdfDocument pdf = PdfReader.Open(#"YOURFILEPATHandNAME", PdfDocumentOpenMode.Modify);
//fix some odd setting where filled fields don't always show SetupPDF(pdf);
//find and fill fields
PdfTextField txtEmployerName = (PdfTextField)(pdf.AcroForm.Fields["txtEmployerName"]);
txtEmployerName.Value = new PdfString("My Name");
PdfTextField txtEmployeeTitle = (PdfTextField)(pdf.AcroForm.Fields["txtEmployeeTitle"]);
txtEmployeeTitle.Value = new PdfString("Workin'");
PdfCheckBoxField chxAttached = (PdfCheckBoxField)(pdf.AcroForm.Fields["chxAttached"]);
chxAttached.Checked = true;
//save file
pdf.Save(#"NEWFILEPATHandNAMEHERE");
}
private void SetupPDF(PdfDocument pdf)
{
if (pdf.AcroForm.Elements.ContainsKey("/NeedAppearances") == false)
{
pdf.AcroForm.Elements.Add("/NeedAppearances", new PdfSharp.Pdf.PdfBoolean(true));
}
else
{
pdf.AcroForm.Elements["/NeedAppearances"] = new PdfSharp.Pdf.PdfBoolean(true);
}
}
private void PDFTest()
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
PdfDocument _document = null;
try { _document = PdfReader.Open(ofd.FileName, PdfDocumentOpenMode.Modify); }
catch (Exception ex)
{
MessageBox.Show(ex.Message, "FATAL"); //do any cleanup and return
return;
}
if (_document != null)
{
if (_document.AcroForm != null)
{
MessageBox.Show("Acroform is object", "SUCCEEDED");
//pass acroform to some function for processing
_document.Save(#"C:\temp\newcopy.pdf");
}
else
{
MessageBox.Show("Acroform is null", "FAILED");
}
}
else
{
MessageBox.Show("Unknown error opening document", "FAILED");
}
}
}
}
}

This is a free and open source solution: SharpPDF
But in all honesty iTextSharp is probably the best thing you'll find.

You might try Docotic.Pdf library for your task. The library is not free but probably there is nothing to worry about (licensing wise).
Here are samples available online that might help you:
Fill existing form
Find control by name
Other samples from Forms and Annotations group could also prove to be useful in your case.
Disclaimer: I work for the vendor of the library.

Related

loading loose XAML drawing securely

A C# Windows application would like to load vector drawings that are stored in loose XAML files without allowing arbitrary code execution.
I am already loading such drawings from resources in linked assemblies over which I have control. However, I would like to also support loading loose XAML files. I imagine you can use XAML access control to limit the objects that can be instantiated in such XAML? Ideally, I would limit the loader to instantiating only the drawing primitives that are in the files we know about. It's ok that it would reject a file that has new drawing primitives in it that we have not whitelisted.
Is this a standard thing already supported by an API? Because I could not find it. Otherwise, does anyone have an example or beginnings of an example? This is for a free open source project and any help getting started would probably cut down the research I need to do by a lot.
The following seems to do a pretty decent job of white listing specific types in a XAML load:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Windows.Controls;
using System.Windows.Media;
using System.Xaml;
using System.Xml;
namespace TestXamlLoading
{
internal class SchemaContext : XamlSchemaContext
{
// map from XAML element name to required namespace (currently always the same)
private static readonly Dictionary<string, string> AllowedTypes = new Dictionary<string, string>();
static SchemaContext()
{
// questionable: <Image> is used in some drawing XAML, should review it
foreach (string name in new[]
{
"Canvas", "Compound", "Ellipse", "GradientStop", "GradientStopCollection", "Group", "Line",
"LinearGradientBrush", "MatrixTransform", "Path", "PathGeometry", "Polygon",
"RadialGradientBrush", "Rectangle", "RotateTransform", "ScaleTransform", "SkewTransform", "TextBlock",
"TransformGroup", "TranslateTransform"
})
{
AllowedTypes[name] = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
}
}
public SchemaContext(IEnumerable<Assembly> referenceAssemblies, XamlSchemaContextSettings settings) : base(
referenceAssemblies, settings)
{
// no code
}
protected override XamlType GetXamlType(string xamlNamespace, string name, params XamlType[] typeArguments)
{
if (!AllowedTypes.TryGetValue(name, out string requiredNamespace) || xamlNamespace != requiredNamespace)
{
throw new Exception($"disallowed instantiation of '{xamlNamespace}' '{name}' from XAML");
}
return base.GetXamlType(xamlNamespace, name, typeArguments);
}
}
internal class Program
{
[STAThreadAttribute]
private static void Main(string[] args)
{
bool shouldFail = TestLoad("..\\..\\..\\badfile.xaml");
Debug.Assert(!shouldFail);
bool shouldSucceed = TestLoad("..\\..\\..\\goodfile.xaml");
Debug.Assert(shouldSucceed);
}
private static bool TestLoad(string path)
{
Stream inputStream = new FileStream(path, FileMode.Open);
XmlReader xmlReader = new XmlTextReader(inputStream);
Assembly[] referenceAssemblies =
{
// these are two separate assemblies which contain all the types we allow
Assembly.GetAssembly(typeof(Canvas)),
Assembly.GetAssembly(typeof(TransformGroup))
};
XamlSchemaContextSettings settings = new XamlSchemaContextSettings();
XamlSchemaContext schemaContext = new SchemaContext(referenceAssemblies, settings);
try
{
XamlReader reader = new XamlXmlReader(xmlReader, schemaContext);
Canvas canvas = (Canvas) System.Windows.Markup.XamlReader.Load(reader);
}
catch (Exception e)
{
Debug.WriteLine(e);
return false;
}
return true;
}
}
}

Print a ReportViewer Without Preview

I'm using Visual Studio 2010 C# Windows Forms Application + MySql
I have a Report Viewer that is working 100% . The reportviewer is filled with data of my database, it shows up I click on the button to print and it prints... BUT, my client does not want to click on this button, he wants to print automatically. When I Call the ReportViewer it print by itself without need to click on a button to do that. Could anyone tell me how I do that ?
I tryed reportviewer1.print and the PrintDocument from the toolbox. But I do not know how to use these correctly.
Thanks the attention !
I had just the same issue this is the code i use and works like a charm!
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 class has the set up perfect for what you need then all you need to do is:
private void AutoPrint()
{
AutoPrintCls autoprintme = new AutoPrintCls(reportViewer1.LocalReport);
autoprintme.Print();
}
and hey presto it prints. Just attach this to A method in your code(maybe after the report Loads.) and your setup nicely!
option: (not tested)
As spotted this prints out to the default printer, to change the printer you could do the following:
if (printDialog.ShowDialog() == DialogResult.OK)
{
m_pageSettings .PrinterSettings.PrinterName = printDialog.PrinterSettings.PrinterName;
}
not tested though as i no longer have any source code to test this out
If my Crystal Report answer doesn't work for you, you can also try this page. Again, I haven't tested it, and can't be sure that it works, but it looks like an entirely different approach which might work. If not, then I'm not going to be any help, unfortunately.
This is how we do it with Crystal Reports.
ReportDocument rd = new ReportDocument();
// Insert code to run the report here
// This gets the user's default printer to print to.
PrintDialog prt = new PrintDialog();
rd.PrintOptions.PrinterName = prt.PrinterSettings.PrinterName;
// This does the printing.
rd.PrintToPrinter(copies, true, 1, 1000);
I think the equivalent to PrintOptions.PrinterName for you would be ReportViewer.PrinterSettings, but I suspect what you really need is the equivalent to PrintToPrinter(), which I don't see in my brief look.

Permanently locking a file

I am working on a file locker/unlocker application using C# on VS2010.
what i want is to lock a file with a password using my application and then unlock it any time.
In fact, I used the following code to lock the file, but the file is being locked only while the application is still running; when I close the application, the file is unlocked.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Configuration;
using System.Windows.Forms;
namespace LockFile
{
public enum LockStatus
{
Unlocked,
Locked
}
public class LockFilePresenter
{
private ILockFileView view;
private string file2Lock = string.Empty;
private FileStream fileLockStream = null;
public LockFilePresenter(ILockFileView view)
{
this.view = view;
}
internal void LockFile()
{
if (string.IsNullOrEmpty(file2Lock) || !File.Exists(file2Lock))
{
view.ShowMessage("Please select a path to lock.");
return;
}
if (fileLockStream != null)
{
view.ShowMessage("The path is already locked.");
return;
}
try
{
fileLockStream = File.Open(file2Lock, FileMode.Open);
fileLockStream.Lock(0, fileLockStream.Length);
view.SetStatus(LockStatus.Locked);
}
catch (Exception ex)
{
fileLockStream = null;
view.SetStatus(LockStatus.Unlocked);
view.ShowMessage(string.Format("An error occurred locking the path.\r\n\r\n{0}", ex.Message));
}
}
internal void UnlockFile()
{
if (fileLockStream == null)
{
view.ShowMessage("No path is currently locked.");
return;
}
try
{
using (fileLockStream)
fileLockStream.Unlock(0, fileLockStream.Length);
}
catch (Exception ex)
{
view.ShowMessage(string.Format("An error occurred unlocking the path.\r\n\r\n{0}", ex.Message));
}
finally
{
fileLockStream = null;
}
view.SetStatus(LockStatus.Unlocked);
}
internal void SetFile(string path)
{
if (ValidateFile(path))
{
if (fileLockStream != null)
UnlockFile();
view.SetStatus(LockStatus.Unlocked);
file2Lock = path;
view.SetFile(path);
}
}
internal bool ValidateFile(string path)
{
bool exists = File.Exists(path);
if (!exists)
view.ShowMessage("File does not exist.");
return exists;
}
}
}
and
using System;
using System.Collections.Generic;
using System.Text;
namespace LockFile
{
public interface ILockFileView
{
void ShowMessage(string p);
void SetStatus(LockStatus lockStatus);
void SetFile(string path);
}
}
As I said previously, the application works fine during the running time, but when I close it, the locked file will be unlocked.
If anybody has any idea about how to do it, I would be grateful.
A Lock on a FileStream just means that your process has exclusive access to the file while it's active; it has nothing to do with password protecting a file.
It sounds like what you want is to encrypt a file with a password. The file class provides Encrypt/Decrypt based on the current user, or, if you want it based on your own custom password there's a sample of using some of the classes in the System.Security.Cryptography namespace to encrypt a file with a password here (instead of hard coding you would take it as input presumably) http://www.codeproject.com/Articles/26085/File-Encryption-and-Decryption-in-C
Keep in mind, doing security right is hard.
You're using the FileStream.Lock() method to lock a specific file so that only the process running the FileStream can use it.
http://msdn.microsoft.com/en-us/library/system.io.filestream.lock.aspx
This is a mechanism designed to prevent other processes writing to a file that you are reading/writing to, and you can see this method in use with applications like Microsoft Excel.
When you close your application, the process is no longer running, and the lock on the file is disengaged.
If your goal is to prevent other applications from reading the file, you have some limited options:
Encrypt the file. This will mean that an application cannot read usable information from the file without the decryption key, but there is the potential for an application to open and change the encrypted file.
Save the file to a read-only media like a CD/DVD, or to removable storage that you then unplug and carry with you.
If you want to prevent other applications from modifying the file, you might look at the ReadOnly flags that Windows offers: http://msdn.microsoft.com/en-us/library/system.io.fileinfo.isreadonly.aspx
Note that these will still be insecure, as readonly flags can be ignored.
Something you need to think about is your reasoning for why you want to be restricting access to a file - that will help determine the best strategy for restricting access.
If all you need to do is make sure nothing else can read or modify the file while you've got your application locking it, the below should do the job.
If you need anything more, look into proper file encryption techniques.
Note that if you close the application the lock will no longer be in effect.
System.IO.FileStream fileStream;
private void LockFile(string FilePath)
{
fileStream = System.IO.File.Open(FilePath, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite, System.IO.FileShare.None);
//using System.IO.FileShare.None in the above line should be sufficient, but just to go the extra mile...
fileStream.Lock(0, fileStream.Length);
}
private void UnlockFile()
{
if (fileStream != null)
{
try { fileStream.Unlock(0, fileStream.Length); }
finally { fileStream.Dispose(); }
}
}

Programming an autoupdater / launcher application in c# that works with any program

I am making a program updater / launcher that can be used for any program.
I have a config file on the client and a config file on a http server. I get version numbers from both of them and compare them and if they are are not = then update the client.
I have everything working except for when the update starts. What I need is say if someone downloaded my application and do not use if for say a month and in between that time I have 5 or so updates.
The problem is how to I get my program to download the first update , install it and then download the next update untill they have all been downloaded?
I am new to programming and this is the only kind of app I can think of to work on to learn.
Thanks
My settings.conf on http server XML File.
<Table>
<Product>
<Product_id>1</Product_id>
<Product_name>Infected</Product_name>
<Product_version>1.0.0.1</Product_version>
<Product_Url>http://localhost/update/v1.0.0.1.exe</Product_Url>
<Product_id>2</Product_id>
<Product_name>Infected</Product_name>
<Product_version>1.0.0.2</Product_version>
<Product_Url>http://localhost/update/v1.0.0.2.exe</Product_Url>
<Product_id>3</Product_id>
<Product_name>Infected</Product_name>
<Product_version>1.0.0.3</Product_version>
<Product_Url>http://localhost/update/v1.0.0.3.exe</Product_Url>
<Product_id>4</Product_id>
<Product_name>Infected</Product_name>
<Product_version>1.0.0.4</Product_version>
<Product_Url>http://localhost/update/v1.0.0.4.exe</Product_Url>
<Product_id>5</Product_id>
<Product_name>Infected</Product_name>
<Product_version>1.0.0.5</Product_version>
<Product_Url>http://localhost/update/v1.0.0.5.exe</Product_Url>
</Product>
</Table>
My Client Config XML file.
<Table>
<Product>
<Product_id>1</Product_id>
<Product_name>Infected</Product_name>
<Product_version>1.0.0.0</Product_version>
<Product_Url>http://localhost/update/v1.0.0.1.exe</Product_Url>
</Product>
</Table>
My C# Form.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Net;
using System.Diagnostics;
using System.Runtime.Remoting;
namespace Launcher
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string localversion { get; set; }
public string remoteversion { get; set; }
public string UpdateURL { get; set; }
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.kceoc.com/");
webBrowser2.Navigate("http://www.kceoc.com/");
button1.Enabled = false; // Disable the launch button untill all updates are completed.
GetLocalXMLFile(); //Run first xml function to start everything off.
}
private void GetLocalXMLFile()
{
try //Start error checking.
{
using (XmlTextReader localxml = new XmlTextReader("settings.conf")) //Load xml file in same folder as launcher.exe
{
while (localxml.Read()) // Start reading the settings.conf file
{
switch (localxml.NodeType) //Get the Node that we will use.
{
case XmlNodeType.Text:
label1.Text = localxml.Value; //Change the text of label1 to value of Node.
string localversion = localxml.Value; // Store Node Value in string localversion for latter use.
GetRemoteXMLFile(localversion, remoteversion); //Everything went ok and got a value from Node so pass this all to our next function witch is get remote xml.
break;
}
}
}
}
catch (FileNotFoundException)
{
label1.Text = "Local Config not found. Reinstall the application"; // Catch error incase file is not there.
}
}
private void GetRemoteXMLFile(string localversion, string remoteversion)
{
try //Start error checking
{
using (XmlTextReader remotexml = new XmlTextReader("http://localhost/update/settings.conf")) //Load up remote xml on web server
{
while (remotexml.Read()) //Start reading xml file from server.
{
switch (remotexml.NodeType)
{
case XmlNodeType.Text:
label2.Text = remotexml.Value; // Change value of label2 to remote xml node value
remoteversion = remotexml.Value; // Set the remoteversion string to remotexml.value
CompareXMLFileVersions(localversion, remoteversion); // Everything went ok so send localversion string and remoteversion string to compare function.
break;
}
}
}
}
catch (FileNotFoundException)
{
label1.Text = "Remote config not found. Maby website id down?"; // Catch error incase file is not there.
}
}
private void CompareXMLFileVersions(string localversion, string remoteversion)
{
label1.Text = localversion; // Just so we can see the value in the lables to konw if they have value or not.
label2.Text = remoteversion; // Just so we can see the value in the lables to konw if they have value or not.
if (localversion == remoteversion) // Comparing the values of localversion and remoteversion and if they have same value then
{ // change label3 to You have latest version.
label3.Text = "You have the latest version";
}
else
{
label3.Text = "There is a new version. Starting update process here"; // If localversion and remoteversion are diffrent then let user know the files are out of date and start the updating process..
GetListOfUpdates(remoteversion); // Starting the updating process function..
}
}
private void GetListOfUpdates(string remoteversion)
{
//WebClient webClient = new WebClient();
//webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
//webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
//webClient.DownloadFileAsync(new Uri(remoteversion), #"v1.0.0.1.exe");
string url = "http://localhost/update/v1.0.0.1.exe";
WebClient downloader = new WebClient();
downloader.DownloadFileCompleted += new AsyncCompletedEventHandler(downloader_DownloadFileCompleted);
downloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloader_DownloadProgressChanged);
downloader.DownloadFileAsync(new Uri(url), "temp.exe");
}
void downloader_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
label1.Text = e.BytesReceived + " " + e.ProgressPercentage;
}
void downloader_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
//if (e.Error != null)
// MessageBox.Show(e.Error.Message);
//else
// MessageBox.Show("Completed!!!");
}
}
}
Thanks
Welcome to SO!
I am new to programming and this is the only kind of app I can think of to work on to learn. Thanks
Depending on how new you are, I'd really recommend you start with something a little easier. Otherwise, the first thing I'd recommend you do is to actually draw a flowchart. Your logic looks a little off, and it looks as if you're huffing it trying to design this system as you write it, which is something you never want to be doing.
There are a lot of solutions for this that provide a better, more reliable systems then anything you can make yourself, but I can understand the educational value of this sort of project. I made my own 'auto-update/launcher' recently for just that reason, and it works reasonably well, albeit on a free webserver with myself an some friends as the only users.
Here's the flowchart I made for it:
Large:http://i.imgur.com/qS1U8.png
This is actually the second iteration of my little project, with the first being less then overwhelming and somewhat disastrous in uncommon circumstances, but it's a good learning experiance. This one also has goofy command files that I can define things like showing messages to the user during an update, which is nice.
If you don't mind looking at terrible and messy code, you can look through the code repo here, although it's not documented and a few part's aren't actually used but haven't been removed from source control. An example application that uses it is here (source, also messy).
Sorry for what looks like a shameless self-plug, but I can't really answer your question directly and hope that you might be able to make use of some of this as an indication of how you should go about doing this, since it's actually a pretty fun project.

C#: execute a function stored in a string variable

Is it possible to write a simple and fast function in C# that will execute arbitrary methods from a string? For example, if I set MyString="MessageBox.Show("Some Message")" and then call ExecuteString(MyString), a message box would pop up with "Some Message" in it.
(I've probably made some sort of error in the above code. I don't yet know C#; I'm trying to evaluate whether it would be appropriate for a specific project.)
You should be able to use this and wrap the code required to run a string into a function.
Essentially what you're doing is wrapping the small bit of C# code in a Program.Mainstyle function, referencing some assemblies for basic functionality (maybe including your own assembly) then run the compiled program in memory.
It's likely a bit of more overhead than you need to simply run one or two lines of code mind you.
http://support.microsoft.com/kb/304655
what you appear to be looking for is CS-Script
Alas, C# is not a dynamic language in that way. You can't really do this easily, and if it's really something you need to do, consider using a .Net language more in line with your needs, like IronPython or IronRuby.
Your best available alternative is to use the CodeDom namespace, as this truly convoluted and heinous example from this forum thread shows:
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using System.Windows.Forms;
namespace TestApp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SampleLib.SampleType test = new SampleLib.SampleType();
private void button1_Click(object sender, EventArgs e)
{
// Dynamically build and call the method
label1.Text = test.MyText;
}
private void button2_Click(object sender, EventArgs e)
{
StringBuilder DynamicCode = new StringBuilder();
DynamicCode.Append("namespace TestDynamic");
DynamicCode.Append("{");
DynamicCode.Append("public class DynamicCode");
DynamicCode.Append("{");
DynamicCode.Append("public static void EditText(SampleLib.SampleType t)");
DynamicCode.Append("{");
DynamicCode.Append("t.MyText = \"Goodbye!\";");
DynamicCode.Append("}");
DynamicCode.Append("}");
DynamicCode.Append("}");
string CodeString = DynamicCode.ToString();
System.IO.FileInfo fi = new System.IO.FileInfo(Application.ExecutablePath);
CodeDomProvider provider = CodeDomProvider.CreateProvider("C#");
CompilerParameters CompileParams = new CompilerParameters(new string[] { fi.DirectoryName + "\\SampleLib.dll" },
fi.DirectoryName + "\\Dynamic.dll");
CompileParams.MainClass = "DynamicCode";
CompileParams.GenerateExecutable = false;
//CompileParams.GenerateInMemory = true;
CompilerResults r = provider.CompileAssemblyFromSource(CompileParams, new string[] {CodeString});
foreach (CompilerError er in r.Errors)
{
Console.WriteLine(er.ErrorText);
}
}
private void button3_Click(object sender, EventArgs e)
{
// Dynamically call assembly
System.IO.FileInfo fi = new System.IO.FileInfo(Application.ExecutablePath);
Assembly dynAsm = Assembly.LoadFile(fi.DirectoryName + "\\Dynamic.dll");
if (dynAsm != null)
{
object o = dynAsm.CreateInstance("TestDynamic.DynamicCode", true);
Type t = dynAsm.GetType("TestDynamic.DynamicCode");
t.GetMethod("EditText").Invoke(o, new object[]{test});
}
}
}
}

Categories