Click here to Skip to main content
15,891,529 members
Articles / General Programming / Internet

File Transfer Protocol (FTP) Client

Rate me:
Please Sign up or sign in to vote.
4.83/5 (20 votes)
1 Dec 2011CPOL7 min read 269.3K   26.6K   81  
Connect to FTP server, and download, upload, rename, and delete files or directories.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Drawing;
using System.IO;

namespace FTPClient
{
    public static class FileIconLoader
    {
        private const uint SHGFI_ICON = 0x100;
        private const uint SHGFI_LARGEICON = 0x0;
        private const uint SHGFI_SMALLICON = 0x1;
        private const uint SHGFI_USEFILEATTRIBUTES = 0x10;
        private const uint FILE_ATTRIBUTE_NORMAL = 0x80;
        [DllImport("shell32.dll")]
        private static extern IntPtr SHGetFileInfo(string pszPath,
        uint dwFileAttributes,
        ref SHFILEINFO psfi,
        uint cbSizeFileInfo,
        uint uFlags);
        public static Icon GetFileIcon(string fileName, bool largeIcon)
        {
            string extension = Path.GetExtension(fileName);
            fileName = "*" + extension; // just use wildcard in case file doesn't exist. 

            SHFILEINFO shinfo = new SHFILEINFO();
            IntPtr hImg;
            if (largeIcon)
            {
                hImg = SHGetFileInfo(fileName, FILE_ATTRIBUTE_NORMAL, ref shinfo,
                (uint)Marshal.SizeOf(shinfo),
                SHGFI_ICON |
                SHGFI_LARGEICON |
                SHGFI_USEFILEATTRIBUTES);
            }
            else
            {
                hImg = SHGetFileInfo(fileName, FILE_ATTRIBUTE_NORMAL, ref shinfo,
                (uint)Marshal.SizeOf(shinfo),
                SHGFI_ICON |
                SHGFI_SMALLICON |
                SHGFI_USEFILEATTRIBUTES);
            }
            try
            {
                return Icon.FromHandle(shinfo.hIcon);
            }
            catch
            { 
                return null;
            }
        }
    }
    [StructLayout(LayoutKind.Sequential)]
    public struct SHFILEINFO
    {
        public IntPtr hIcon;
        public IntPtr iIcon;
        public uint dwAttributes;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
        public string szDisplayName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
        public string szTypeName;
    }; 
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Team Leader
Turkey Turkey
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions