Thursday, January 28, 2016

Adding resource file for localization in Asp.net

Localization is the process of customization to make our application behave as per the current culture and locale. So in that case what we do is we keep key value pairs in a type of file called resource files (.resx files)

Lets see how to store key value pairs in a resource file and how to use them in a Razor view.(Assume our project name is "MyApplication" for examples)


  1. Create a directory in your project for resource files (Ex: directory named "Localization")
  2. To create a resource file in that directory,
    right click on that directory => click Add => click New Item => Select general tab under Visual C# => Select Resource File => click Add (Give a name to that file as you want ex:MyResource)
  3. Then your resource file will be displayed in a table format in Visual Studio Insert your Name , Value pairs. 
  4. Set "Custom Tool Namespace" value as path to your resource file (ex:MyApplication.Localization)
Now you have created a resource file successfully. Then you just have to use them in Razor views.
for that,
Add this code at the top of your views.

@using Resources = MyApplication.Localization

Here MyApplication is project name and Localization is directory of resource file.

Then when ever you want to use a value in Razor view use this,

@Resources.MyResource.Name

Here "MyResource" is name of your resource file and "Name" name of your key value pair. 

Saturday, January 23, 2016

Direct user to a web page using a bat file


code is simple.

@echo off
title Go to site
start http://jascripts.blogspot.com/

Save this batch script with a name you want and after executing it you will be taken to the URL specified in your script in your default browser.

Note: If it gives errors
use start iexplore.exe http://jascripts.blogspot.com/

First steps to write batch files in windows



I'm sure that nearly every windows user have seen files with extension .bat . What are the they, What do they do, How can we make them.

They are actually one type of files that exist in windows operating system and those files can be used to get some work done by OS.

Lets see how we can create those files with simple example.

@echo off
title My First Batch File
echo Hello!
pause


  • Don't worry about the first line @echo off (If you want check out more details from here)
  • title My First Batch File this line gives a title to our batch file
  • echo Hello! line print the word "hello!"
  • pause this will pause the program for user inputs
Now save this file with any name you want with .bat extension and double click on the created batch file to execute what you programmed.

Note:Make sure when you save set Save as type to "All types".


Friday, January 22, 2016

Split string by another string in C#

Normally developers use String.Split method to split a string by a character. An Overloaded version of same method can be used to split a string by string in C#.


string data = "THExxQUICKxxBROWNxxFOX"; 
return data.Split(new string[] { "xx" }, StringSplitOptions.None);

  • A string array should be passed as first parameter. 
  • In second parameter we can specify whether the substrings include empty array elements. etc
In this example string "xx" is passed but multiple string values can be passed as we want

string value = "The handsome, energetic, young dog was playing with his smaller, more lethargic litter mate."; 
string[] separators = {",", ".", "!", "?", ";", ":", " "}; 
string[] words = value.Split(separators, StringSplitOptions.RemoveEmptyEntries); 
 foreach (var word in words) 
  Console.WriteLine(word);

more details can be found here.

Thursday, January 21, 2016

Best method to extract text from PDF in asp.net C#


There are many ways to extract text from PDF.
  • Microsoft IFilter interface and Adobe IFilter implementation
  • iTextSharp
  • PDFBox
Among those solutions using PDFBox is the best according to my experiences because,
  • Using Adobe IFilter implementation need to have lot of other things set up in running machine like,
    • Windows 2000 or later
    • Adobe Acrobat or Reader 7.0.5+
    • IFilter COM wrapper class
  • iTextSharp is opensource but can't be used in commercial products.
So the best solution is using PDFBox. It doesn't need other things installed on your machine.

PDFBox is a java PDF library and there is a .net version of PDFBox (It can be downloaded from here


Here is an example how to use PDFBox


using org.apache.pdfbox.pdmodel;
using org.apache.pdfbox.util;

// ...

private static string ExtractTextFromPdf(string path)
{
  PDDocument doc = null;
  try {
    doc = PDDocument.load(path)
    PDFTextStripper stripper = new PDFTextStripper();
    return stripper.getText(doc);
  }
  finally {
    if (doc != null) {
      doc.close();
    }
  }
}  

NOTE:
  1. Following dll files of PDFBox library should be added to bin file of your project
    • commons-logging.dll
    • fontbox-1.8.9.dll
    • IKVM.OpenJDK.Text.dll
    • IKVM.OpenJDK.Util.dll
    • IKVM.Runtime.dll
  2. Reference for following dll s should be added
    • IKVM.OpenJDK.Core.dll
    • IKVM.OpenJDK.SwingAWT.dll
    • pdfbox-1.8.9.dll

Deleting Files in ASP.NET C#

Simplest way to delete files in asp.net C#

if ((System.IO.File.Exists(fileName)))
                {
                    System.IO.File.Delete(fileName);
}


This is a simple code snippet in asp.net that can be used to delete files from any directory.
In here variable "fileName" is the specific path to the file.

Ex: If want to delete a file called "hello.pdf" in "upload" folder which is in my C drive, file name should be full path to that file "C:\upload\hello.pdf"


Monday, January 11, 2016

What is Windows Power Shell & how it differs from CMD

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework.
If it sounds complicated you can think it as same as the CMD prompt but with more advance tools.
PowerShell is a much more powerful command-line shell and scripting language than the Command Prompt is. It gives Windows system administrators a useful command-line environment.

There are some fabulous things that can be done using Power Shell
  • perform your favorite CMD tasks
  • Kill processes in power shell instead of using task manager
  • Export NTFS file permossions
  • Play with power sell 2.0
  • Background a time consuming task
  • Report all of the USB devices installed
( Read more about these tasks)

There are few main differences between cmd prompt and power shell
  • Power sell uses a different set of commands called cmdlets.Many system administration tasks are exposed via Power Shell cmdlets
  • PowerShell makes use of pipes, just as Linux and other Unix-like systems do. 
  • You can create complex scripts for managing Windows systems much more easily than you could with the Command Prompt.
So here is why people use Power Sell over cmd prompt
  • Cmd prompt is limited, can’t access many Windows system administration features.
  • It is is more difficult to compose complex scripts with 
PowerShell is a new environment for Windows system administrators that allows them to use a more modern command-line environment to manage Windows.

Optimize you working enviorenment : Single command to create & move to a directory in linux (C Shell, Bash)

Usually move to a directory just after creating is bit of a anxious task specially if the directory name is too long. mkdir long-name-of...