标题:
c# 操作FTP文件类(2)
[打印本页]
作者:
look_w
时间:
2019-4-17 18:34
标题:
c# 操作FTP文件类(2)
#region 在ftp上创建目录
public void MakeMultipleDir(string dirName)
{
if (dirName != "")
{
string[] dirs = dirName.Trim('/').TrimEnd('/').Split('/');
if (dirs.Length > 0)
{
string oldpath = ""; //文件夹完整路径
string nowdir = ""; //当前文件夹名称
bool b = true; //是否存在该文件夹
for (int i = 0; i < dirs.Length; i++)
{
b = true;
string[] list = GetFileList(oldpath.TrimEnd('/'));
if (list != null)
{
for (int z = 0; z < list.Length; z++)
{
if (i > 0)
nowdir = dirs[i - 1] + "/" + dirs[i];
else
nowdir = dirs[i];
if (list[z] == nowdir)
{
b = false;
break;
}
}
}
oldpath += dirs[i] + "/";
if (b)
MakeDir(oldpath.TrimEnd('/'));
}
}
else
{
MakeDir(dirName);
}
}
}
/// <summary>
/// 在ftp上创建目录
/// </summary>
/// <param name="dirName"></param>
public void MakeDir(string dirName)
{
try
{
string uri = "ftp://" + _serverIP + "/" + dirName;
Connect(uri);//连接
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
response.Close();
}
catch (Exception ex)
{
// MessageBox.Show(ex.Message);
}
}
#endregion
#region 删除ftp上目录
/// <summary>
/// 删除ftp上目录
/// </summary>
/// <param name="dirName"></param>
public void DelDir(string dirName)
{
try
{
string uri = "ftp://" + _serverIP + "/" + dirName;
Connect(uri);//连接
reqFTP.Method = WebRequestMethods.Ftp.RemoveDirectory;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
response.Close();
}
catch (Exception ex)
{
// MessageBox.Show(ex.Message);
}
}
#endregion
#region 获得ftp上文件大小
/// <summary>
/// 获得ftp上文件大小
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public long GetFileSize(string filename)
{
long fileSize = 0;
filename = filename.Replace("\\", "/");
try
{
string uri = "ftp://" + filename;
this.Connect(uri);//连接
reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
fileSize = response.ContentLength;
response.Close();
}
catch (Exception ex)
{
// MessageBox.Show(ex.Message);
}
return fileSize;
}
#endregion
#region 重命名对象
/// <summary>
/// 重命名对象
/// </summary>
/// <param name="filePath"></param>
/// <param name="newFilename"></param>
public void Rename(string filePath, string newFilename)
{
try
{
string uri = "ftp://" + _serverIP + "/" + filePath;
Connect(uri);//连接
reqFTP.Method = WebRequestMethods.Ftp.Rename;
reqFTP.RenameTo = newFilename;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
response.Close();
}
catch (Exception ex)
{
// MessageBox.Show(ex.Message);
}
}
#endregion
#region 获得文件明细
/// <summary>
/// 获得文件明细
/// </summary>
/// <returns></returns>
public string[] GetFilesDetailList()
{
return GetFileList("ftp://" + _serverIP + "/", WebRequestMethods.Ftp.ListDirectoryDetails);
}
/// <summary>
/// 获得文件明细
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public string[] GetFilesDetailList(string path)
{
path = path.Replace("\\", "/");
return GetFileList("ftp://" + _serverIP + "/" + path, WebRequestMethods.Ftp.ListDirectoryDetails);
}
#endregion
}
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0