Archive

Archive for the ‘C#’ Category

DotNetZip – Zip file manipulation in .NET

December 23, 2010 Leave a comment

DotNetZip is a small, easy-to-use class library for manipulating .zip files. It can enable .NET applications written in VB.NET, C#, or any .NET language, to easily create, read, and update zip files. Zip Compression is easy with DotNetZip. The DotNetZip project also produces a GUI ZIP tool, and a few command line tools.

DotNetZip works on Windows-powered PCs with the full .NET Framework, and also runs on Windows Mobile devices that use the .NET Compact Framework. Create and read zip files in VB, C#, or any .NET language. The library can also be used from COM enironments, like PHP, Classic ASP, or VB-Script. DotNetZip supports these scenarios:

  • creating a zip archive, adding files or directories into the archive
  • listing files in an archive, extracting files from an archive
  • modifying an existing archive – renaming entries, removing entries from an archive, or adding new entries to an archive
  • creating zip files from stream content, saving to a stream, extracting to a stream, reading from a stream
  • dynamically creating ZIP files from ASP.NET applications

Read more at http://dotnetzip.codeplex.com/

Categories: ASP.Net, C# Tags: , , , ,

Accessing Protected Pages in C#

September 24, 2010 Leave a comment

System.Net.WebRequest can be used to download a web resource in .Net code. Following is code to download a web resource in code:

String url = "http://domain.com/resource_url";
HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest;
WebResponse resp = req.GetResponse();
Stream Answer = resp.GetResponseStream();

This code works okay if the web resource is not protected and is public-ally accessible. But if the resource is protected using windows authentication, the code req.GetResponse() will return an Unauthorized exception.

To access a protected resource, we will need to use NetworkCredentials for the web request as shown in the following code:

String url = "http://domain.com/resource_url";
String user = "window_auth_username";
String pwd = "window_auth_password";
HttpWebRequest req = HttpWebRequest.Create(url) as HttpWebRequest;
req.PreAuthenticate = true; //Tell the server that request is authenticated
req.Credentials = new NetworkCredential(user, pwd, "http://domain.com"); //Link the credentials
//Remaining code will remain the same.

WebResponse resp = req.GetResponse();
Stream Answer = resp.GetResponseStream();
Categories: C#

A Generic way to Get IDs of server controls in java-script

April 25, 2009 Leave a comment


While working with ASP.Net and java-script, we need to manipulate the HTML objects on the form. If these objects are coded as server side controls, the ids on the client side are different to what we have on the server side.

E.g. a server side text box added in a page with ID=textTitle will have its client id=ctl00_ContentPlaceHolder1_textTitle. (ctl00 is sequence identifier, ContentPlaceHolder1 is id of the parent container and txtTitle is the actual object id) The value of the client id depends on the location of the control, if we add the same control in a different position or we move it inside a user control, its client id will be different.

So one option is to hard-code its client id to access the object in java-script. But this option does not seem appropriate, as the client id can be different with time, with either addition of the new controls or with moving the object to a user control or a similar reason.

Following approach solves this problem to some extent; this approach automates the process of mapping the server and client ids of all objects on a page.

  1. Include the jsClientHtmlObjects.js into the page.
  2. Add the code mentioned in the BasePage.cs to your page class (or its base class)
  3. Include the following script to the end of the page.aspx file just before the </body> tag:

    <
    script type=”text/javascript” language=”javascript”>
             <%=ClientObjectsList%>
    </script>
  4. In Javascript, where you want to access the client instance of the server side object, call the mthods:
    • objClientHtmlObjects.GetObject(serverID) //To get the HTML object with the passed server id
    • objClientHtmlObjects.GetValue(serverID) //To get the HTML object’s value with the passed server id
Categories: C# Tags:

Session Expires when Directory.Delete() is called

February 21, 2009 Leave a comment

In DotNet 2.0 and later if we delete some directory inside the web application root, application gets reset and all sessions are expired.

There are different options to resolve this issue, one of the solution is to disable the monitoring on web root:

using System.Reflection;

PropertyInfo p = typeof(System.Web.HttpRuntime).GetProperty(“FileChangesMonitor”, BindingFlags.NonPublic | BindingFlags.Public |  BindingFlags.Static);
object o = p.GetValue(null, null);
FieldInfo f = o.GetType().GetField(“_dirMonSubdirs”, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
object monitor = f.GetValue(o);
MethodInfo m = monitor.GetType().GetMethod(“StopMonitoring”, BindingFlags.Instance | BindingFlags.NonPublic);
m.Invoke(monitor, new object[] { });

Categories: ASP.Net, C#

C# Coding Standard

February 9, 2009 Leave a comment


While searching for some document on coding standards, i found following links useful:

http://se.ethz.ch/teaching/ss2007/251-0290-00/project/CSharpCodingStandards.pdf

www.dotnetspider.com/tutorials/CodingStandards.doc

Categories: C# Tags: