Friday, July 30, 2010

How to: List all Query string variables in C#

This is how you can do it in C#, Either of will work for getting Query string values

for (int i = 0; i < Request.QueryString.Count; i++)
{                 
    Response.Write(Request.QueryString.Keys[i].ToString()+ ":"+Request.QueryString.Keys[i].ToString()+"<br/>");
}
foreach (string key in Request.QueryString.Keys)
{
    Response.Write(key+ ":"+Request.QueryString[key]+"<br/>");
}
  

How to: List all Session variables?

This is how we can get list of all session variables using c#.

for (int i = 0; i < Session.Contents.Count; i++)
{
    Response.Write(Session.Keys[i] + " - " + Session[i] + "<br />");
}
foreach (string key in Session.Keys)
{
    Response.Write(key + " - " + Session[key] + "<br />");
}

Either of them can get you all session details.

Ionic Zip Utility : SaveProgress Event

Save() method in Ionic Zip, this method allows the application to explicitly specify the name of the zip file when saving. Use this when creating a new zip file, or when updating a zip archive.

The ZipFile instance is written to storage, typically a zip file in a filesystem, only when the caller calls Save. The Save operation writes the zip content to a temporary file, and then renames the temporary file to the desired name. If necessary, this method will delete a pre-existing file before the rename.

using (ZipFile zip = ZipFile.Read("Archive.zip"))
{
  zip.AddFile("test.jpg");
  zip.Save("UpdatedArchive.zip");
}

There is an Save Progress event handler which invoked when a Save() starts, before and after each entry has been written to the archive, when a Save() completes, and during other Save events.
Depending on the particular event, different properties on the SaveProgressEventArgs parameter are set.


The following  EventTypes describes under which this event handler is invoked  with the given EventType.
ZipProgressEventType.Saving_Started Fired when ZipFile.Save() begins.
ZipProgressEventType.Saving_BeforeSaveEntry Fired within ZipFile.Save(), just before writing data for each particular entry.
ZipProgressEventType.Saving_AfterSaveEntry Fired within ZipFile.Save(), just after having finished writing data for each particular entry.
ZipProgressEventType.Saving_Completed Fired when ZipFile.Save() has completed.
ZipProgressEventType.Saving_AfterSaveTempArchive Fired after the temporary file has been created. This happens only when saving to a disk file. This event will not be invoked when saving to a stream.
ZipProgressEventType.Saving_BeforeRenameTempArchive Fired just before renaming the temporary file to the permanent location. This happens only when saving to a disk file. This event will not be invoked when saving to a stream.
ZipProgressEventType.Saving_AfterRenameTempArchive Fired just after renaming the temporary file to the permanent location. This happens only when saving to a disk file. This event will not be invoked when saving to a stream.
ZipProgressEventType.Saving_AfterCompileSelfExtractor Fired after a self-extracting archive has finished compiling. This EventType is used only within SaveSelfExtractor().
ZipProgressEventType.Saving_BytesRead Set during the save of a particular entry, to update progress of the Save(). When this EventType is set, the BytesTransferred is the number of bytes that have been read from the source stream. The TotalBytesToTransfer is the number of bytes in the uncompressed file.


In example below to see how it can be used.

static bool justHadByteUpdate= false;
public static void SaveProgress(object sender, SaveProgressEventArgs e)
{
    if (e.EventType == ZipProgressEventType.Saving_Started)
        Console.WriteLine("Saving: {0}", e.ArchiveName);
    else if (e.EventType == ZipProgressEventType.Saving_Completed)
    {
        justHadByteUpdate= false;
        Console.WriteLine();
        Console.WriteLine("Done: {0}", e.ArchiveName);
    }
    else if (e.EventType == ZipProgressEventType.Saving_BeforeWriteEntry)
    {
        if (justHadByteUpdate)
            Console.WriteLine();
        Console.WriteLine("  Writing: {0} ({1}/{2})",
                          e.CurrentEntry.FileName, e.EntriesSaved, e.EntriesTotal);
        justHadByteUpdate= false;
    }
    else if (e.EventType == ZipProgressEventType.Saving_EntryBytesRead)
    {
        if (justHadByteUpdate)
            Console.SetCursorPosition(0, Console.CursorTop);
         Console.Write("     {0}/{1} ({2:N0}%)", e.BytesTransferred, e.TotalBytesToTransfer,
                      e.BytesTransferred / (0.01 * e.TotalBytesToTransfer ));
        justHadByteUpdate= true;
    }
}
public static ZipUp(string targetZip, string directory)
{
  using (var zip = new ZipFile()) {
    zip.SaveProgress += SaveProgress;
    zip.AddDirectory(directory);
    zip.Save(targetZip);
  }
}


I have copied this information from Ionic help file for my future reference.

DotNetZip - Zip and Unzip in C#, VB, any .NET language

DotNetZip is an easy-to-use, FAST, FREE class library and toolset for manipulating zip files or folders. Zip and Unzip is easy: with DotNetZip, .NET applications written in VB, C# - any .NET language - can easily create, read, extract, or update zip files. For Mono or MS .NET.

You can find more information at codeplex. Download from here.

Thursday, July 29, 2010

How to: Javascript Loading Conditionally for IE

Loading Javascript conditionally for IE. When rendering fixes are needed for IE, you can enclose CSS, JavaScript, or HTML markup in conditional comment tags directed at one or more versions of IE with an "if" statement; IE will then execute the code specified within them, while all other browsers treat them as standard comment tags and ignore them. The "if" statement must reference IE in square brackets as shown below; in this example include script only for IE

<!--[if IE]>
    <script type="text/javascript">
        /*  Do Stuff */
    </script>
<![endif]-->
below example shows how to exclude a script block from IE:
<![if !IE]>
    <script type="text/javascript">
        /*  Do Stuff */
    </script>
<![endif]>
Conditional comments can also be targeted to a particular version of IE, or a subset of versions, like those released prior to IE7 or IE6
<!--[if lt IE 7]>
  <link rel="stylesheet" type="text/css" href="ie_fixes.css" />
<![endif]-->

Hope this helps!