Wednesday, 16 November 2011

Sorting array of primitive types in C#


In C# array can be sorted using static method Array.Sort which internally use Quicksort algorithm.
To sort array of primitive types such as int, double or string use method Array.Sort(Array) with the array as a paramater. The primitive types implements interface IComparable, which is internally used by the Sort method (it calls IComparable.Com­pareTo method).

Ex: Sort int array
// declare int array
int[] intArray = new int[5] { 8, 10, 2, 6, 3 };
// sort int array
Array.Sort(intArray);
// write array
foreach (int i in intArray) Console.Write(i + " ");  
// output is: 2 3 6 8 10
  
Ex: Sort string array
// sort string array
string[] stringArray = new string[5] { "X", "B", "Z", "Y", "A" };
Array.Sort(stringArray);
// write array
foreach (string str in stringArray) Console.Write(str + " "); 
// output is: A B X Y Z

Performance tuning tricks for ASP.NET website and IIS 7

In performance tuning tricks for ASP.NET and IIS 7 we will look at some of the easy, yet powerful possibilities in the web.config file. By taking advantage of these few tricks we can increase the performance of any new or existing website without changing anything but the web.config file.
The following XML snippets must be placed in the <system.webServer> section of the web.config.

 

HTTP Compression
You’ve always been able to perform HTTP compression in ASP.NET by using third-party libraries or own custom built ones. With IIS 7 you can now throw that away and utilize the build-in compression available from the web.config. Add the following line to enable HTTP compression:
<urlCompression doDynamicCompression="true" doStaticCompression="true" dynamicCompressionBeforeCache="true"/>
By default, only text based content types are compressed.

 

doDynamicCompression
Setting this attribute to true enables compression of dynamically generated content such as pages, views, handlers. There really aren’t any reasons not to enable this.

doStaticCompression
This attribute allows you to decide whether or not you want static files such as stylesheets and script files to be compressed. Images and other non-text content types will not be compressed by default. This is also something you want to enable.

dynamicCompressionBeforeCache
If you do output caching from within your ASP.NET website, you can tell IIS 7 to compress the output before putting it into cache. Only if you do some custom output caching you might run into issues with setting this to true. Try it and test it. If your website works with this enabled, then you definitely want to keep it enabled.

Tip
By default, only text based content types are compressed. That means if you send application/x-javascript as content type, you should change it to text/javascript. If you use some custom modules in your website, then you might experience conflicts with the IIS 7 compression feature.

Saturday, 5 November 2011

URL properties of HttpContext.Current.Request.Url in ASP.NET


The following attributes are some of the useful properties of the URL object. I use the following URL to send my request to localhost:
Ex URL: http://localhost/Test/Home.aspx?test=fine
And I get the following results:
  1.  AbsolutePath = /Test/Home.aspx
  2. AbsoluteUri = http://localhost/Test/Home.aspx?test=fine
  3. Authority = localhost
  4. DnsSafeHost = localhost
  5. Fragment = “”
  6. Host = localhost
  7. HostNameType= Dns
  8. IsAbsoluteUri = True
  9. IsFile = False
  10. LocalPath = /Test/Home.aspx
  11. OriginalString = http://localhost:80/Test/Home.aspx?test=fine
  12. PathAndQuery = /Test/Home.aspx?test=fine
  13. Port = 80
  14. Query = ?test=fine
  15. Scheme = http
  16. UserEscaped = False
  17. UserInfo = “”

Friday, 4 November 2011

Feature of Silverlight


Silverlight combines multiple technologies into a single development platform that enables you to select the right tools and the right programming language for your needs. Silverlight offers the following features:
1.    WPF and XAML: Silverlight includes a subset of the Windows Presentation Foundation (WPF) technology, which greatly extends the elements in the browser for creating UI. Silverlight lets you create immersive graphics, animation, media, and other rich client features, extending browser-based UI beyond what is available with HTML alone. XAML provides declarative markup syntax for creating elements.

2.    Extensions to JavaScript: Silverlight provides extensions to the universal browser scripting language that provide control over the browser UI, including the ability to work with WPF elements.

3.    Cross-browser, cross-platform support: Silverlight runs the same on all popular browsers (and on popular platforms). You can design and develop your application without having to worry about which browser or platform your users have.

4.    Integration with existing applications: Silverlight integrates seamlessly with your existing JavaScript and ASP.NET AJAX code to complement functionality you have already created.

5.    Access to the .NET Framework programming model: You can create Silverlight applications using dynamic languages such as IronPython as well as languages such as C# and Visual Basic.

6.    Tools Support: You can use development tools, such as Visual Studio and Expression Blend, to quickly create Silverlight applications.

7.    Networking support: Silverlight includes support for HTTP over TCP. You can connect to WCF, SOAP, or ASP.NET AJAX services and receive XML, JSON, or RSS data.

8.    LINQ: Silverlight includes language-integrated query (LINQ), which enables you to program data access using intuitive native syntax and strongly typed objects in .NET Framework languages.

What is Silverlight


Microsoft Silverlight is a cross-browser, cross-platform implementation of the .NET Framework for building and delivering the next generation of media experiences and rich interactive applications (RIA) for the Web.
Silverlight uses the Extensible Application Markup Language (XAML) to ease UI development (e.g. controls, animations, graphics, layout, etc.) while using managed code or dynamic languages for application logic.
You can also create Silverlight applications that run outside of the browser on your desktop. Finally, you use the Silverlight framework to create applications for Windows Phone.

Silverlight enables you to create a state-of-the-art application that has the following features:
  • It is a cross-browser, cross-platform technology. It runs in all popular Web browsers, including Microsoft Internet Explorer, Mozilla Firefox, and Apple Safari, Google Chrome, and on Microsoft Windows and Apple Mac OS X.
  • It enables you to create Windows Phone applications.
  • It is supported by a small download that installs in seconds.
  • It streams video and audio. It scales video quality to everything from mobile devices to desktop browsers to 720p HDTV video modes.
  • It includes compelling graphics that users can manipulate—drag, turn, and zoom—directly in the browser.
  • It reads data and updates the display, but it doesn't interrupt the user by refreshing the whole page.
  • The application can run in the Web browser or you can configure it so users can run it on their computer (out-of-browser). In addition, you can use your knowledge of the Silverlight framework to create Windows Phone applications.
 You can create Silverlight applications in a variety of ways. You can use Silverlight markup to create media and graphics, and manipulate them with dynamic languages and managed code. Silverlight also enables you to use professional-quality tools like Visual Studio for coding and Microsoft Expression Blend for layout and graphic design.

Tuesday, 1 November 2011

Remove directory which contains read-only file in C#.net


using System.IO;

public bool DeleteDirectory(string dirPath)
{
if (Directory.Exists(dirPath))
      {
            try
            {
                ClearAttributes(dirPath);
                Directory.Delete(dirPath, true);
            }
            catch (IOException e)
            {
                Console.WriteLine(e.Message);
                return false;
            }
}
        return true;
}


public void ClearAttributes(string currentDir)
{
if (Directory.Exists(currentDir))
      {
            string[] subDirs = Directory.GetDirectories(currentDir);
            foreach (string dir in subDirs)
                ClearAttributes(dir);

            string[] files = files = Directory.GetFiles(currentDir);
            foreach (string file in files)
                File.SetAttributes(file, FileAttributes.Normal);
      }
}

Remove files attributes of a directory in c#.net


using System.IO;


private void ClearAttributes(string currentDir)
{
if (Directory.Exists(currentDir))
      {
            string[] subDirs = Directory.GetDirectories(currentDir);
            foreach (string dir in subDirs)
                ClearAttributes(dir);

            string[] files = files = Directory.GetFiles(currentDir);
            foreach (string file in files)
                File.SetAttributes(file, FileAttributes.Normal);
      }
}