Showing posts with label Sample Code. Show all posts
Showing posts with label Sample Code. Show all posts

Tuesday, April 7, 2009

Calling Javascript functino from your code behind in ASP.Net

Here is the code by which you can call a javascript function within any where of you C# code.



Page.ClientScript.RegisterStartupScript(Page.GetType(), "YourPrompt",
"<script>alert('Hello');</script>");



Hope it works.

Thanks
/Zaq

Wednesday, March 4, 2009

Zip/Unzip using C#

I have spent few hours over internet to find out a way to create zip file using .net 2.0, C#. My intention was to avoid any third party library or application. I started with System.IO.Compression, but I did not find any resource about creating a zip from a directory with files and subdirectory as well. I got few .net library and I felt few of those are quite familier and using wiedly. But as I have some restriction i can not use those. Finally I got a solutoin to create and extract zip file using Shell32.dll. Here is two functions I wrote.

private static void ZipFile(string sourceFolder, string destinationFile)
{
byte[] emptyzip = new byte[] { 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

FileStream fs = File.Create(destinationFile);
fs.Write(emptyzip, 0, emptyzip.Length);
fs.Flush();
fs.Close();
fs = null;

//Copy a folder and its contents into the newly created zip file
Shell32.ShellClass sc = new Shell32.ShellClass();
Shell32.Folder SrcFlder = sc.NameSpace(sourceFolder);
Shell32.Folder DestFlder = sc.NameSpace(destinationFile);
Shell32.FolderItems items = SrcFlder.Items();
DestFlder.CopyHere(items, 20);
}


private static string UnzipFile(string inputFileName, string destinationPath)
{
if (!Directory.Exists(destinationPath))
Directory.CreateDirectory(destinationPath);

Shell shell = new ShellClass();
Folder sourceFolder = shell.NameSpace(inputFileName);
Folder destinationFolder = shell.NameSpace(destinationPath);
string outputFileName = sourceFolder.Items().Item(0).Name;
destinationFolder.CopyHere(sourceFolder.Items(), "");
return outputFileName;
}


Here is the code to call those function.

ZipFile(@"D:\SourceFolder", @"D:\MyZip.zip");

UnzipFile(@"D:\MyZip.zip", @"D:\ExtractFolder");

Hope this may help you. Thanks.

Tuesday, September 16, 2008

Execute Java jar file from .Net

Step1: Creating a Java Class

package ZAQ;

import javax.swing.JOptionPane;
public class Hello {
public static void main (String [] args) {
JOptionPane.showMessageDialog(null, "Hello World!");
}
}

Put this Class Inside ZAQ directory and set the class file name as Hello.java


Step2: Preparing of mainClass.txt file.
Write the following line in the file. Please remember to put a carriage return after the line.
Main-Class: ZAQ.Hello


Step3: Compile the Java Class and Build the jar file
Create class file for Hello.java and then build the jar file. Do the following commands
->Javac ZAQ/Hello.java
->jar cmf mainClass.txt ZAQ.jar ZAQ
->java –jar ZAQ.jar Hello
Now your jar is ready.


Step4: Create a batch file to be execute in .Net
Save a file with the following text:
java -jar D:\ZAQ.jar


Step5: .Net code is here.

int i = 0;
string sOut = ShellExec(@"d:\Comm.bat", "", "", out i);
Console.WriteLine(sOut);

And the function is here:

protected static String ShellExec(String cmd, String path, String parms, out int exit_code)
{
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(path + cmd, parms);
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;

System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);
string tool_output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
exit_code = p.ExitCode;
return tool_output;
}