File Storage Capacity

File storage capacity is usually shown by multiple of bytes. The following table shows the multiple of bytes from KB to YB.­­­­ Writing a simple algorithm to convert a Byte value to an appropriate KB, MB…YB value can be achieved by divding the byte value by 1024.

 

 

 

 

 

 

How to Convert Bytes to KB, MB… YB?

The conversion is simple. The byte value should be divided by 1024 while is greater than 1024 (KB).

do { bytes /= 1024; index++; }
            while (bytes >= 1024);

The following code can be used to retrieve the total size of each drive and depends on the size, the output will be formatted to the closest byte value such as GB, TB, and so on.

 

 

 

 

 

 

 

 

 

 

 

 

The output is shown below:

Zip Archive Class in .NET 4.5

In .NET 4.5, the System.IO.Compression namespace contains the following new classes for compressing and decompressing the streams:

1- ZipArchive which represents a Zip archive.

2- ZipArchiveEntry which represents an entry in the zip archive.

The ExtractToDirectory and CreateFromDirectory methods of ZipArchive class can be used to handle the following most used scenarios:

Scenario 1 – Unzipping: Use ExtractToDirectory method to extract all the contents of a zip archive to the specified directory

Scenario 2 – Zipping: the contents: Use CreateFromDirectory to take the contents of the directory and to zip it’s content to a zip file

Example 1. ZipArchive.CreateFromDirectory(@”docs\attach”, “attachment.zip”);

Example 2. ZipArchive.ExtractToDirectory(“Photos.zip”, @”photos\January2012″);

What is ZipArchiveEntry?

ZipArchive represents a zip archive, which is a collection of entries, and ZipArchiveEntry represents an archived file entry.
A ZipArchive class (representing a zip file ) can have one or more ZipArchiveEntry class (representing normal files like text file, doc file, pdf file etc)

The following example extracts only the text files from an archive:

using (var archive = new ZipArchive(“data.zip”))
{

foreach (var entry in archive.Entries)
{

if (entry.FullName.EndsWith(“.txt”, StringComparison.OrdinalIgnoreCase))
{

entry.ExtractToFile(Path.Combine(directory, entry.FullName));

}

}

}

Silverlight 5

Upgrading Projects from Silverlight 4 to Silverlight 5

Several changes have been made to the Silverlight runtime and the Silverlight Tools between Silverlight 4 and Silverlight 5. For these changes, the following principles apply:

1- Most Silverlight 4 applications will work with Silverlight 5 without any changes.

2- When breaking changes are required, Silverlight will try to maintain support for the old behavior, as well as the new behavior, by using a quirks mode.

Nevertheless, some changes made to Silverlight components can potentially cause your older Silverlight-based applications to fail (compile time, XAML load time, or possibly design time) or to behave differently.

There are no known breaking changes between Silverlight 4 and Silverlight 5 in the Silverlight core runtime. Applications that were originally compiled using Silverlight 4 tools, and that continue to target the Silverlight 4 runtime, should work without issues on a client that has the Silverlight 5 runtime.

To retarget an existing Silverlight 4 project for the Silverlight 5 runtime specifically, you should change the project targeting in Visual Studio, using the Silverlight Build Options / Target Silverlight Version section of the Visual Studio project configuration dialog. Then, recompile the project.

Silverlight 5 Templates

The latest Silverlight templates are available in Silverlight Toolkit December 2011. This toolkit is the 9th release which is targeting the Silverlight 5’s new 3D features.

Roslyn CTP

What is Roslyn?

Microsoft compilers have been always acting as black boxes. You provide the source text in and out the other end, you receive an assembly. All of that rich knowledge and information that the compiler produces is thrown away and unavailable for anyone else to use.

Visual Studio team has recently been working on a project called Roslyn with a goal to rewrite the C# and VB compilers and language services in managed code. This means that Microsoft is opening up the C# and Visual Basic compilers and exposing all that rich information and code analysis to be available for the software developers. Microsoft exposes a public API surface and provide extension points in the C# and VB language services. This opens up new opportunities for VS extenders to write powerful refactorings and language analysis tools, as well as allow anyone to incorporate Microsoft parsers, semantic engines, code generators and scripting in their own applications.
The Community Technological Preview (CTP) version can be downloaded from Roslyn CTP

Getting Started

After installing the Roslyn CTP, the best place to start is to open Start Menu -> Microsoft Codename Roslyn CTP -> Getting Started.
The CTP ships with quite a few samples for Visual Studio Extensions, compiler API, code issues, refactorings and so on. Most of the samples are provided for both C# and Visual Basic. You can open the sample source code from the Getting Started page.
In addition, several new project templates are available in the New Project dialog:

You can run scripts using the new rcsi.exe, which installs into %ProgramFiles(x86)%\Microsoft Codename Roslyn CTP\Binaries\rcsi.exe. You can add rcsi.exe to the path and then type rcsi .csx.
You can also copy chunks of code from a script file and send them to the C# Interactive Window (using the right-click context menu or a keyboard shortcut).

WinRT versus Win32 on Windows 8

The Windows Runtime API (WinRT) replaces the old Win32 libraries for accessing operating system functions. The WinRT API is object oriented, largely asynchronous, and callable easily by a wide variety of programming languages. Win32 continues to be supported by Windows 8, and it is what enables all existing Windows 7 applications to function in the Windows 8 desktop environment. The following figure shows how existing technologies map to the Windows 8 development platform.

 

 

 

 

 

 

 

 

Windows 8 Development Strategy

When considering the impact of Windows 8 on future software development, the following broad strategies should be evaluated:

1- Continue to use existing technologies, and run the application in the desktop environment.

2- Create a WinRT/Metro style smart client application that takes full advantage of the new WinRT and Windows 8 features.

3- Create a browser-based web application that relies on no plug-ins, so it can run in the browser in both the WinRT and desktop environments.

In summary, the new WinRT API and Metro style applications may represent the future of smart client development on the Windows operating system. Existing applications will continue to run in the Windows 8 desktop environment. Additionally, existing web applications that avoid the use of plug-ins will run in the WinRT web browser.
Existing developer skills in XAML, C#, VB, .NET, and Silverlight carry forward to WinRT development. The same is true for HTML5, CSS, and JavaScript developer skills.

Random Number Generator

Random Numbers

The generation of random numbers is important in many applications like simulations, cryptography, sampling and mostly in statistics. A sequence of numbers is random when it does not have a recognizable pattern in it or in other words if it is non-deterministic. Although non-deterministic random numbers are ideal, the computer generated, deterministic random numbers can be statistically “random enough”. These random numbers are named as pseudo-random numbers and can have easily identifiable patterns if the algorithm is not chosen wisely. Sometimes, you need to generate a cryptographically secure random number, for example, for creating a random password. For this scenario, you should use a class derived from the System.Security.Cryptography.RandomNumberGenerator. For example, you can use System.Security.Cryptography.RNGCryptoServiceProvider. CryptoRandom brings with it a performance cost. While Random’s implementation does not produce cryptographically strong random numbers, it is extremely fast when compared to RNGCryptoServiceProvider. Roughly, random is over 100 times faster than CryptoRandom.

Performance of PLINQ Queries

Parallel LINQ (PLINQ)
The main goal of the Parallel LINQ, or PLINQ is to execute LINQ to Objects queries in parallel, realizing the benefits of multithreading. Using PLINQ is simple, if you have to perform the same task on each element in a sequence, and those tasks are independent. If you need the result of one calculation step in order to find the next, PLINQ is not for you but many CPU intensive tasks can in fact be done in parallel. To tell the compiler to use PLINQ, you just need to call AsParallel and let PLINQ handle the threading.
The following samples demonstrate the performance of PLINQ queries for different scenarios:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The result is shown below:

Without the AsParallel call, we would only use a single thread. Please note that except you specify that you want the results in the same order as the original sequence, PLINQ will assume you don’t mind getting results as soon as they’re available, even if results from earlier elements haven’t been returned yet. You can prevent this by using AsParallel().AsOrdered()

When to Use PLINQ

It’s tempting to search your existing applications for LINQ queries and experiment with parallelizing them. This is usually unproductive, because most problems for which LINQ is obviously the best solution tend to execute very quickly and so don’t benefit from parallelization. A better approach is to find a CPU-intensive bottleneck and then consider, “Can this be expressed as a LINQ query?”
PLINQ is well suited to embarrassingly parallel problems. It also works well for structured blocking tasks, such as calling several web services at once. PLINQ can be a poor choice for imaging, because collating millions of pixels into an output sequence creates a bottleneck. Instead, it’s better to write pixels directly to an array or unmanaged memory block and use the Parallel class or task parallelism to manage the multi-threading.

Concurrency Visualizer SDK

What is new in Concurrency Visualizer SDK?



 

 

 

 

 

 


The Concurrency Visualizer displays rich data related to CPU thread behavior, DirectX activity, and disk I/O, among other things. This information can be incredibly valuable when investigating application behavior, but sometimes it is difficult to quickly understand how the data displayed in the Concurrency Visualizer maps to application behavior. The new Concurrency Visualizer SDK, which you can use in Visual Studio 11 Developer Preview allows you to instrument your code in order to augment the visualizations displayed in the Threads View of the Concurrency Visualizer. These visualizations, referred to as “Markers”, make the Threads View data more semantically meaningful because they represent specific phases and events in your application.
For those of you who have used Scenario Markers with the Visual Studio 2010 Concurrency Visualizer, you’ll find that the Concurrency Visualizer SDK is conceptually similar, but provides much more control and flexibility.
The SDK exposes three visual primitives: Span, Flag, and Message
A span represents an interval of time in your application, such as an application phase. A flag represents a single point in time (e.g. the point where some value reached a threshold or when an exception was thrown). A message also represents a single point in time, but is meant as a visual analog to classic event-style tracing. So what might have previously been dumped to a log file can now be wrapped in a message call. This will yield visualizations in the Threads View and you’ll have the ability (via the UI) to export the data into a CSV file.

Adobe Flash (SWF) to XAML Export





 

What is Adobe Flash (SWF) to XAML Export Tool?

This tool allows you to directly open a SWF file and browse through its frames one by one. When you have found a frame that you like, you can either export to a XAML file or copy the XAML to the clipboard for easy pasting into your tool of choice.

To get the SWF2XAML tool up-and-running on your system, follow the follwoing steps:

Step 1- Make sure that you have the .NET Framework 3.0 installed on your machine.

Step 2- download SWF2XAML_0.2.zip from the following link: SWF2XAMLExport

Step 3- Extract all of the files to a folder of your choice.

Step 4- Double-click SWF2XAML.exe to run the tool.

Using Lambda Expressions in C#

What is a Lambda Expression?

A lambda expression is an anonymous function or simply a method that can contain expressions and statements, and can be used to create delegates or expression tree types. All lambda expressions use the lambda operator =>, which is read as “goes to”. The left side of the lambda operator specifies the input parameters (if any) and the right side holds the expression or statement block. The lambda expression x => x * x is read “x goes to x times x.”

The following examples show how to use lambda expressions in C#:

Copyright © All Rights Reserved - C# Learners