Tuesday, February 24, 2009

How to download file from FTP server using C#

Add below namespaces,
using System.Net;
using System.IO;
using System.Text;

Create a button and add the below code under button click
//fileName = Name of the file to be downloaded from FTP server.
        string fileName = "test.txt";

        //filePath = The full path where the file is to be created. Here i put local system path.
        string filePath = "C:\\FTP";
        
        string ftpServerIP = "<IP Address>";
        string ftpUserID = "<User ID>";
        string ftpPassword = "<Password>";
        FtpWebRequest reqFTP;
        try
        {
            FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileName));
            reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
            reqFTP.UseBinary = true;
            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
            FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
            Stream ftpStream = response.GetResponseStream();
            long cl = response.ContentLength;
            int bufferSize = 2048;
            int readCount;
            byte[] buffer = new byte[bufferSize];
            readCount = ftpStream.Read(buffer, 0, bufferSize);
            while (readCount > 0)
            {
                outputStream.Write(buffer, 0, readCount);
                readCount = ftpStream.Read(buffer, 0, bufferSize);
            }
            ftpStream.Close();
            outputStream.Close();
            response.Close();
        }
        catch (Exception ex)
        {
           string errMsg= (ex.Message);
        }

Execute the code and see the C:\FTP folder for the file….

Monday, February 23, 2009

How to upload file to FTP server using C#

Add below namespaces,
using System.Net;
using System.IO;

Create a button and add the below code under button click
        string filename = "c:\\test.txt";
        string ftpServerIP = "<IP address>";
        string ftpUserID = "<userid>";
        string ftpPassword = "<Password>";

        FileInfo fileInf = new FileInfo(filename);

     string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;

        FtpWebRequest reqFTP;

        // Create FtpWebRequest object
        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));

        //Provide ftp userid and password
        reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

        // By default KeepAlive is true, where the control connection is not closed
        // after a command is executed.
        reqFTP.KeepAlive = false;

        //Specify the command to be executed. Here we use upload file command
        reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

        //data transfer type we use here is binary
        reqFTP.UseBinary = true;

        // Notify the server about the size of the uploaded file
        reqFTP.ContentLength = fileInf.Length;

        // The buffer size is set to 2kb
        int buffLength = 2048;

        byte[] buff = new byte[buffLength];

        int contentLen;

        // Opens a file stream (System.IO.FileStream) to read the file to be uploaded
        FileStream fs = fileInf.OpenRead();

        try
        {

            // Stream to which the file to be upload is written
            Stream strm = reqFTP.GetRequestStream();

            // Read from the file stream 2kb at a time
            contentLen = fs.Read(buff, 0, buffLength);

            // Till Stream content ends
            while (contentLen != 0)
            {

                // Write Content from the file stream to the FTP Upload Stream
                strm.Write(buff, 0, contentLen);
                contentLen = fs.Read(buff, 0, buffLength);
            }

            // Close the file stream and the Request Stream
            strm.Close();
            fs.Close();
        }
        catch (Exception ex)
        {
            string ErrMsg = ex.Message;
        }
Execute the code and check the FTP server for the file.

Friday, February 20, 2009

SQL Query to list all Users or Login Names from the database

This will be very useful when you want to know who all are having access to the database.
Execute the below stored procedure,
sp_helpuser 
out put of this stored procedure will be
UserName, GroupName, LoginName, DefDBName, UserID, SID

Thursday, February 12, 2009

SQL Query to find out a particular field name across all tables in a Database

SELECT name FROM sysobjects WHERE id IN 
(SELECT id FROM syscolumns WHERE name = '<colname>')
another simple query to return all the column names.

select table_name,column_name from information_schema.columns where column_name like '%xyz%'

Thursday, February 5, 2009

How to record voice from microphone using C#

   1:  using System;
   2:  using System.Data;
   3:  using System.Configuration;
   4:  using System.Web;
   5:  using System.Web.Security;
   6:  using System.Web.UI;
   7:  using System.Web.UI.WebControls;
   8:  using System.Web.UI.WebControls.WebParts;
   9:  using System.Web.UI.HtmlControls;
  10:   
  11:  //add below namespaces
  12:  using Microsoft.VisualBasic.Devices;
  13:  using Microsoft.VisualBasic;
  14:  using System.Runtime.InteropServices;
  15:   
  16:  //Add reference to "Microsoft.VisualBasic" which is
       available in .NET tab
  17:   
  18:  public partial class _Default : System.Web.UI.Page 
  19:  {
  20:     protected void Page_Load(object sender, EventArgs e)
  21:      {
  22:   
  23:      }
  24:      //Add the below API. winmm.dll is a module for the Windows Multimedia API, which contains low-level audio and joystick functions
  25: [DllImport("winmm.dll", EntryPoint = "mciSendStringA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
  26:      
  27:      //The mciSendString function sends a command
           string to an MCI device. 
  28:      //MCI devices are drivers that provide Windows- 
           based programs 
  29:      //device-independent capabilities for controlling
           multimedia hardware and software.
  30:      private static extern int mciSendString
  31:          (string lpstrCommand, string 
      lpstrReturnString, int uReturnLength, int hwndCallback);
  32:   
  33:      protected void  btnRecord_Click(object sender,
           EventArgs e)
  34:  {
  35:      mciSendString("open new Type waveaudio Alias
          TestSoundRecord", "", 0, 0);
  36:      mciSendString("record TestSoundRecord", "", 0, 0);
  37:  }
  38:  protected void  btnStopSave_Click(object sender,
       EventArgs e)
  39:  {
  40:      mciSendString("save TestSoundRecord
           c:\\recorded.wav", "", 0, 0);
  41:      mciSendString("close TestSoundRecord ", "", 0, 0);
  42:      Computer c = new Computer(); 
  43:      c.Audio.Stop();
  44:  }
  45: protected void btnRead_Click(object sender, EventArgs e)
  46:  {
  47:      Computer computer = new Computer(); 
  48:      computer.Audio.Play("C:\\recorded.wav",
           AudioPlayMode.Background);
  49:  }
  50:  }

recorded.wav will be a continuous file. you can create new file each time you click stop and save.