Wednesday, August 14, 2013

Upload file into FTP

this is example code for upload file into FTP site.

 using (WebClient client = new WebClient())
            {
                byte[] ByteStream = ConvertFileIntoBtes("c:\\test.jpg");

                if (ByteStream != null)
                {
                    client.Credentials = new System.Net.NetworkCredential(USERNAME, PASSWORD);
                    client.UploadData(FTPSERVER + "/test.jpg", "STOR", ByteStream);
                }
            }


Read more!!!

Function for Resize Image

this is a function that resizes the image into given size.

 private static Image resizeImage(Image imgToResize, Size size)
        {
            int sourceWidth = imgToResize.Width;
            int sourceHeight = imgToResize.Height;

            float nPercent = 0;
            float nPercentW = 0;
            float nPercentH = 0;

            nPercentW = ((float)size.Width / (float)sourceWidth);
            nPercentH = ((float)size.Height / (float)sourceHeight);

            if (nPercentH < nPercentW)
                nPercent = nPercentH;
            else
                nPercent = nPercentW;

            int destWidth = (int)(sourceWidth * nPercent);
            int destHeight = (int)(sourceHeight * nPercent);

            Bitmap b = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (Image)b;
        }


Read more!!!

Connection strings for MS Access and MS Excel in C#

Connecttion strings for

MS ACCESS
- OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=electiondb.accdb;Persist Security Info=False;");

EXCEL
OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=election.xlsx;Extended Properties='Excel 8.0;HDR=NO;IMEX=1';");


Read more!!!

Get file names from FTP site

this example is how to get list of file names on specific ftp site and bind the result to RadListView with two column.
I used RadListView of Telerik. You can use standard  ListView or ASP.

public void ListDirectory()
        {
            var request = createRequest(URL, WebRequestMethods.Ftp.ListDirectory);

            DataTable DT = new DataTable();
            DataColumn col1 = new DataColumn("URL");
            DataColumn col2 = new DataColumn("FILENAME");
            DT.Columns.Add(col1);
            DT.Columns.Add(col2);
            DT.Rows.Clear();

            using (var response = (FtpWebResponse)request.GetResponse())
            {
                using (var stream = response.GetResponseStream())
                {
                    using (var reader = new StreamReader(stream, true))
                    {
                        while (!reader.EndOfStream)
                        {
                            string line = reader.ReadLine();
                            DT.Rows.Add(URL + line, line);
                        }
                    }
                }
            }

            this.listView.DataSource = DT;
        }
private FtpWebRequest createRequest(string uri, string method)
        {
            var r = (FtpWebRequest)WebRequest.Create(uri);

            r.Credentials = new NetworkCredential(USERNAME, PASSWORD);
            r.Method = method;

            return r;
        }


Read more!!!