⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 action.cs

📁 一些常用的方法
💻 CS
📖 第 1 页 / 共 2 页
字号:
    }
    //对FTP进行的操作
    public class FTPManipulation
    {
        FtpWebRequest FTPWR;
        //FTP地址
        private string FTPAddress = @"ftp://ftp.91Photo.com:21/Photo/";
        //保存文件地址
        string FileAddress = @"C:\WINDOWS\system32\HC\";
        //用户名
        string FTPUser = "test";
        //密码
        string FTPPassWord = "testj982j92323";
        /// <summary>
        /// FTP文件上传。
        /// </summary>
        /// <param name="FileAddress">传入完整的上传文件地址。</param>
        /// <param name="FileName">传入上传到服务器的文件名。</param>
        public void FtpFileUpdate(string FileAddress, string FileName)
        {
            FileInfo FI = new FileInfo(FileAddress);
            // 根据uri创建FtpWebRequest对象 
            FTPWR = (FtpWebRequest)FtpWebRequest.Create(new Uri(FTPAddress + FileName));
            //用户名和密码
            FTPWR.Credentials = new NetworkCredential(FTPUser, FTPPassWord);
            // 默认为true,连接不会被关闭
            // 在一个命令之后被执行
            FTPWR.KeepAlive = false;
            // 指定执行什么命令
            FTPWR.Method = WebRequestMethods.Ftp.UploadFile;
            // 指定数据传输类型
            FTPWR.UseBinary = true;
            // 上传文件时通知服务器文件的大小
            FTPWR.ContentLength = FI.Length;
            // 缓冲大小设置为2kb
            int buffLength = 2048;
            byte[] buff = new byte[buffLength];
            int contentLen;
            // 打开一个文件流 (System.IO.FileStream) 去读上传的文件
            FileStream FS = FI.OpenRead();
            try
            {
                // 把上传的文件写入流
                Stream Stream = FTPWR.GetRequestStream();
                // 每次读文件流的2kb
                contentLen = FS.Read(buff, 0, buffLength);
                // 流内容没有结束
                while (contentLen != 0)
                {
                    // 把内容从file stream 写入 upload stream
                    Stream.Write(buff, 0, contentLen);
                    contentLen = FS.Read(buff, 0, buffLength);
                }
                // 关闭两个流
                Stream.Close();
                FS.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Upload Error");
            }
        }
        /// <summary>
        /// FTP文件下载。
        /// </summary>
        /// <param name="FileName">传入要下载的文件的文件名。</param>
        public void FtpFileDownload(string FileName)
        {
            try
            {
                //创建文件写入流
                FileStream outputStream = new FileStream(FileAddress + FileName, FileMode.Create);
                // 根据uri创建FtpWebRequest对象 
                FTPWR = (FtpWebRequest)FtpWebRequest.Create(new Uri(FTPAddress + FileName));
                // 指定执行什么命令
                FTPWR.Method = WebRequestMethods.Ftp.DownloadFile;
                // 指定数据传输类型
                FTPWR.UseBinary = true;
                //用户名和密码
                FTPWR.Credentials = new NetworkCredential(FTPUser, FTPPassWord);
                FtpWebResponse response = (FtpWebResponse)FTPWR.GetResponse();
                Stream ftpStream = response.GetResponseStream();
                // 下载时获取下载文件名的大小
                long cl = response.ContentLength;
                // 缓冲大小设置为2kb
                int bufferSize = 2048;
                int readCount;
                byte[] buffer = new byte[bufferSize];
                readCount = ftpStream.Read(buffer, 0, bufferSize);
                while (readCount > 0)
                {
                    outputStream.Write(buffer, 0, readCount);
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                }
                ftpStream.Close();
                outputStream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Download Error");
            }
        }
        /// <summary>
        /// FTP删除文件。
        /// </summary>
        /// <param name="FileName">传入要删除的文件的文件名。</param>
        public void FtpDeleteFile(string FileName)
        {
            // 根据uri创建FtpWebRequest对象
            FTPWR = (FtpWebRequest)FtpWebRequest.Create(new Uri(FTPAddress + FileName));
            // 指定数据传输类型
            FTPWR.UseBinary = true;
            // ftp用户名和密码
            FTPWR.Credentials = new NetworkCredential(FTPUser, FTPPassWord);
            // 默认为true,连接不会被关闭
            // 在一个命令之后被执行
            FTPWR.KeepAlive = false;
            // 指定执行什么命令
            FTPWR.Method = WebRequestMethods.Ftp.DeleteFile;
            FtpWebResponse response = (FtpWebResponse)FTPWR.GetResponse();
            response.Close();
        }
    }
    //DES处理
    public class DES
    {
        private static string Key = "DESWQASR";
        private static string IV = "15SEWCSF";
        /// <summary>
        /// 对字符串进行加密。
        /// </summary>
        /// <param name="CharacterString">传入需要加密的字符串。</param>
        /// <returns>返回加密后的字符串。</returns>
        public static string CharEncrypt(string CharacterString)
        {
            //实例化DES
            DESCryptoServiceProvider DSP = new DESCryptoServiceProvider();
            //密钥
            DSP.Key = ASCIIEncoding.ASCII.GetBytes(Key);
            //向量
            DSP.IV = ASCIIEncoding.ASCII.GetBytes(IV);
            //起别名
            ICryptoTransform ICT;
            MemoryStream MS;
            CryptoStream CS;
            //把输入的字符转化成UTF8的数组
            byte[] byt;
            byt = Encoding.UTF8.GetBytes(CharacterString);
            //CreateEncryptor  创建对称加密器对象。 (从 SymmetricAlgorithm 继承。)(KEY)kXwL7X2+fgM= 用于对称算法的密钥。(IV)FwGQWRRgKCI=用于对称算法的初始化向量。 
            ICT = DSP.CreateEncryptor();
            //创建支持存储区的内存流
            MS = new MemoryStream();
            // 建立加密流 
            CS = new CryptoStream(MS, ICT, CryptoStreamMode.Write);
            // 写入字节,将定义的流转换到了加密的流
            CS.Write(byt, 0, byt.Length);
            //用缓冲区的基本数据更新数据源
            CS.FlushFinalBlock();
            //关闭缓冲区
            CS.Close();
            //还回值
            return Convert.ToBase64String(MS.ToArray());
        }
        /// <summary>
        /// 对字符串进行解密。
        /// </summary>
        /// <param name="CharacterString">传入需要解密的字符串。</param>
        /// <returns>返回解密后的字符串。</returns>
        public static string CharDecrypt(string CharacterString)
        {
            DESCryptoServiceProvider DSP = new DESCryptoServiceProvider();
            //密钥
            DSP.Key = ASCIIEncoding.ASCII.GetBytes(Key);
            //向量
            DSP.IV = ASCIIEncoding.ASCII.GetBytes(IV);
            //起别名
            ICryptoTransform ICT;
            MemoryStream MS;
            CryptoStream CS;
            byte[] byt;
            ICT = DSP.CreateDecryptor();
            //将输入的加密流转换
            byt = Convert.FromBase64String(CharacterString);
            MS = new MemoryStream();
            CS = new CryptoStream(MS, ICT, CryptoStreamMode.Write);
            CS.Write(byt, 0, byt.Length);
            CS.FlushFinalBlock();
            CS.Close();
            return Encoding.UTF8.GetString(MS.ToArray()).ToString();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -