網站首頁 編程語言 正文
一、建立連接
public string connectFTP(string vPath, string vUID, string vPassword)
{
string errormsg = "";
Process proc = new Process();
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();
string dosLine = "net use " + vPath + " " + vPassword + " /user:" + vUID;
proc.StandardInput.WriteLine(dosLine);
proc.StandardInput.WriteLine("exit");
while (!proc.HasExited)
{
proc.WaitForExit(1000);
}
errormsg = proc.StandardError.ReadToEnd();
proc.StandardError.Close();
}
catch (Exception ex)
{
//throw ex;
//MessageBox.Show(ex.Message);
}
finally
{
proc.Close();
proc.Dispose();
}
return errormsg;
}
二、上傳文件
public void UploadFile(string vPath, string vUID, string vPassword, string vLocalPath, string file)
{
bool status = false;
status = connectState(vPath, vUID, vPassword);
if (status)
{
DirectoryInfo theFolder = new DirectoryInfo(vPath + "/" + file);
string filename = vLocalPath;
Transport(vLocalPath, vPath + "/" + file);
//System.Diagnostics.Process.Start(vPath);
}
else
{
mesLog.Info("未能連接!");
//MessageBox.Show("未能連接!");
}
}
三、連接狀態
public static bool connectState(string vPath, string vUID, string vPassword)
{
bool Flag = false;
Process proc = new Process();
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();
string dosLine = "net use " + vPath + " " + vPassword + " /user:" + vUID;
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 new Exception(errormsg);
}
}
catch (Exception ex)
{
//throw ex;
//MessageBox.Show(ex.Message);
}
finally
{
proc.Close();
proc.Dispose();
}
return Flag;
}
四、傳送
public static void Transport(string src, string fileName)
{
FileStream inFileStream = new FileStream(src, FileMode.Open);
FileStream outFileStream = new FileStream(fileName, FileMode.OpenOrCreate);
byte[] buf = new byte[inFileStream.Length];
int byteCount;
while ((byteCount = inFileStream.Read(buf, 0, buf.Length)) > 0)
{
outFileStream.Write(buf, 0, byteCount);
}
inFileStream.Flush();
inFileStream.Close();
outFileStream.Flush();
outFileStream.Close();
File.Delete(src);
}
原文鏈接:https://www.cnblogs.com/wml-it/p/15188139.html
相關推薦
- 2024-07-18 restTemplate使用總結
- 2024-01-12 JPA實現不等于查詢
- 2022-12-15 Go字典使用詳解_Golang
- 2022-11-01 PyTorch實現MNIST數據集手寫數字識別詳情_python
- 2024-07-15 SpringBoot使用itext導出pdf(含圖片和表格)
- 2023-03-22 python實現四舍五入方式_python
- 2022-07-23 SpringBoot分頁查詢
- 2023-04-18 Python之split函數的深入理解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支