实现文件上传和下载,包含服务器和客户端对于学习TCP的同学有帮助
public bool Save(string filename, byte[] byt)
{
try
{
string UploadPath = System.IO.Directory.GetCurrentDirectory() "\\Upload\\";
if (!Directory.Exists(UploadPath))
{
Directory.CreateDirectory(UploadPath);
Console.WriteLine("创建目录" UploadPath "!");
}
FileStream fs = new FileStream(UploadPath filename, FileMode.Append, FileAccess.Write);
fs.Write(byt, 0, byt.Length);
fs.Close();
fs.Dispose();
return true;
}
catch (Exception err)
{
Console.WriteLine("发生错误" err.ToString());
try
{
string UploadPath = System.IO.Directory.GetCurrentDirectory() "\\Upload\\";
string DelFileName = UploadPath filename;
if (File.Exists(DelFileName))
{
File.Delete(DelFileName);
}
}
catch (Exception err2)
{
Console.WriteLine("发生错误" err2.ToString());
}
return false;
}
}
评论