• FTP文件服务器代码


    文件操作的核心代码:

    /// <summary>
    /// FTP文件信息类(帮助进行文件的上传于下载)
    /// </summary>
    [Serializable()]
    public class FileInfo
    {
    /// <summary>
    /// 默认构造函数
    /// </summary>
    public FileInfo()
    {
    }
    /// <summary>
    ///
    /// </summary>
    /// <param name="filename">文件名</param>
    /// <param name="size">文件大小</param>
    public FileInfo(string filename, long size)
    {
    this.Filename = filename;
    this.Size = size;
    }
    /// <summary>
    /// 文件名
    /// </summary>
    public string Filename = string.Empty;
    /// <summary>
    /// 文件大小
    /// </summary>
    public long Size = long.MinValue;
    }
    public delegate void PostedDataHandler(string user, byte[] data, string filepath);
    public delegate void UpdateHandler(string user, string filepath);

    [Serializable()]
    public struct UploadData
    {
    public string Filename;
    public byte[] File;
    }
    /// <summary>
    /// 文件操作契约
    /// </summary>
    public interface IFTPServer
    {
    void Upload(string user, List<UploadData> files, string filepath);
    void Download(string user, string filename, out byte[] file, string filepath);
    void GetFiles(out List<FileInfo> files, string filepath);
    void Connect(string user);
    void Disconnect(string user);
    void PostData(string user, byte[] data, string filepath);
    event PostedDataHandler PostedData;
    event UpdateHandler Update;

    }
    /// <summary>
    /// 可远程操作的文件处理类
    /// </summary>
    public abstract class PostedData : System.MarshalByRefObject
    {
    public void Server_PostData(string user, byte[] data, string filepath)
    {
    this.ImplementedPostData(user, data, filepath);
    }

    public void Server_Update(string user, string filepath)
    {
    this.ImplementedUpdate(user, filepath);
    }

    public abstract void ImplementedPostData(string user, byte[] data, string filepath);

    public abstract void ImplementedUpdate(string user, string filepath);
    }
    public class MachineInfo
    {

    public static string MachineName = Environment.MachineName;
    public static IPEndPoint MachineIP;
    public static List<FileInfo> Files = new List<FileInfo>();

    /// <summary>
    /// 获取文件的列表
    /// </summary>
    /// <param name="folder"></param>
    public static void GetFiles(string folder)
    {
    if (!Directory.Exists(folder))
    return;

    foreach (string file in Directory.GetFiles(folder))
    {
    FileInfo info = new FileInfo(Path.GetFileName(file), (new System.IO.FileInfo(file).Length / 1024));
    Files.Add(info);
    }
    }

    /// <summary>
    /// 过去机器的IP地址
    /// </summary>
    /// <returns></returns>
    public static IPEndPoint GetMachineIP()
    {
    IPEndPoint result = null;

    ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select IPAddress From Win32_NetworkAdapterConfiguration");

    foreach (ManagementObject obj in searcher.Get())
    {
    if (obj["IPAddress"] != null)
    {
    string[] ip = (string[])obj["IPAddress"];
    result = new IPEndPoint(IPAddress.Parse(ip[0]), 9898);
    break;
    }
    }

    return result;
    }

    public static string GetJustIP()
    {
    string result = null;

    ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select IPAddress From Win32_NetworkAdapterConfiguration");

    foreach (ManagementObject obj in searcher.Get())
    {
    if (obj["IPAddress"] != null)
    {
    string[] ip = (string[])obj["IPAddress"];
    result = ip[0];
    break;
    }
    }

    return result;
    }

    }

  • 相关阅读:
    cxf简单实例
    生成素描图片
    Robotlegs2 学习笔记 -- SwiftSuspenders 2.x (2)
    Robotlegs2 学习笔记 -- SwiftSuspenders 2.x (1)
    Spring boot的put请求 (如何实现RestFul Api风格)
    net start npf启用失败
    vue项目中:‘vue-cli-service' 不是内部或外部命令,也不是可运行的程序或批处理文件的报错
    SpringBoot + Mybatis 打印sql语句修改配置文件的方式
    vue-cli最新脚手架的安装
    SpringBoot项目中设置日期返回的格式
  • 原文地址:https://www.cnblogs.com/rongfengliang/p/3419047.html
Copyright © 2020-2023  润新知