/// <summary>
/// 下载函数
/// </summary>
/// <param name="client">Sockets套接字</param>
/// <param name="LocalFilePath">服务器端本地的目标文件路径(包含文件名)</param>
/// <param name="isFileExists">返回该目录下是否存在该文件</param>
/// <param name="TransFeredFileSize">返回该文件的大小</param>
/// <returns>传输是否成功</returns>
private bool download(Socket client,string LocalFilePath,out long offSet)
{
int bytes;
//isFileExists = false;
long TransFeredFileSize = 0;
bool isException = false;
Byte[] buffer = new Byte[1024];
string FileName = retFileName(LocalFilePath).Trim();
if(File.Exists(rc1.StaticPath + "\\" + FileName))
{
//isFileExists = true;
//偏移量 文件存储的开始 索引
FileInfo localFile = new FileInfo(rc1.StaticPath + "\\" + FileName);
offSet = TransFeredFileSize = localFile.Length;
//
localFile.Delete();
Stream st = File.Create(rc1.StaticPath + "\\" + FileName);
st.Close();
///
FileStream output = new FileStream(rc1.StaticPath + "\\" + FileName,FileMode.Open);
//offSet =TransFeredFileSize;
offSet = 0;
output.Seek(offSet,SeekOrigin.Begin);
try
{
byte[] byte1 = new byte[1];
byte1[0] = 2;
client.Send(byte1);
while(true)
{
bytes = client.Receive(buffer, buffer.Length, 0);
//为支持蓄传功能 从文件的大小处开始前移 52 个字节
//output.Write(buffer,TransFeredFileSize - 52,bytes);
output.Write(buffer,0,bytes);
if(bytes <=1023)
{
break;
}
}
if(client.Connected)
isException = true;
else
isException = false;
output.Close();
}
catch(Exception ex2)
{}
finally
{
if(!output.CanWrite)
output.Close();
}
}
else
{
offSet = 0;
Stream st = File.Create(rc1.StaticPath + "\\" + FileName);
st.Close();
FileStream output = new FileStream(rc1.StaticPath + "\\" + FileName,FileMode.Open);
try
{
byte[] byte1 = new byte[1];
byte1[0] = 2;
client.Send(byte1);
while(true)
{
bytes = client.Receive(buffer, buffer.Length, 0);
output.Write(buffer,0,bytes);
if(bytes <=1023)
{
break;
}
}
if(client.Connected)
isException = true;
else
isException = false;
output.Close();
}
catch(Exception ex3)
{}
finally
{
if(!output.CanWrite)
output.Close();
}
}
if(isException)
return true;
else
return false;
}
public ArrayList dirAccess(Socket client,string sCommand,out bool IsSuccess)
{
IsSuccess = false;
ArrayList ArrayResult;
//=================续传用
//==================
string command;//命令
string command2;
int CommandTag;//命令标记
int index;
CommandTag = this.RetCommandTag(sCommand);
switch(CommandTag)
{
#region dir
case 0://dir
directoryInfo = new DirectoryInfo(rc1.StaticPath);
if(rc1.StaticPath.Trim()=="")
rc1.StaticPath = directoryInfo.FullName;
ArrayResult = new ArrayList();
ArrayResult.Add("=================");
ArrayResult.Add("当前服务器路径为: " + rc1.StaticPath);
ArrayResult.Add("=================");
ArrayResult.AddRange(RetPathInfo(rc1.StaticPath));
ArrayResult.AddRange(this.RetFileInfo(rc1.StaticPath));
IsSuccess = true;
return ArrayResult;
//break;
#endregion
#region copy
case 1://copy
ArrayResult = new ArrayList(5);
string[] arrStr = sCommand.Split(' ');
command2 = arrStr[2];
command = arrStr[1];
DirectoryInfo directoryInfo2 = new DirectoryInfo(command2);
directoryInfo = new DirectoryInfo(command);
try
{
if(command.IndexOf(".")!=-1)//如果指令中移动的是文件
{
if(!File.Exists(command2 + "\\" + command))//如果目标目录已存在该文件
{
if(File.Exists(rc1.StaticPath + "\\" + command))//如果源文件存在
{
File.Copy(rc1.StaticPath + "\\" + command,command2 + "\\" + command ,true);
ArrayResult.Add("成功! 拷贝指定 文件");
}
else
ArrayResult.Add("找不到要移动的文件!");
}
else
{
//File.Copy(command,command2,true);
ArrayResult.Add("移动失败,该文件已存在!");
}
}
else
{
if(!directoryInfo2.Exists)//如果目标目录不存在
{
if(directoryInfo.Exists)
{
if(this.CopyDir(command2,command))
ArrayResult.Add("成功! 移动指定 目录");
else
ArrayResult.Add("失败 移动指定 目录");
}
}
else ArrayResult.Add("目标目录已存在!");
}
}
catch(Exception ex)
{
ArrayResult.Add("移动指定目录/文件失败!\r\n" + ex.Message);
}
return ArrayResult;
//break;
#endregion
#region del
case 2://del
command = "";
ArrayResult = new ArrayList(5);
int index3;
index3 = sCommand.IndexOf(" ");
command = sCommand.Substring(index3 + 1);
directoryInfo = new DirectoryInfo(rc1.StaticPath+"\\" + command);
command = rc1.StaticPath + "\\" + command;
try
{
if(File.Exists(command))
{
File.Delete(command);
ArrayResult.Add("成功! 删除指定 文件");
}
else if(directoryInfo.Exists)
{
directoryInfo.Delete(true);
ArrayResult.Add("成功! 删除指定目录");
}
else ArrayResult.Add("失败! 指定文件或路径不存在!");
}
catch(Exception ex)
{
ArrayResult.Add("删除指定目录/文件失败!\r\n" + ex.Message);
}
return ArrayResult;
//break;
#endregion
#region cd
case 3://cd c:\aa\bb\cc
index = 0;
command = "";
command = sCommand.Substring(3);
if(command == "..")
{
index = rc1.StaticPath.LastIndexOf("\\");
rc1.StaticPath = rc1.StaticPath.Substring(0,index);
}
else
{
rc1.StaticPath = rc1.StaticPath +"\\"+ command;
}
ArrayResult = new ArrayList(1);
ArrayResult.Add("成功! 初始目录已设置为: " + rc1.StaticPath);
return ArrayResult;
//break;
#endregion
#region setdir
case 4://setdir
//cd c:\aa\bb\cc
string str = sCommand.Substring(7);
if(str.LastIndexOf("\\") != (str.Length - 1))
str += "\\";
rc1.StaticPath = str;
ArrayResult = new ArrayList(1);
ArrayResult.Add("成功! 初始目录已设置为: " + rc1.StaticPath);
IsSuccess = true;
return ArrayResult;
//break;
#endregion
#region update
case 5 :// update
//update 001.txt
//update 函数参数组成:
string[] arrStr1 = sCommand.Split(' ');
long offSet;
if(!this.download(client,arrStr1[1],out offSet))
{
ArrayResult = new ArrayList(3);
ArrayResult.Add("下载文件 失败");
return ArrayResult;
}
else
{
ArrayResult = new ArrayList(1);
ArrayResult.Add("成功! 下载文件");
return ArrayResult;
}
//break;
#endregion
#region get
case 6 :// get
//download 001.txt
//download 函数参数组成:0参数 : 不需要客户端的路径,只需在参数中输入服务器 当前路径的 文件名即可
command = "";
command = sCommand.Substring(4);
arrStr1 = command.Split(' ');
//if(!this.upload(client,rc1.StaticPath + "\\" + arrStr1[0].ToString(),out isExists,out TransFeredFileSize))
if(!this.upload(client,rc1.StaticPath + "\\" + arrStr1[0].ToString(),0))
{
ArrayResult = new ArrayList(1);
//if(isExists)
// ArrayResult.Add("该文件已存在");
//ArrayResult.Add("且该文件大小为:" + TransFeredFileSize.ToString());
ArrayResult.Add("上传文件失败");
}
else
{
ArrayResult = new ArrayList(1);
ArrayResult.Add("成功! 上传文件");
}
return ArrayResult;
#endregion
#region move
case 7://move 001.txt c:\\
//move 000 c:\\
ArrayResult = new ArrayList(5);
arrStr = sCommand.Split(' ');
command2 = arrStr[2];
command = arrStr[1];
directoryInfo2 = new DirectoryInfo(command2);
directoryInfo = new DirectoryInfo(command);
try
{
if(command.IndexOf(".")!=-1)//如果指令中移动的是文件
{
if(!File.Exists(command2 + "\\" + command))//如果目标目录已存在该文件
{
if(File.Exists(rc1.StaticPath + "\\" + command))//如果源文件不存在
{
File.Move(command,command2);
ArrayResult.Add("成功! 移动指定 文件");
}
ArrayResult.Add("找不到要移动的文件!");
}
else
{
ArrayResult.Add("移动失败,该文件已存在!");
}
}
else
{
if(command.Substring(0,1) == command2.Substring(0,1))//如果同处于一个卷下
{
directoryInfo2 = new DirectoryInfo(command2 + "\\" + directoryInfo.Name);
if(!directoryInfo2.Exists)//如果目标目录不存在
{
if(directoryInfo.Exists)
{
directoryInfo.MoveTo(directoryInfo2.FullName);
ArrayResult.Add("成功! 移动指定 目录");
}
}
else ArrayResult.Add("目标目录已存在!");
}
else//不处于同一卷 则先复制过去然后将 本文件夹删除
{
if(!directoryInfo2.Exists)//如果目标目录不存在
{
if(directoryInfo.Exists)
{
if(this.CopyDir(command2,command))
{
ArrayResult.Add("成功! 移动指定 目录");
directoryInfo.Delete(true);
}
else
ArrayResult.Add("失败 移动指定 目录");
}
}
else ArrayResult.Add("目标目录已存在!");
}
}
}
catch(Exception ex)
{
ArrayResult.Add("移动指定目录/文件失败!\r\n" + ex.Message);
}
return ArrayResult;
//break;
#endregion
#region md
case 8://md
ArrayResult = new ArrayList(2);
try
{
string[] splitStr = sCommand.Split(' ');
directoryInfo = new DirectoryInfo(rc1.StaticPath + "\\" + splitStr[1]);
directoryInfo.Create();
ArrayResult.Add("添加目录 成功");
return ArrayResult;
}
catch(IOException ioEx)
{
ArrayResult.Add("添加目录 失败!原因:"+ ioEx.Message);
}
return ArrayResult;
break;
#endregion
#region default
default:
ArrayResult = new ArrayList(1);
ArrayResult.Add("请输入指定的命令:" +
"dir / copy / del / cd / setdir / update /get" );
IsSuccess = true;
return ArrayResult;
//break;
#endregion
}
return null;
}
}
}