Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

How to get image URL from site-core via C#

Jul 2, 2015 Posted by Lara Kannan 0 comments
This is just a quick post to show how to get image URL via code behind C#.

This is mostly useful when you are binding children items to a repeater or returning image URL in a JSON web-service.

 public static string GetImageURL(Item currentItem)  
 {  
      string imageURL = string.Empty;  
      Sitecore.Data.Fields.ImageField imageField = currentItem.Fields["Image"];  
      if (imageField != null && imageField.MediaItem != null)  
      {  
       Sitecore.Data.Items.MediaItem image = new Sitecore.Data.Items.MediaItem(imageField.MediaItem);  
       imageURL = Sitecore.StringUtil.EnsurePrefix('/', Sitecore.Resources.Media.MediaManager.GetMediaUrl(image));  
      }  
 return imageURL;  
 }  


Special Thanks To : 


Share
Labels: ,

Design Patterns : An introduction

Jan 12, 2011 Posted by Lara Kannan 1 comments
Design Patterns

Design Patterns are an essential tool for any object orientated programmer who wants to take his/her skills to the next level. Understanding the specifics of Design Pattern practices and methodologies will help developers uncover reoccurring patterns in software development and give them the tools to apply the correct solution.

Types of Design Patterns

  • Informal Design Patterns - such as the use of standard code constructs, best practice, well structured code, common sense, the accepted approach, and evolution over time.

  • Formal Design Patterns - documented with sections such as "Context", "Problem", "Solution", and a UML diagram.

Formal patterns usually have specific aims and solve specific issues, whereas informal patterns tend to provide guidance that is more general. Formal patterns usually have specific aims and solve specific issues, whereas informal patterns tend to provide guidance that is more general.

For example, some patterns provide presentation logic for displaying specific views that make up the user interface. Others control the way that the application behaves as the user interacts with it. There are also groups of patterns that specify techniques for persisting data, define best practices for data access, and indicate optimum approaches for creating instances of objects that the application uses.

The following list shows some of the most common design patterns within these groups:

  • Presentation Logic

    1. Model-View-Controller (MVC)

    2. Model-View-Presenter (MVP)

    3. Use Case Controller

  • Host or Behavioral

    1. Command

    2. Publish-Subscribe / Observer

    3. Plug-in / Module / Intercepting Filter

  • Structural

    1. Service Agent / Proxy / Broker

    2. Provider / Adapter


  • Creational

    1. Factory / Builder / Injection

    2. Singleton


  • Persistence

    1. Repository


    1. test





Share
Labels: ,

To Upper Case First Letter of a String in C#

Oct 28, 2010 Posted by Lara Kannan 0 comments
Utilize a simple extension method to capitalize the first letter of a string.


public class Program
{
static void Main(string[] args)
{
string s = "toUpperFirstLetter";

// ToUpperFirstLetter is the extension method defined below
Console.WriteLine(s.ToUpperFirstLetter());

// output: ToUpperFirstLetter
Console.ReadLine();
}
}

// string extension class
public static class StringExtension
{
// string extension method ToUpperFirstLetter
public static string ToUpperFirstLetter(this string source)
{
if (string.IsNullOrEmpty(source))
return string.Empty;
// convert to char array of the string
char[] letters = source.ToCharArray();

// upper case the first char
letters[0] = char.ToUpper(letters[0]);

// return the array made of the new char array
return new string(letters);
}
}


Source : www.codemeit.com
Share
Labels: ,

Who is father of C# or C Sharp ?

Apr 23, 2010 Posted by Lara Kannan 2 comments
The father of C#

The father of C# is Mr.Anders Hejlsberg who born December 1960, is a prominent Danish software engineer who co-designed several popular and commercially successful programming languages and development tools.

  • He was the original author of Turbo Pascal

  • The chief architect of Delphi

  • Currently works for Microsoft as the lead architect of the C# programming language


Awards ...

  • 2001 - Dr. Dobb's Excellence in Programming Award for his work on Turbo Pascal

  • 2010 - Technical Recognition Award for Outstanding Technical Achievement for their work on the C# language
Now C# 4.0 released with Dot Net Framework 4.0.






VersionMicrosoft compiler
C# 1.0January 2002
C# 2.0November 2005
C# 3.0November 2006
C# 4.0April 2010


Share
Labels: ,

Free ebook - C# 2008 In Simple Steps

Apr 17, 2010 Posted by Lara Kannan 2 comments
C# 2008 In Simple Steps

This is a book that helps you to learn C# using Visual Studio 2008. Precision, an easy-to-understanding style, real life examples in support of the concepts, and practical approach in presentation are some of the features that make the book unique in itself.

The text in the book is presented in such a way that is equally helpful to beginners as well as professionals. Apart from basic concepts of C#, this edition of the book particularly deals with some new and advanced topics, such as WPF, WCF, WF and LINQ.



Product Details

  • Paperback: 280 pages
  • Dreamtech Press (2009)
  • Language: English
  • ISBN-10: 8177229184

Free Download Links :

1. Hotfile Link
http://hotfile.com/dl/.../8177229176.rar.html

2. Depositfiles Link
http://depositfiles.com/.../2fhcbp8jt

3. Uploading Link
http://uploading.com/.../8177229176CSteps.rar/

4. Megaupload Link
http://www.megaupload.com/?d=4F42LAWO


If you like this post, please add your comments and 'A thanks' would be nice.

Thanks : www.freebooks.download-123.com

Share

Free ebook - Professional C# 4.0 and .NET 4

Apr 14, 2010 Posted by Lara Kannan 1 comments
Professional C# 4.0 and .NET 4

The new C# 4 language version is indispensable for writing code in Visual Studio 2010. This essential guide emphasizes that C# is the language of choice for your .NET 4 applications.

The unparalleled author team of experts begins with a refresher of C# basics and quickly moves on to provide detailed coverage of all the recently added language and Framework features so that you can start writing Windows applications and ASP.NET web applications immediately.


Product Details

  • Paperback: 1536 pages
  • Wrox; 1 edition (March 8, 2010)
  • Language: English
  • ISBN-10: 0470502258
  • ISBN-13: 978-0470502259

Free Download Links :

1. Hotfile Link
http://hotfile.com/.../Wrox.Professional.CSharp.4.and.NET.4.Mar.2010.rar.html

2. Depositfiles Link
http://depositfiles.com/files/v874ehlvf

3. Megaupload Link
http://www.megaupload.com/?d=VD037AZW

4. Uploading Link
http://uploading.com/.../CSharp.4.and.NET.4.rar/

5. SharingMatrix Link
http://sharingmatrix.com/.../CSharp.4.and.NET.4.rar

If you like this post, please add your comments and 'A thanks' would be nice.

Thanks : ebookw.net

Share

Free ebook - Essential C# 4.0, 3rd Edition

Apr 6, 2010 Posted by Lara Kannan 0 comments
Essential C# 4.0, 3rd Edition

Essential C# 4.0 is a well-organized,“no-fluff” guide to all versions of C# for programmers at all levels of C# experience. This fully updated edition shows how to make the most of C# 4.0’s new features and programming patterns to write code that is simple, yet powerful.

This edition contains two new chapters on parallel programming, multi­threading, and concurrency, as well as extensive coverage of new C# 4.0 features: dynamic typing, variance, optional/named parameters, and many other new topics.

Mark Michaelis covers the C# language in depth, illustrating key constructs with succinct, downloadable code examples. Graphical “mind maps” at the beginning of each chapter show what material is covered and how individual topics interrelate.

Topics intended for beginners and advanced readers are clearly marked, and the book includes indexes of C# versions (2.0, 3.0, and 4.0), which make it easy for readers to reference topics specific to a given release of C#.


Product Details

  • Paperback: 984 pages
  • Addison-Wesley Professional; 3 edition (March 20, 2010)
  • Language: English
  • ISBN-10: 0321694694
  • ISBN-13: 978-0321694690

Free Download Links :

1. Hotfile Link
http://hotfile.com/.../Addison.Wesley.Essential.CSharp.4.0.Feb.2010.rar.html

2. Depositfiles Link
http://depositfiles.com/../qpw02z5ro

3. SharingMatrix Link
http://sharingmatrix.com/file/3164843

4. Megaupload Link
http://www.megaupload.com/?d=GLA2ONHW

5. Ifile Link
http://ifile.it/dl

If you like this post, please add your comments and 'A thanks' would be nice.


Share

Free ebook - Numerical Methods, Algorithms and Tools in C#

Posted by Lara Kannan 0 comments
Numerical Methods, Algorithms and Tools in C#

The book focuses on standard numerical methods, novel object-oriented techniques, and the latest Microsoft .NET programming environment. It covers complex number functions, data sorting and searching algorithms, bit manipulation, interpolation methods, numerical manipulation of linear algebraic equations, and numerical methods for calculating approximate solutions of non-linear equations.

The author discusses alternative ways to obtain computer-generated pseudo-random numbers and real random numbers generated by naturally occurring physical phenomena.

He also describes various methods for approximating integrals and special functions, routines for performing statistical analyses of data, and least squares and numerical curve fitting methods for analyzing experimental data, along with numerical methods for solving ordinary and partial differential equations.

The final chapter offers optimization methods for the minimization or maximization of functions.

Exploiting the useful features of C#, this book shows how to write efficient, mathematically intense object-oriented computer programs. The vast array of practical examples presented can be easily customized and implemented to solve complex engineering and scientific problems typically found in real-world computer applications.



Product Details

  • Paperback: 600 pages
  • Numerical Methods, Algorithms and Tools in C# (October 10, 2009)
  • CRC Press
  • Language: English
  • ISBN-10: 0849374790

Free Download Links :

1. SharingMatrix Link
http://sharingmatrix.com/.../0849374790.rar

2. Rapidshare Link
http://rapidshare.com/files/369658409/0849374790.rar

3. File2box Link
http://www.file2box.net/xui1dg28qqku

4. Uploading Link
http://uploading.com/.../0849374790.rar/


If you like this post, please add your comments and 'A thanks' would be nice.


Share

Convert a DataTable to a List<> using C#

Feb 10, 2010 Posted by Lara Kannan 0 comments
Question is...
Convert a DataTable to a List<> using C#

To convert a DataTable to a List<> in .NET 2.0.

Here’s the code to do so:


// Assuming there is a DataTable called dt
List<DataRow> drlist = new List<DataRow>();

foreach (DataRow row in dt.Rows)
{
drlist.Add((DataRow)row);
}

Please note that this is just a prototype and may not cover all scenarios.

Note: I have not tested this but in .NET 3.5, you should be able to do this:

List<DataRow> drlist = dt.AsEnumerable().ToList();


Share
Labels: ,

Simplest way to Programmatically ShutDown or Restart your System using C#

Posted by Lara Kannan 0 comments
Question is...
Simplest way to Programmatically ShutDown or Restart your System using C#

One of the ways to ShutDown or Restart your system is to use WMI (Windows Management Interface). However it does require you to write a few lines of code.

I have found that the simplest way to ShutDown or Restart your computer is to use the Process class in the System.Diagnostics namespace. Here’s an example:


private void btnShutDown_Click(object sender, EventArgs e)
{
Process.Start("shutdown", "-s -f -t 0");
}

private void btnRestart_Click(object sender, EventArgs e)
{
Process.Start("shutdown", "-r -f -t 0");
}


Share
Labels: ,

Convert a String Array to a Decimal Array using C#

Posted by Lara Kannan 0 comments
Question is...
Convert a String Array to a Decimal Array using C#

The Array.ConvertAll() method is a very useful method to convert an array of one type to an array of another type.

Here’s how to use ConvertAll() to convert a String[] to Decimal[]


static void Main(string[] args)
{
string[] strArray = new string[] { "1164", "2213" };

decimal d;
decimal[] d1;

// Check if string can be converted to decimal equivalent
if (strArray.All(number => Decimal.TryParse(number, out d)))
{
d1 = Array.ConvertAll<string, decimal>(strArray, Convert.ToDecimal);
}
}

The first step is to use Enumerable.All(TSource) to determine whether all the elements in a sequence satisfy a condition, in our case, if all the elements of the array can be converted to its decimal equivalent.

Once this can be done, we use ConvertAll() to convert an array of string to an array of decimal.

Share
Labels: ,

Convert LowerCase and UpperCase to TitleCase in C#

Posted by Lara Kannan 0 comments
Question is...
Convert LowerCase and UpperCase to TitleCase in C#

The String class has the ToLower() and the ToUpper() methods to convert a string to lowercase and uppercase respectively.

However when it comes to converting the string to TitleCase, the ToTitleCase() method of the TextInfo class comes very handy. Let me demonstrate this with an example:


class Program
{
static void Main(string[] args)
{
string strLow = "lower case";
string strT = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(strLow);
Console.WriteLine("Lower Case to Title Case: " + strT);

string strCap = "UPPER CASE";
strT = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(strCap.ToLower());
Console.WriteLine("Upper Case to Title Case: " + strT);
Console.ReadLine();
}
}

Make sure you import the System.Globalization namespace. As you can observe, we are using the CurrentCulture.TextInfo property to retrieve an instance of the TextInfo class based on the current culture.

Note:
As mentioned on the site, "this method does not currently provide proper casing to convert a word that is entirely uppercase, such as an acronym". This is the reason why we first convert strCap to lower case and then supply the lowercase string to ToTitleCase() in the following manner:

CultureInfo.CurrentCulture.TextInfo.ToTitleCase(strCap.ToLower());

Share
Labels: ,

Convert Enum to List<> using C#

Posted by Lara Kannan 0 comments
Question is...
Convert Enum to List<> using C#

A user recently asked me how to convert an Enum to a List<>.

Here's the code to do so:

class Program
{
enum Days { Sat, Sun, Mon, Tue, Wed, Thu, Fri };

static void Main(string[] args)
{
Array arr = Enum.GetValues(typeof(Days));
List<string> lstDays = new List<string>(arr.Length);

for (int i = 0; i < arr.Length; i++)
{
lstDays.Add(arr.GetValue(i).ToString());
}
}
}


Share
Labels: ,

Find Out the Image Type without Checking its Extension using C#

Posted by Lara Kannan 0 comments
Question is...
Find Out the Image Type without Checking its Extension using C#

My colleague called up with a query. His application accepted images to be uploaded to a central server. Later the application processed the images and separated them according to the Image Type. They determined the image type by checking the extension of the image uploaded –- a very common practice of detecting the image type.

The issue here was that some images which were actually gif’s were renamed as jpeg's and then uploaded, which led to faulty image processing. The requirement was to determine the correct image type in the simplest possible way even if the extension was renamed.

Here’s what I suggested:


try
{
Image imgUp = Image.FromFile(Server.MapPath("~/images/abc.jpg"));

if (imgUp.RawFormat.Equals(ImageFormat.Jpeg))
Response.Write("JPEG");

else if (imgUp.RawFormat.Equals(ImageFormat.Gif))
Response.Write("GIF");
}
catch (Exception ex)
{
}


Share
Labels: ,

Count Words and Characters in a String using C#

Feb 9, 2010 Posted by Lara Kannan 0 comments
Question is...
Count Words and Characters in a String using C#

A user recently asked me a question on how to use Regular Expressions to count words and characters in a string. Here’s how:

First add a reference to System.Text.RegularExpressions

Here the strOriginal is your string.

// Count words
MatchCollection wordColl =
Regex.Matches(strOriginal, @"[\S]+");

Console.WriteLine(wordColl.Count.ToString());

// Count characters. White space is treated as a character
MatchCollection charColl = Regex.Matches(strOriginal, @".");
Console.WriteLine(charColl.Count.ToString());


Share
Labels: ,

Find Information about your Network Cards using C#

Posted by Lara Kannan 0 comments
Find Information about your Network Cards using C#

Here’s a small snippet of code of how to find out which network cards are enabled and able to transmit data on your machine. Here’s the code.

Add a reference to System.Net.NetworkInformation


var nics = NetworkInterface.GetAllNetworkInterfaces()
.Where(o => o.OperationalStatus == OperationalStatus.Up);

foreach (var item in nics)
{
Response.Write(item.Description + "<br />");
Response.Write(item.Name + "<br />");
Response.Write(item.Speed + "<br />");
Response.Write(item.NetworkInterfaceType + "<br /><br />");
}


Share
Labels: ,

Hiding a File using C#

Posted by Lara Kannan 0 comments
Hiding a File using C#

The FileInfo class is very useful class that contains methods for the creation, copying, deletion, moving, and opening of files. Here’s how to hide a file using C#.


class Program
{
static void Main(string[] args)
{
try
{
string filePath = @"D:\Network.txt";
FileInfo f = new FileInfo(filePath);
f.Attributes = FileAttributes.Hidden;
Console.WriteLine("File Hidden");
}
catch (Exception ex)
{
// handle ex
}
}
}


Share
Labels: ,

Retrieve a List of the Services Running on your Computer using C#

Posted by Lara Kannan 0 comments
Retrieve a List of the Services Running on your Computer using C#

The ServiceController class can be used to retrieve a list of the services running on your computer. The GetServices() can be used to do so. Here’s an example to list the services that are running on your machine

Add a reference to System.ServiceProcess.

Then write the following code:


using System;
using System.ServiceProcess;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
ServiceController[] sController =
ServiceController.GetServices();
foreach (ServiceController sc in sController)
{
if (sc.Status.ToString() == "Running")
{
Console.WriteLine(sc.ServiceName);
}
}
Console.ReadLine();
}
catch (Exception ex)
{
// handle ex
}
}
}
}


Share
Labels: ,

List the Drives on your Computer using C#

Posted by Lara Kannan 0 comments
List the Drives on your Computer using C#?

Imagine you have two List. If you want to create a program to provide information about your Computer Drive, then use the System.IO.DriveInfo class.

The DriveInfo class can be used to determine the drives available on your computer, drive capacity, free space, type of drives etc. as shown below:


// Add namespace System.IO;

DriveInfo[] myDrives = DriveInfo.GetDrives();

foreach (DriveInfo di in myDrives)
{
Console.WriteLine(di.Name);
if (di.IsReady)
{
Console.WriteLine(di.TotalSize);
Console.WriteLine(di.DriveFormat);

Console.WriteLine(di.AvailableFreeSpace);
Console.WriteLine(di.TotalFreeSpace);

Console.WriteLine(di.DriveType);
Console.WriteLine(di.VolumeLabel);
}
}


Share
Labels: ,

Compare Two List of Strings in C#

Posted by Lara Kannan 0 comments
Compare Two List of Strings in C#?

Imagine you have two List. You want to quickly compare them to see if all the elements match each other. Here's how to do so:

List<string> strList1 = new List<string>
{
"Jack", "And", "Jill", "Went", "Up", "The", "Hill"
};

List<string> strList2 = new List<string>
{
"Jack", "And", "Jill", "Went", "Down", "The", "Hill"
};

bool result = strList1.SequenceEqual(strList2);


Share
Labels: ,