Calling JavaScript

You can simply call JavaScript from C# but actually, C# is used to implement the server-side application whereas, JavaScript is used to implement the client-side application but it isn’t always. You can embed the local web page file to the C# application via WebBrowser Component. So, It should be able to call JavaScript via C#.

How to

1. Put the WebBrowser component into the application’s form.
*The WebBrowser compoenet is a default component. So, you just simply drag from the toolbox to the application’s form.

2. Use Navigate method to load the web page.

  1. webBrowser1.Navigate(@"C:\Users\Thada\documents\visual studio 2010\Projects\GoogleMapApi\GoogleMapApi\googlemap.html");

Suppose: webBrowser1 is the WebBrowser’s object.

3. Call InvokeScript method from the HtmlDocument object which can be accessed by WebBrowser’s attribute.

Get HtmlDocument
  1. HtmlDocument doc = webBrowser1.Document;

InvokeScript Method
  1. object InvokeScript(string scriptName, object[] args)

JavaScript
  1. <script type="text/javascript" language="JavaScript">
  2.     function initialize() {
  3.         var latlng = new google.maps.LatLng(-34.397, 150.644);
  4.         var myOptions = {
  5.             zoom: 8,
  6.             center: latlng,
  7.             mapTypeId: google.maps.MapTypeId.ROADMAP
  8.         };
  9.         var map = new google.maps.Map(document.getElementById("map_canvas"),
  10.     myOptions);
  11.     }
  12.  
  13.     function msgbox(text) {
  14.         alert(text);
  15.     }
  16.  
  17.     function getint() {
  18.         return 1.23;
  19.     }
  20.  
  21. </script>

C#
  1. doc.InvokeScript("initialize", null); //no argument

C#
  1. doc.InvokeScript("msgbox", new object[] { "To You" }); //With one argument

Note: Sometimes, you need some value return from calling JavaScript. The InvokeScript also returns object out but you have to know know what exact type of the object from JavaScript first (JavaScript is dynamic.) by using GetType method of the object to know the type is and then convert the object to that type.

  1. object a = doc.InvokeScript("getint", null);
  2. Console.WriteLine(a.GetType()); // print System.Int32
  3. //rewrite
  4. int a = (Int32)doc.InvokeScript("getint", null);

SevenZipSharp: Extractor (Asynchronous)

1. Import the SevenZip namespace in your namespace first.

  1. using SevenZip;

2. Create object from SevenZipExtractor with a path for the 7z file and the password for encrypted 7z, if the 7z file was encrypted.

  1. SevenZipExtractor se = new SevenZipExtractor(@"C:\Users\Thada\Desktop\Test7Zip\test.7z", "passwordhere");

PS. add @ sign in front of path string.

3. Use BeginExtractArchive method to compress files and encrypt with a password

  1. BeginCompressFilesEncrypted( Path of extracted files);

  1. se.BeginExtractArchive(@"C:\Users\Thada\Desktop\Test7Zip\Extracted\");

4. Done!! But also not completely. The method that we are using is asynchronous,

There are 5 events:

· Extracting

· ExtractionFinished << Focus on this

· FileExists

· FileExtractionFinished

· FileExtractionStarted

5. We have to embed the method for the event. So, start typing your object’s name and follow by dot with ExtractionFinished.

Note: try to use intellisense to help you coding. When you are typing, there will be a list box of names that are partial match of your typing. Then click tab or spacebar button, it will autocomplete for you.

6. Type operation += and then click tab twice. Visual Studio will complete the statement and create a method for you. You will get something like these.

  1. se.ExtractionFinished += new EventHandler<EventArgs>(se_ExtractionFinished);

  1. void se_ExtractionFinished(object sender, EventArgs e)
  2. {
  3.     Console.WriteLine("…Extraction Finished");
  4. }

7. Add any statement you want in the method. After the extraction finished, it will call this method automatically.

Reference:

Vadim, M. (2010, 8 27). SevenZipSharp Documentation. Retrieved 1 4, 2011, from http://sevenzipsharp.codeplex.com/releases/51254/download/145909

SevenZipSharp: Compressor (Asynchronous)

1.      Import the SevenZip namespace in your namespace first.

  1. using SevenZip;

2.      Create object from SevenZipCompressor with a path for the file output

  1. SevenZipCompressor sc = new SevenZipCompressor(@"C:\Users\Thada\Desktop\Test7Zip\");

PS. add @ sign in front of path string. The @-quoting will tell the compiler that the escape sequences are not processed.

PS. leave \ sign at the last character of string too.

3.      Use BeginCompressFilesEncrypted method to compress files and encrypt with a password

  1. BeginCompressFilesEncrypted( Output file name including path, password, either list of file path or array of file path);

  1. sc.BeginCompressFilesEncrypted(sc.TempFolderPath + "test.7z", "passwordhere", @"C:\Users\Thada\Desktop\Test7Zip\a.txt", @"C:\Users\Thada\Desktop\Test7Zip\b.txt", @"C:\Users\Thada\Desktop\Test7Zip\c.txt");

or

 

  1. String[] files = { @"C:\Users\Thada\Desktop\Test7Zip\a.txt", @"C:\Users\Thada\Desktop\Test7Zip\b.txt", @"C:\Users\Thada\Desktop\Test7Zip\c.txt" };
  2. sc.BeginCompressFilesEncrypted(sc.TempFolderPath + "test.7z", "passwordhere", @"C:\Users\Thada\Desktop\Test7Zip\a.txt", @"C:\Users\Thada\Desktop\Test7Zip\b.txt", @"C:\Users\Thada\Desktop\Test7Zip\c.txt");

4.      Done!! But not completely. The method that we are using is asynchronous which means you cannot simply tell that statements after the line of calling BeginCompressFilesEncrypted will work after the compression has done. So, you cannot insert any statement that has to be cooperating with the BeginCompressFilesEncrypted. Because of that, SevenZipSharp also provides events for the compressing operation to fix this situation.

There are 5 events:

  • Compressing
  • CompressionFinished << Focus on this
  • FileCompressionStarted
  • FilesFound
  • FileCompressionFinished

5.      We have to embed the method for the event. So, start typing your object’s name and follow by dot with CompressionFinished.

Note: try to use intellisense to help you coding. When you are typing, there will be a list box of names that are partial match of your typing. Then click tab or spacebar button, it will autocomplete for you.

6.      Type operation += and then click tab twice. Visual Studio will complete the statement and create a method for you. You will get something like these.

  1. sc.CompressionFinished += new EventHandler<EventArgs>(sc_CompressionFinished);

  1. void sc_CompressionFinished(object sender, EventArgs e)
  2. {
  3. }

7.       Add any statement you want in the method. After the compression finished, it will call this method automatically.

Reference:

Vadim, M. (2010, 8 27). SevenZipSharp Documentation. Retrieved 1 4, 2011, from http://sevenzipsharp.codeplex.com/releases/51254/download/145909

SevenZipSharp: Preparing

Requirement:

Prepare to use:

1.      Open Solution you have created

2.      Look at the Solution Explorer (usually on the right)

3.      Expand the solution, you will see References

 

4.      Right click on it and then choose Add Reference…

5.      After Add Reference window pops up, go to tab browse, go to SevenZipSharp.dll path, choose SevenZipSharp.dll file and then click OK.

 

6.      Then copy file 7z.dll to the project’s bin\debug folder

 

Now, you are ready to use 7z operation.

 

Reference:

Vadim, M. (2010, 8 27). SevenZipSharp Documentation. Retrieved 1 4, 2011, from http://sevenzipsharp.codeplex.com/releases/51254/download/145909