📄 formftp.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Net;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
namespace Ftp
{
public partial class FormFtp : Form
{
public FormFtp()
{
InitializeComponent();
}
//在shell32.dll导入函数SHGetFileInfo
[DllImport("shell32.dll", EntryPoint = "SHGetFileInfo")]
public static extern int GetFileInfo(string pszPath, int dwFileAttributes,
ref FileInfomation psfi, int cbFileInfo, int uFlags);
//定义SHFILEINFO结构(名字随便起,这里用FileInfomation)
[StructLayout(LayoutKind.Sequential)]
public struct FileInfomation
{
public IntPtr hIcon;
public int iIcon;
public int dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
}
//定义文件属性标识
public enum FileAttributeFlags : int
{
FILE_ATTRIBUTE_READONLY = 0x00000001,
FILE_ATTRIBUTE_HIDDEN = 0x00000002,
FILE_ATTRIBUTE_SYSTEM = 0x00000004,
FILE_ATTRIBUTE_DIRECTORY = 0x00000010,
FILE_ATTRIBUTE_ARCHIVE = 0x00000020,
FILE_ATTRIBUTE_DEVICE = 0x00000040,
FILE_ATTRIBUTE_NORMAL = 0x00000080,
FILE_ATTRIBUTE_TEMPORARY = 0x00000100,
FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200,
FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400,
FILE_ATTRIBUTE_COMPRESSED = 0x00000800,
FILE_ATTRIBUTE_OFFLINE = 0x00001000,
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000,
FILE_ATTRIBUTE_ENCRYPTED = 0x00004000
}
//定义获取资源标识
public enum GetFileInfoFlags : int
{
SHGFI_ICON = 0x000000100, // get icon
SHGFI_DISPLAYNAME = 0x000000200, // get display name
SHGFI_TYPENAME = 0x000000400, // get type name
SHGFI_ATTRIBUTES = 0x000000800, // get attributes
SHGFI_ICONLOCATION = 0x000001000, // get icon location
SHGFI_EXETYPE = 0x000002000, // return exe type
SHGFI_SYSICONINDEX = 0x000004000, // get system icon index
SHGFI_LINKOVERLAY = 0x000008000, // put a link overlay on icon
SHGFI_SELECTED = 0x000010000, // show icon in selected state
SHGFI_ATTR_SPECIFIED = 0x000020000, // get only specified attributes
SHGFI_LARGEICON = 0x000000000, // get large icon
SHGFI_SMALLICON = 0x000000001, // get small icon
SHGFI_OPENICON = 0x000000002, // get open icon
SHGFI_SHELLICONSIZE = 0x000000004, // get shell size icon
SHGFI_PIDL = 0x000000008, // pszPath is a pidl
SHGFI_USEFILEATTRIBUTES = 0x000000010, // use passed dwFileAttribute
SHGFI_ADDOVERLAYS = 0x000000020, // apply the appropriate overlays
SHGFI_OVERLAYINDEX = 0x000000040 // Get the index of the overlay
}
private string GetTypeName(string fileName)
{
FileInfomation fileInfo = new FileInfomation(); //初始化FileInfomation结构
//调用GetFileInfo函数,最后一个参数说明获取的是文件类型(SHGFI_TYPENAME)
int res = GetFileInfo(fileName, (int)FileAttributeFlags.FILE_ATTRIBUTE_NORMAL,
ref fileInfo, Marshal.SizeOf(fileInfo), (int)GetFileInfoFlags.SHGFI_TYPENAME);
return fileInfo.szTypeName;
}
private void btnUpLoad_Click(object sender, EventArgs e)
{
string sUrl = textBoxUrl.Text;
string sUser = textBoxUser.Text;
string sPw = textBoxPw.Text;
// int i = FileListBox.Items.Count;
for (int i = 0; i < FileListBox.Items.Count; i++)
{
try
{
FileInfo myFileInfo = new FileInfo(FileListBox.Items[i].ToString());
// string fileTypeName = GetTypeName((string)FileListBox.Items[i].ToString());//文件类型
WebClient myWebClient = new WebClient();
myWebClient.Credentials = new NetworkCredential(sUser, sPw);
myWebClient.UploadFileAsync(new Uri(sUrl + "/" + myFileInfo.Name), (string)FileListBox.Items[i].ToString());
myWebClient.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void FormFtp_Load(object sender, EventArgs e)
{
DiskDriveComboBox.Items.AddRange(Directory.GetLogicalDrives());
}
private void DiskDriveComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (DiskDriveComboBox.SelectedIndex != -1)
{
try
{
FileListBox.DataSource = null;
DirectoryListBox.DataSource = null;
string[] subdirectoryEntries = Directory.GetDirectories((string)DiskDriveComboBox.SelectedItem);
DirectoryListBox.DataSource = subdirectoryEntries;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
DirectoryListBox.DataSource = null;
}
}
}
private void DirectoryListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (DirectoryListBox.SelectedIndex != -1)
{
try
{
FileListBox.DataSource = Directory.GetFiles((string)DirectoryListBox.SelectedItem);
}
catch
{
MessageBox.Show("您没有权限来列示此目录。");
return;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
string sUrl = textBoxUrl.Text;
string sUser = textBoxUser.Text;
string sPw = textBoxPw.Text;
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(sUrl);
request.Credentials = new NetworkCredential(sUser, sPw);
request.Method = WebRequestMethods.Ftp.ListDirectory;
try
{
FtpWebResponse resp = (FtpWebResponse)request.GetResponse();
Stream data = resp.GetResponseStream();
StreamReader reader = new StreamReader(data, Encoding.Default);
while (reader.Peek() > 0)
{
try
{
string sData = reader.ReadLine();
WebClient myWebClient = new WebClient();
myWebClient.BaseAddress = sUrl;
myWebClient.Credentials = new NetworkCredential(sUser, sPw);
byte[] myDatabuffer = myWebClient.DownloadData(sUrl + "/" + sData);
FileStream objStream = new FileStream((string)DirectoryListBox.SelectedItem + "\\" + sData, FileMode.Create, FileAccess.Write);
objStream.Write(myDatabuffer, 0, myDatabuffer.Length);
objStream.Close();
FtpWebRequest request1 = (FtpWebRequest)WebRequest.Create(sUrl + "/" + sData);
request1.Credentials = new NetworkCredential(sUser, sPw);
request1.Method = WebRequestMethods.Ftp.DownloadFile;
myWebClient.Dispose();
}
catch (Exception e1)
{
MessageBox.Show(e1.Message);
}
}
resp.Close();
}
catch (Exception e2)
{
MessageBox.Show(e2.Message);
}
MessageBox.Show("OK");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -