要实现一个CS结构的文件管理系统,项目要求Vs2005 C# 2.0 开发,winForm 窗体模式,客户端和Ftp服务器都放在局域网内;
我用一个TreeView遍历到服务器的所有文件夹,以树状形式存放到TreeView中,然后选中节点后自动添加到右边的
一个ListView中,如下图:
在开发的过程中有几个问题不能解决,希望大家帮忙指教:1、我现在之是遍历到本地磁盘的文件,怎么连接到服务器遍历到服务器的。
2、怎么获取选中的TreeView节点下文件的大小、并添加到ListView中。
3、能给点源代码或者项目简单实例更好,希望大侠们帮帮忙。感激不尽...!
我用一个TreeView遍历到服务器的所有文件夹,以树状形式存放到TreeView中,然后选中节点后自动添加到右边的
一个ListView中,如下图:
在开发的过程中有几个问题不能解决,希望大家帮忙指教:1、我现在之是遍历到本地磁盘的文件,怎么连接到服务器遍历到服务器的。
2、怎么获取选中的TreeView节点下文件的大小、并添加到ListView中。
3、能给点源代码或者项目简单实例更好,希望大侠们帮帮忙。感激不尽...!
根据共享目录
DirectoryEntry root = new DirectoryEntry("WinNT:");
DirectoryEntries domains = root.Children;
domains.SchemaFilter.Add("domain");
foreach (DirectoryEntry domain in domains) {
IPHostEntry iphe = null;
try {
iphe = Dns.GetHostByName(computer.Name);
}
catch { }
}foreach(TreeNode node in node.ChildNodes){}
http://www.codeproject.com/KB/miscctrl/FileBrowser.aspx
http://www.codeproject.com/KB/IP/FtpClient.aspx
剩下的自己写啦.
利用file 的size
比如,你发送一个获取服务器中某个文件夹内内容的请求,服务器端接收请求后将文件夹内文件内容全部拼凑成字符串,再发送回客户端,客户端收到应答后,解析字符串内容,再进行处理就OK了
这个就是一般FTP服务端和客户端的交流方式。
http://www.codeproject.com/KB/IP/win7ftp.aspx
有源码
难点1、删除文件夹时,如果文件夹内有子文件或子文件夹,程序报错?
难点2、怎么查看文件内容,如下图的 文本文件、word文件、excel文件、图片文件?
难点3、批量的文件上传或下载;或者直接拖放,可多选?
难点2、怎么直接查看Ftp服务器上的文件内容,如下图的 文本文件.txt、word文件.doc、excel文件.xls、图片文件.jpg?
是在Ftp服务器上实现呀,不是本地目录!
循环递归,挨个删除,例如
private void DoDeleteDirs(string _Path, FTPclient _FTPclient)
{
FTPdirectory _FTPdirectory = _FTPclient.ListDirectoryDetail(_Path);
foreach (FTPfileInfo file in _FTPdirectory)
{
if (string.IsNullOrEmpty(file.Filename) || file.Filename.Equals(".") || file.Filename.Equals(".."))
{
continue;
}
if (file.FileType == FTPfileInfo.DirectoryEntryTypes.Directory)
{
if (!_FTPclient.FtpDeleteDirectory(_Path + "/" + file.Filename))
{
DoDeleteDirs(_Path + "/" + file.Filename, _FTPclient);
_FTPclient.FtpDeleteDirectory(_Path + "/" + file.Filename);
}
}
}
} private void DoDeleteFiles(string _Path, FTPclient _FTPclient, bool _IsFile)
{ FTPdirectory _FTPdirectory = _FTPclient.ListDirectoryDetail(_Path);
foreach (FTPfileInfo file in _FTPdirectory)
{
if (string.IsNullOrEmpty(file.Filename) || file.Filename.Equals(".") || file.Filename.Equals(".."))
{
continue;
}
if (file.FileType == FTPfileInfo.DirectoryEntryTypes.File)
{
if (_IsFile)
_FTPclient.FtpDelete(_Path);
else
_FTPclient.FtpDelete(_Path + "/" + file.Filename);
}
else
{
DoDeleteFiles(_Path + "/" + file.Filename, _FTPclient, false); ;
}
}
}
难点2、怎么查看文件内容,如下图的 文本文件、word文件、excel文件、图片文件?
你的FtpClient可以从Server得到文件的类型,你自己再根据类型选择图片并显示.
难点3、批量的文件上传或下载;或者直接拖放,可多选?
拖放需要设置控件的AllowDrop属性为true
然后写DragEnter和DragDrop事件例如
private void DoDragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop) || e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Copy;
}
else
{
e.Effect = DragDropEffects.None;
}
}private void DoDragDrop(object sender, DragEventArgs e)
{
//得到拖入的文件路径,可以是多个路径
string[] _Files = (string[])e.Data.GetData(DataFormats.FileDrop);
//判断拖入的Path中是否包含有文件夹,如果有,依次递归遍历得到所有的文件路径,空文件夹则直接忽略
//最后将处理过的文件路径依次上传.
}
如需查看文件内容,只能下载下来,再自动打开查看
求解!
{
FileInfo fileInf = new FileInfo(filename);
string uri = "ftp://" + ftpServerIP + "/" + fileInf.Name;
FtpWebRequest reqFTP; // Create FtpWebRequest object from the Uri provided
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + strPath + "/" + fileInf.Name)); // Provide the WebPermission Credintials
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 = true; // Specify the command to be executed.
reqFTP.Method = WebRequestMethods.Ftp.UploadFile; // Specify the data transfer type.
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)
{
MessageBox.Show(ex.Message, "Upload Error");
}
}