C# 程序如何操作linux服务器上的文件

本文介绍如何使用C#通过FTP协议来操作Linux服务器上的文件,包括上传、下载、删除、创建目录、改名等操作,提供详细的代码示例。
摘要由CSDN通过智能技术生成

刚开始思考这个问题的时候,完全没有头绪,不知道怎么跨越系统的屏障来操作linux服务器上的文件,还好大神很多,度娘了一下,发现其实跟系统没关系,只要是想操作文件,FTP,HTTP等都可以。

下面把简单的例子,分享一下。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Globalization;

namespace filecontrol
{
    public partial class Form1 : Form
    {
        string ftpServerIP;

        string ftpRemotePath;

        string ftpUserID;

        string ftpPassword;

        string ftpURI;

        /// <summary>

        /// 连接FTP

        /// </summary>

        /// <param name="FtpServerIP">FTP连接地址</param>

        /// <param name="FtpRemotePath">指定FTP连接成功后的当前目录, 如果不指定即默认为根目录</param>

        /// <param name="FtpUserID">用户名</param>

        /// <param name="FtpPassword">密码</param>
        public Form1(string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword)
        {
            InitializeComponent();

            ftpServerIP = FtpServerIP;

            ftpRemotePath = FtpRemotePath;

            ftpUserID = FtpUserID;

            ftpPassword = FtpPassword;

            ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";
        }

        private void button1_Click(object sender, EventArgs e)
        {

        }


        /// <summary>

        /// 上传

        /// </summary>

        /// <param name="filename"></param>

        public void Upload(string filename)

        {

            FileInfo fileInf = new FileInfo(filename);

            string uri = ftpURI + fileInf.Name;

            FtpWebRequest reqFTP;

 

            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));

            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

            reqFTP.KeepAlive = false;

            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

            reqFTP.UseBinary = true;

            reqFTP.ContentLength = fileInf.Length;

            int buffLength = 2048;

            byte[] buff = new byte[buffLength];

            int contentLen;

            FileStream fs = fileInf.OpenRead();

            try

            {

                Stream strm = reqFTP.GetRequestStream();

                contentLen = fs.Read(buff, 0, buffLength);

                while (contentLen != 0)

                {

                    strm.Write(buff, 0, contentLen);

                    contentLen = fs.Read(buff, 0, buffLength);

                }

                strm.Close();

                fs.Close();

            }

            catch (Exception ex)

            {

                Insert_Standard_ErrorLog.Insert("FtpWeb", "Upload Error --> " + ex.Message);

            }

        }

 

        /// <summary>

        /// 下载

        /// </summary>

        /// <param name="filePath"></param>

        /// <param name="fileName"></param>

        public void Download(string filePath, string fileName)

        {

            FtpWebRequest reqFTP;

            try

            {

                FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create);

 

                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + fileName));

                reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;

                reqFTP.UseBinary = true;

                reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

                FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

                Stream ftpStream = response.GetResponseStream();

                long cl = response.ContentLength;

                int bufferSize = 2048;

                int readCount;

                byte[] buffer = new byte[bufferSize];

 

                readCount = ftpStream.Read(buffer, 0, bufferSize);

                while (readCount > 0)

                {

             

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值