c语言 上传图片至服务器,c#将本地文件上传至服务器(内网)

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingAspose.Pdf;usingSystem.IO;usingSystem.Diagnostics;namespaceWindowsFormsApplication1

{public partial classForm1 : Form

{publicForm1()

{

InitializeComponent();

}private void button1_Click(objectsender, EventArgs e)

{

UploadECCInvoice_Intranet("");

}///

///内网上传ECC Invoice///

/// ECCInvoice的路径

private void UploadECCInvoice_Intranet(stringpath_ECCInvoice)

{string destinationFile = @"E:\test\DL180226H012.pdf";//要上传的文件

bool status = false;//连接

string serverFolder = @"\\192.168.1.188\Evan";string PWD = "qwe123!@#";//kingdee1!

status = connectState(serverFolder, "Administrator", PWD);if(status)

{//共享文件夹的目录

DirectoryInfo theFolder = newDirectoryInfo(serverFolder);string filename =theFolder.ToString();//执行方法

TransportRemoteToServer(serverFolder+ @"\", destinationFile, "test.pdf"); //实现将远程服务器文件写入到本地

}else{

MessageBox.Show("连接服务器失败!");

}

}///

///从本地上传文件至服务器///

/// 远程服务器路径(共享文件夹路径)

/// 本地文件夹路径

/// 上传至服务器上的文件名,包含扩展名

public static void TransportRemoteToServer(string src, string dst, stringfileName)

{if (!Directory.Exists(dst))

{

Directory.CreateDirectory(dst);

}

src= src +fileName;

FileStream inFileStream= new FileStream(src, FileMode.OpenOrCreate); //从远程服务器下载到本地的文件

FileStream outFileStream= new FileStream(dst, FileMode.Open); //远程服务器文件 此处假定远程服务器共享文件夹下确实包含本文件,否则程序报错

byte[] buf = new byte[outFileStream.Length];intbyteCount;while ((byteCount = outFileStream.Read(buf, 0, buf.Length)) > 0)

{

inFileStream.Write(buf,0, byteCount);

}

inFileStream.Flush();

inFileStream.Close();

outFileStream.Flush();

outFileStream.Close();

}///

///连接远程共享文件夹///

/// 远程共享文件夹的路径

/// 用户名

/// 密码

///

public static bool connectState(string path, string userName, stringpassWord)

{bool Flag = false;

Process proc= newProcess();try{

proc.StartInfo.FileName= "cmd.exe";

proc.StartInfo.UseShellExecute= false;

proc.StartInfo.RedirectStandardInput= true;

proc.StartInfo.RedirectStandardOutput= true;

proc.StartInfo.RedirectStandardError= true;

proc.StartInfo.CreateNoWindow= true;

proc.Start();

proc.StandardInput.WriteLine("net use * /del /y");string dosLine = "net use" + path + " " + passWord + "/user:" +userName;

proc.StandardInput.WriteLine(dosLine);

proc.StandardInput.WriteLine("exit");while (!proc.HasExited)

{

proc.WaitForExit(1000);

}string errormsg =proc.StandardError.ReadToEnd();

proc.StandardError.Close();if (string.IsNullOrEmpty(errormsg))

{

Flag= true;

}else{throw newException(errormsg);

}

}catch(Exception ex)

{throwex;

}finally{

proc.Close();

proc.Dispose();

}returnFlag;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值