Wednesday, July 28, 2010

C#.NET FTP File Upload

C#.NET gives the namespace to upload Files to FTP System.NET following custom method can be use to upload a file in to a FTP Server
public static void UploadToFTP(string FTPAddress, string FileName, string FullPath, string UserName, string Password)
{
       /*
        * FTPAddress - URL or IP address of the FTP server
        * FileName - Name of the File which is going to upload
        * FullPath - You have to give the Absolute path to the File you are going to upload
        * UserName - User name for the FTP account
        * Password - FTP Password
        */
System.Net.FtpWebRequest objWebRequest = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(string.Format("{0}/{1}", FTPAddress, FileName));

if (objWebRequest != null)
{
objWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
objWebRequest.Credentials = new System.Net.NetworkCredential(UserName, Password);
objWebRequest.UsePassive = true;
objWebRequest.UseBinary = true;
objWebRequest.KeepAlive = false;

byte[] FileBuffer = null;
//Load The File To Mememory

try
{
   //open and Read the File in To the Memory
   using (System.IO.FileStream objFStream = System.IO.File.OpenRead(FullPath))
  {
     FileBuffer = new byte[objFStream.Length];

    objFStream.Read(FileBuffer, 0, FileBuffer.Length);
    objFStream.Close();
  }

    //Upload File To the FTP
    using (System.IO.Stream objStream = objWebRequest.GetRequestStream())
   {
       if (FileBuffer != null)
       {
           objStream.Write(FileBuffer, 0, FileBuffer.Length);
           objStream.Close();
       }
    }
}
 catch (Exception Ex)
 {
    throw Ex;
 }
}


}

Did you get this Error Exception? “System.Security.SecurityException: Request for the permission of type 'System.Net.WebPermission, …..” Then your have to read this Post too

You will be able to download a good windows application to the this purpose from http://www.vcskicks.com/download-file-ftp.php that was really help full to me thanks should goes to the people who ever wrote that article






Tuesday, July 27, 2010

FTP File Download C#.NET

You can download files from FTP using following custom Method
public static string FTPDownload(string FTPURL, string FileName, string SavingPathonServer, string UserName, string Password)
{
/*
* FTPAddress - URL or IP address of the FTP server
* FileName - Name of the File which is going to upload
* SavingPathonServer - This method will save Specifide file in to the WebServer you have to give the path to save on web server
* UserName - User name for the FTP account
* Password - FTP Password
*/

byte[] bteDownloadedData = new byte[0];
string strServerPathToFile = string.Empty;
try
{

System.Net.FtpWebRequest objWebRequest = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(string.Format("{0}/{1}", FTPURL, FileName));
objWebRequest.Method = System.Net.WebRequestMethods.Ftp.GetFileSize;
objWebRequest.Credentials = new System.Net.NetworkCredential(UserName, Password);
objWebRequest.UsePassive = true;
objWebRequest.UseBinary = true;
objWebRequest.KeepAlive = true;

int intDataLength = (int)objWebRequest.GetResponse().ContentLength;


objWebRequest = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(string.Format("{0}/{1}", FTPURL, FileName));
objWebRequest.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;
objWebRequest.Credentials = new System.Net.NetworkCredential(UserName, Password);
objWebRequest.UsePassive = true;
objWebRequest.UseBinary = true;
objWebRequest.KeepAlive = false;

System.Net.FtpWebResponse objFTPWebResponse =(System.Net.FtpWebResponse)objWebRequest.GetResponse();
System.IO.Stream objStreamReader = objFTPWebResponse.GetResponseStream();

System.IO.MemoryStream objMemStream = new System.IO.MemoryStream();
byte[] memBuffer = new byte[1024];

int intBytesRead = 0;

while (true)
{
intBytesRead = objStreamReader.Read(memBuffer, 0, memBuffer.Length);
if (intBytesRead != 0)
{
objMemStream.Write(memBuffer, 0, intBytesRead);
}
else
{
break;
}

}

bteDownloadedData = objMemStream.ToArray();
objStreamReader.Close();
objMemStream.Close();
objFTPWebResponse.Close();

if (bteDownloadedData != null && bteDownloadedData.Length != 0)
{
System.IO.FileStream objFileStream = new System.IO.FileStream(SavingPathonServer, System.IO.FileMode.Create);
objFileStream.Write(bteDownloadedData, 0, bteDownloadedData.Length);
objFileStream.Close();
}
return SavingPathonServer;

}
catch (Exception Ex)
{
throw Ex;
}

}


}
}

Did you get this Error Exception? “System.Security.SecurityException: Request for the permission of type 'System.Net.WebPermission, …..” Then your have to read this Post too

Your will be able to download a good windows application to the this purpose from http://www.vcskicks.com/csharp_ftp_upload.php that was really help full to me thanks should goes to the people who ever wrote that article




Friday, March 19, 2010

IT Professionals....

 I just found this from my in box. That brings smile in to my face.. hope it will happen for you too... 
And... you will think about it twice like I did :)

1. We work weird (night) shifts...
    Like prostitutes.

2. They pay you to make the client happy...
    Like a prostitute.

3. The client pays a lot of money, but your employer keeps almost
    every penny...
    Like a prostitute.

4. You are rewarded for fulfilling the client's dreams...
    Like a prostitute.

5. Your friends fall apart and you end up hanging out with people in the
same profession as you...
Like a prostitute.

6. When you have to meet the client you always have to be perfectly
groomed...
Like a prostitute.

7. But when you go back home it seems like you are coming back from hell...
Like a prostitute.

8. The client always wants to pay less but expects incredible things
    from you...
    Like a prostitute.

9. When people ask you about your job, you have difficulties to explain
    it...
    Like a prostitute.

10. Everyday when you wake up, you say: "I'm not going to spent the
     rest of my life doing this."
    Like a prostitute ........


Read Data From a Excel Sheet , C# .NET

Reading data from a excel sheet is a simple task through the .NET. Following method will return a Data Table after reading the data from specified Sheet of the Excel sheet.
If you are going to select several columns from the sheet , then all the specifying column names needs to be map with the columns of the excel sheet. In the following example I just used the Select * (all).
Using any file uploading Control get the File Name and the Extension of the file.
  •  I have use the variable names strFileName, strExtension for File name and file extension respectively.
  • Sheet1 is the name of the excel sheet which is containing the required data in the specified Excel file
public DataTable ReadExcelToDataTable(string FileName, string Extention)
{
   string strConnectionString = string.Empty;
   if (Extention == "xls")//For Office 2003 version
   {
     strConnectionString =                     string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=   {0};Extended Properties=Excel 8.0", FileName);
   }
     else if (Extention=="xlsx")//for Office 2007
   {
    strConnectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0", FileName);
   }
     DataTable dttExecelDataTable = new DataTable();
     if (!string.IsNullOrEmpty(strConnectionString))
     {
         System.Data.OleDb.OleDbConnection objOleDBCon = new System.Data.OleDb.OleDbConnection(strConnectionString);
        try
        {
          objOleDBCon.Open(); // open excel file.
          System.Data.OleDb.OleDbCommand objDBCommand = new System.Data.OleDb.OleDbCommand("SELECT * FROM [Sheet1$]", objOleDBCon);
          System.Data.OleDb.OleDbDataAdapter objOleAdapter = new System.Data.OleDb.OleDbDataAdapter (objDBCommand);
                    objOleAdapter.Fill(dttExecelDataTable);
        }
        catch (Exception)
        {
         throw;
        }
      }
    return dttExecelDataTable;

 }






Friday, December 18, 2009

Insert Categories to your BlogSpot

Inserting Categories is not included in the blogger.com but they are providing various alternatives for that. Labels are one of those solutions. I saw various developers and bloggers gave several solutions for it.
But I couldn’t find a solutions which suits for me. So I decided to do it in my own way. That was simple.
Steps are as follows
  1. Fist you have to Add HTML/JavaScrtipt gadget to your layout. (I hope it will be a easy task to add html/javascript gadget to the blog so I don't going to explain that here).
  2. Then I add simple html table and script section as follows




In the href of the link tag past the link to your blog post. Blogger is creating separate HTML page for your each post. Hence you can paste that page url in the tag. (link will be available in the post list which is ordered according to the posted date).


One thing of this solution is you have to add new table row to this table each time you create a post.
But according to my requirement I'm ready for do it :)

Cheers....!!!






Monday, December 14, 2009

Send Emails Using Gmail As SMTP Server


GmailSmtp

We can use gmail as our SMTP server for .NET development purposes.
C#.Net Code for send mail as follows 
Main thing is you need to define the Port 587 and set  Enable the SSL for SmtpClient

int Port = 587;
string MailServer = "smtp.gmail.com";
string UserName = "gmailUserName";
string Password = "gmailPassword";

System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
mailMessage.From = new System.Net.Mail.MailAddress("FromAddress");
mailMessage.To.Add(new System.Net.Mail.MailAddress("ToAddress"));

mailMessage.Subject = "Subject";
mailMessage.Body = "Message";
mailMessage.IsBodyHtml = true;

System.Net.Mail.SmtpClient smtpClient = new System.Net.Mail.SmtpClient(MailServer, Port);

smtpClient.Credentials = new System.Net.NetworkCredential(UserName, Password);
smtpClient.EnableSsl = true;

try
 {
    smtpClient.Send(mailMessage);

 }
catch (Exception Ex)
 {
      string str = Ex.Message;

 }







Wednesday, November 11, 2009

Google Wave


Google Wave

When I see the Developer preview video in the about the Google Wave. I just said WOW. It's an amazing work done by Google.
Nobody needs to Update their blog, images all separately. They only have just let the Google Wave to do it . We do not need to wait other person to press the Enter key when we are in the online conversation Wave will just let us know what he/she is typing word by word (no more messages like "she is typing a message").

I can not wait to use this great application.

Yes Finally I got access to the Wave now I'm Waving with Google Wave.... in addition to that I got some invitations tooo.

that's cool (for me.... ;) )