Tuesday, 2 October 2012

How to replace text in file in .Net?

Definition: How to replace text in file?

Method:
1) Add namespace
    using System.IO;

Code:
 protected void btnReplace_Click(object sender, EventArgs e)
    {
        string sfileName = string.Empty;
        sfileName = "F:\\Work\\HTML\\Profile.txt";

        File.WriteAllText(
            sfileName,
            File.ReadAllText(sfileName)
                .Replace("Name", "Abhishek")
                .Replace("Address","Block No.123,LBS Road,Mumbai,Maharastra")
                .Replace("Mobile", "93117481253")
       );

    }


How to unzip file in .Net?

Definition: How to unzip file in .Net?

Method: 


1) Add the Ionic.Zip dll through Add reference

2) Add the NameSpaces (using Ionic.Zip;)

In my previous post I show you how to make zip file. Now I will show you how to unzip that zip folder.

Code:
 protected void btnUnZip_Click(object sender, EventArgs e)
    {
        String TargetDirectory = "f:\\Zip\\myzip.zip";

        using (ZipFile zip1 = ZipFile.Read(TargetDirectory))
        {
            zip1.ExtractAll("f:\\UnZip\\myzip",
            Ionic.Zip.ExtractExistingFileAction.DoNotOverwrite);
        }
        File.Delete(TargetDirectory);  //Delete myzip.zip file. It is optional. If you want to delete
                                                         //then delete else don't delete.
    }


How to do zip in .Net? How to make zip file in .Net?

Definition: Make zip file.

Method:
1) Add the Ionic.Zip dll through Add reference

2) Add the NameSpaces (using Ionic.Zip;). 

Code: 

A) Following method will zip whole folder.

protected void btnZip_Click(object sender, EventArgs e)
    {
        string zipfilename = @"f:\\zip\\myzip.zip";  //zipped file name
        string filename = @"F:\\myfiles";         //folder to be zip

        using (ZipFile zip = new ZipFile())
        {
            zip.AddDirectory(filename);
            zip.Save(zipfilename);
        }
    }

Output:
myzip.zip folder at F: drive.

B) Following method will add selected file and folders from different path and add it to archive folder.
 
 protected void btnZip_Click(object sender, EventArgs e)
    {
        string zipfilename = @"f:\\unzip\\myzip.zip";   //zipped file name

        string filename = @"F:\\myfiles";                //folder to be zip

        using (ZipFile zip = new ZipFile())
        {
            //// add this map file into the "images" directory in the zip archive
            zip.AddFile("F:\\Food\\files\\0.png","images");

            //// add the report into a different directory in the archive
            zip.AddFile("F:\\Recipe", "Files");

            zip.AddDirectory("f:\\ticket", "Ticket");


            zip.Save(zipfilename);
        }
    }



Output:
myzip.zip folder at F: drive.
This zip folder will contain 3 folders i.e. images, Files and Ticket.

You run one by one each code and you can see difference between 2 methods.














Tuesday, 29 May 2012

How to prepare appam in kerala style? Appam recipe

Kerala Appam recipe

Ingredients for Appam recipe

2  cup(s) raw rice soaked for 4-5 hours
4  cups coconut shavings
1  cup(s) cooked rice
a pinch of yeast granules dissolved in some coconut water or little hot water
salt and sugar to taste

How to make Appam?

Drain the soaked rice and grind it along with the coconut shavings and cooked rice to a fine thick paste. Do not add too much water. Coconut water may be preferably used instead of water for grinding. Add the yeast and mix lightly. Mix in the salt and sugar to taste. Allow to ferment at room temperature for at least 6 hours.
Heat a small non-stick wok. Pour approximately half a cup of batter and quickly but gently swirl the pan around such that only a thin layer of the batter covers the sides and a thick layer collects at the bottom. Cover with a lid and cook each appam on medium heat for about 3 minute(s) or till the edges have become golden crisp and the centre is soft and spongy. Another sign of doneness would be when the edges start coming off the wok.


Serve it with vegetable stew or chicken stew or egg curry.

Friends I am sure that you will love this recipe. I love it. I prefer you to eat little bit hot.


Kerala appam
Appam


 

 

How to fetch TextArea value in .ashx file?

Definition: Fetch Textarea control value in .ashx file.

Example:
.Ashx file code:-

public void ProcessRequest (HttpContext context)
    {
        context.Response.ContentType = "text/html";
        try
        {
                  var comment = context.Request.Params["ctl00$ContentPlaceHolder1$txtComment"];                      context.Response.Write(comment);
         }
        catch { }
    }


Note:
contentplaceholder : because i have used Master Page.

Wednesday, 16 May 2012

How to send email in .Net?or how to send an email from a Gmail address using SMTP server?

Definition: SMTP Server

SMTP protocol is used for sending email . SMTP stands for Simple Mail Transfer Protocol. System.Net.Mail namespace is used for sending email . We can instantiate SmtpClient class and assign the Host and Port . The default port using SMTP is 25 , but it may vary different Mail Servers .
The Gmail SMTP server name is smtp.gmail.com and the port using send mail is 587 and also using NetworkCredential for password based authentication.

Following example shows how to send email in .Net? or How to send email by using your gmail email id?


Example:


using System.Net.Mail;
 
private void SendMail_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");

                mail.From = new MailAddress("your_gmail_id@gmail.com");
                mail.To.Add("To_emailid@xyz.com");
                mail.Subject = "Test Mail";
                mail.Body = "This is for testing SMTP mail from gmail.";

                SmtpServer.Port = 587;
  SmtpServer.Credentials=new System.Net.NetworkCredential("your_gmail_id@gmail.com", "your_gmail_id_password");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
                MessageBox.Show("Mail Send Successfully");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }



 

Thursday, 26 April 2012

How to convert string to number in javascript?

Definition: Convert String to Number.

The Number() function converts the object argument to a number that represents the object's value.
If the value cannot be converted to a legal number, NaN is returned.
In short,
Number() method will convert string to number.

Example A:
Javascript function
 
function add(a,b)
{
    var ans;
    var i = a;
    var j = b;
    ans = Number(a) + Number(b);
 
    alert("Your answer: " + ans);
}

 <div>
        <input type="text" id="txta" />
        <input type="text" id="txtb" />
        <input type="button" id="btnAdd" value="Addition" onclick="add(document.getElementByI('txta').value,document.getElementById('txtb').value);" />
          </div>
Output:



Example B:
<script type="text/javascript" language="javascript">

var test1= new Boolean(true);
var test2= new Boolean(false);
var test3= new Date();
var test4= new String("999");
var test5= new String("999 888");

document.write(Number(test1)+ "<br />");

document.write(Number(test2)+ "<br />");
document.write(Number(test3)+ "<br />");
document.write(Number(test4)+ "<br />");
document.write(Number(test5)+ "<br />");

</script> 

Output:
1
0
1335442904505
999
NaN