Windows就是多線程模式.每一個解決方案就是一個進程.一個進程下擁有多個線程.
簡單點.單核的處理器不存在多線程.是CPU在每一個線程上切換處理.在人反應不過來的情況下完成同步的效果.
比如左手畫圓右手畫方.聰敏的人會利用時間差完成.但是換成雙手雙腳就會出現異常
System.Threading.Thread[] threads = new System.Threading.Thread[2];
for (int i = 0; i <= 1; i++)
{
System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(p.GGG));
threads[i].Start();
}
private void GGG()//證明線程利用時間差做出類似同步的案例
{
for (int i = 0; i < 100; i++)
{
int f = 10;
GGG1(f);
}
}
private void GGG1(int f)
{
//lock (Glock)//lock最好放在方法的最上面,沒有死鎖就會出現復數
//{
if (A1 >= f)
{
Console.WriteLine(Thread.CurrentThread.Name);
Console.WriteLine(A1 + "|" + f);
A1 = A1 - f;
Console.WriteLine(A1);
}
// }
}
int A1 = 1000;
簡單點.將單例模式注釋解開.可以得到A1的值大於0if判斷生效
如果將單例模式注釋掉.可以得到A1值小於0 if 無效
如果運行到a1=1 f =1時
第一個線程進入if判斷.通過.但是沒有相減.CPU切換到第二個線程.進入if判斷.然後通過
結果a1運行了-1-1
所以if判斷無效.證明.在單核CPU上不存在同步的多線程.多線程是利用時間差完成對資源的分配.
線程利用時間差完成類似同步效果
現在的CPU可以處理幾十個進程.上百個線程.就像一個人不啟動線程就只有一只手.但是他實際上可以有一千只手
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class MyThread
{
WaitHandle[] wssssss = new WaitHandle[] { };
Thread t = null;
//如果為 true則將初始狀態設置為終止
//如果為 false,則將初始狀態設置為非終止。
ManualResetEvent manualEvent = new ManualResetEvent(false);//true沒有信號運行,false有信號不運行
private void Run()
{
Console.WriteLine(this.manualEvent.WaitOne());//等待信號 ,收到信號返回true,沒有永不返回
//+int|TimeSpan等待指定時間並運行,
Console.WriteLine("這裡是 {0}", Thread.CurrentThread.ManagedThreadId);//如果有多個線程,這裡就自動循環打印
Thread.Sleep(500);
}
public void Start()
{
this.manualEvent.Set();//啟動沒有信號
Console.WriteLine("已啟動");
}
public void Stop()
{
this.manualEvent.Reset();//阻止有信號
Console.WriteLine("已阻止");
}
public MyThread()
{
for (int i = 0; i <=5; i++)
{
t = new Thread(this.Run);
t.Start();
}
}
public void w()
{
for (int i = 0; i <= 5; i++)
{
t = new Thread(this.Run);
t.Start();
}
}
static void Main(string[] args)
{
MyThread myt = new MyThread();
//myt.w();
string str1 = Console.ReadLine();
if (str1.ToLower().Trim() == "stop")
{
myt.Stop();
}
if (str1.ToLower().Trim() == "start")
{
myt.Start();
}
Console.Read();
}
}
}
以上是多線程通信.
有很多代碼都是本人從曾經的案例中操作過.代碼可能不全.但是思想沒錯.
ManualResetEvent 就是信號燈.在線程中通信.
true沒有信號運行,false有信號不運行
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Globalization;
namespace ConsoleApplication1
{
public class FtpWeb
{
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 FtpWeb(string FtpServerIP, string FtpRemotePath, string FtpUserID, string FtpPassword)
{
ftpServerIP = FtpServerIP;
ftpRemotePath = FtpRemotePath;
ftpUserID = FtpUserID;
ftpPassword = FtpPassword;
ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";
}
/// <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)
{
outputStream.Write(buffer, 0, readCount);
readCount = ftpStream.Read(buffer, 0, bufferSize);
}
ftpStream.Close();
outputStream.Close();
response.Close();
}
catch (Exception ex)
{
Insert_Standard_ErrorLog.Insert("FtpWeb", "Download Error --> " + ex.Message);
}
}
/// <summary>
/// 刪除文件
/// </summary>
/// <param name="fileName"></param>
public void Delete(string fileName)
{
try
{
string uri = ftpURI + fileName;
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.DeleteFile;
string result = String.Empty;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
long size = response.ContentLength;
Stream datastream = response.GetResponseStream();
StreamReader sr = new StreamReader(datastream);
result = sr.ReadToEnd();
sr.Close();
datastream.Close();
response.Close();
}
catch (Exception ex)
{
Insert_Standard_ErrorLog.Insert("FtpWeb", "Delete Error --> " + ex.Message + " 文件名:" + fileName);
}
}
/// <summary>
/// 刪除文件夾
/// </summary>
/// <param name="folderName"></param>
public void RemoveDirectory(string folderName)
{
try
{
string uri = ftpURI + folderName;
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.RemoveDirectory;
string result = String.Empty;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
long size = response.ContentLength;
Stream datastream = response.GetResponseStream();
StreamReader sr = new StreamReader(datastream);
result = sr.ReadToEnd();
sr.Close();
datastream.Close();
response.Close();
}
catch (Exception ex)
{
Insert_Standard_ErrorLog.Insert("FtpWeb", "Delete Error --> " + ex.Message + " 文件名:" + folderName);
}
}
/// <summary>
/// 獲取當前目錄下明細(包含文件和文件夾)
/// </summary>
/// <returns></returns>
public string[] GetFilesDetailList()
{
string[] downloadFiles;
try
{
StringBuilder result = new StringBuilder();
FtpWebRequest ftp;
ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI));
ftp.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
WebResponse response = ftp.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
//while (reader.Read() > 0)
//{
//}
string line = reader.ReadLine();
//line = reader.ReadLine();
//line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
result.Remove(result.ToString().LastIndexOf("\n"), 1);
reader.Close();
response.Close();
return result.ToString().Split('\n');
}
catch (Exception ex)
{
downloadFiles = null;
Insert_Standard_ErrorLog.Insert("FtpWeb", "GetFilesDetailList Error --> " + ex.Message);
return downloadFiles;
}
}
/// <summary>
/// 獲取當前目錄下文件列表(僅文件)
/// </summary>
/// <returns></returns>
public string[] GetFileList(string mask)
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
FtpWebRequest reqFTP;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
string line = reader.ReadLine();
while (line != null)
{
if (mask.Trim() != string.Empty && mask.Trim() != "*.*")
{
string mask_ = mask.Substring(0, mask.IndexOf("*"));
if (line.Substring(0, mask_.Length) == mask_)
{
result.Append(line);
result.Append("\n");
}
}
else
{
result.Append(line);
result.Append("\n");
}
line = reader.ReadLine();
}
result.Remove(result.ToString().LastIndexOf('\n'), 1);
reader.Close();
response.Close();
return result.ToString().Split('\n');
}
catch (Exception ex)
{
downloadFiles = null;
if (ex.Message.Trim() != "遠程服務器返回錯誤: (550) 文件不可用(例如,未找到文件,無法訪問文件)。")
{
Insert_Standard_ErrorLog.Insert("FtpWeb", "GetFileList Error --> " + ex.Message.ToString());
}
return downloadFiles;
}
}
/// <summary>
/// 獲取當前目錄下所有的文件夾列表(僅文件夾)
/// </summary>
/// <returns></returns>
public string[] GetDirectoryList()
{
string[] drectory = GetFilesDetailList();
string m = string.Empty;
foreach (string str in drectory)
{
int dirPos = str.IndexOf("<DIR>");
if (dirPos > 0)
{
/*判斷 Windows 風格*/
m += str.Substring(dirPos + 5).Trim() + "\n";
}
else if (str.Trim().Substring(0, 1).ToUpper() == "D")
{
/*判斷 Unix 風格*/
string dir = str.Substring(54).Trim();
if (dir != "." && dir != "..")
{
m += dir + "\n";
}
}
}
char[] n = new char[] { '\n' };
return m.Split(n);
}
/// <summary>
/// 判斷當前目錄下指定的子目錄是否存在
/// </summary>
/// <param name="RemoteDirectoryName">指定的目錄名</param>
public bool DirectoryExist(string RemoteDirectoryName)
{
string[] dirList = GetDirectoryList();
foreach (string str in dirList)
{
if (str.Trim() == RemoteDirectoryName.Trim())
{
return true;
}
}
return false;
}
/// <summary>
/// 判斷當前目錄下指定的文件是否存在
/// </summary>
/// <param name="RemoteFileName">遠程文件名</param>
public bool FileExist(string RemoteFileName)
{
string[] fileList = GetFileList("*.*");
foreach (string str in fileList)
{
if (str.Trim() == RemoteFileName.Trim())
{
return true;
}
}
return false;
}
/// <summary>
/// 創建文件夾
/// </summary>
/// <param name="dirName"></param>
public void MakeDir(string dirName)
{
FtpWebRequest reqFTP;
try
{
// dirName = name of the directory to create.
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + dirName));
reqFTP.Method = WebRequestMethods.Ftp.MakeDirectory;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
ftpStream.Close();
response.Close();
}
catch (Exception ex)
{
Insert_Standard_ErrorLog.Insert("FtpWeb", "MakeDir Error --> " + ex.Message);
}
}
/// <summary>
/// 獲取指定文件大小
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public long GetFileSize(string filename)
{
FtpWebRequest reqFTP;
long fileSize = 0;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + filename));
reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
fileSize = response.ContentLength;
ftpStream.Close();
response.Close();
}
catch (Exception ex)
{
Insert_Standard_ErrorLog.Insert("FtpWeb", "GetFileSize Error --> " + ex.Message);
}
return fileSize;
}
/// <summary>
/// 改名
/// </summary>
/// <param name="currentFilename"></param>
/// <param name="newFilename"></param>
public void ReName(string currentFilename, string newFilename)
{
FtpWebRequest reqFTP;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpURI + currentFilename));
reqFTP.Method = WebRequestMethods.Ftp.Rename;
reqFTP.RenameTo = newFilename;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
ftpStream.Close();
response.Close();
}
catch (Exception ex)
{
Insert_Standard_ErrorLog.Insert("FtpWeb", "ReName Error --> " + ex.Message);
}
}
/// <summary>
/// 移動文件
/// </summary>
/// <param name="currentFilename"></param>
/// <param name="newFilename"></param>
public void MovieFile(string currentFilename, string newDirectory)
{
ReName(currentFilename, newDirectory);
}
/// <summary>
/// 切換當前目錄
/// </summary>
/// <param name="DirectoryName"></param>
/// <param name="IsRoot">true 絕對路徑 false 相對路徑</param>
public void GotoDirectory(string DirectoryName, bool IsRoot)
{
if (IsRoot)
{
ftpRemotePath = DirectoryName;
}
else
{
ftpRemotePath += DirectoryName + "/";
}
ftpURI = "ftp://" + ftpServerIP + "/" + ftpRemotePath + "/";
}
/// <summary>
/// 刪除訂單目錄
/// </summary>
/// <param name="ftpServerIP">FTP 主機地址</param>
/// <param name="folderToDelete">FTP 用戶名</param>
/// <param name="ftpUserID">FTP 用戶名</param>
/// <param name="ftpPassword">FTP 密碼</param>
public static void DeleteOrderDirectory(string ftpServerIP, string folderToDelete, string ftpUserID, string ftpPassword)
{
try
{
if (!string.IsNullOrEmpty(ftpServerIP) && !string.IsNullOrEmpty(folderToDelete) && !string.IsNullOrEmpty(ftpUserID) && !string.IsNullOrEmpty(ftpPassword))
{
FtpWeb fw = new FtpWeb(ftpServerIP, folderToDelete, ftpUserID, ftpPassword);
//進入訂單目錄
fw.GotoDirectory(folderToDelete, true);
//獲取規格目錄
string[] folders = fw.GetDirectoryList();
foreach (string folder in folders)
{
if (!string.IsNullOrEmpty(folder) || folder != "")
{
//進入訂單目錄
string subFolder = folderToDelete + "/" + folder;
fw.GotoDirectory(subFolder, true);
//獲取文件列表
string[] files = fw.GetFileList("*.*");
if (files != null)
{
//刪除文件
foreach (string file in files)
{
fw.Delete(file);
}
}
//刪除沖印規格文件夾
fw.GotoDirectory(folderToDelete, true);
fw.RemoveDirectory(folder);
}
}
//刪除訂單文件夾
string parentFolder = folderToDelete.Remove(folderToDelete.LastIndexOf('/'));
string orderFolder = folderToDelete.Substring(folderToDelete.LastIndexOf('/') + 1);
fw.GotoDirectory(parentFolder, true);
fw.RemoveDirectory(orderFolder);
}
else
{
throw new Exception("FTP 及路徑不能為空!");
}
}
catch (Exception ex)
{
throw new Exception("刪除訂單時發生錯誤,錯誤信息為:" + ex.Message);
}
}
}
public class Insert_Standard_ErrorLog
{
public static void Insert(string x, string y)
{
}
}
}
以上就是FTP的操作類.
// FtpWeb URL = new FtpWeb("FTP地址", "指定目錄", "賬號", "密碼 ");
//上傳到指定目錄
// URL.Upload("D:\\install\\dorado\\docs\\javadoc\\constant-values.html");
//下載指定目錄的文件到指定位置
//URL.Download("sc", "S.TXT");
核心就三個對象.ftprequest .ftprespon . 和FileStream .Stream 基本上類似的對象底層都是這三個.請求.接受.數據流
using System;
using System.Collections;
using System.Windows.Forms;
using Tamir.SharpSsh.jsch;
/*
* ITransferProtocol.cs
*
* Copyright (c) 2006 Tamir Gal, http://www.tamirgal.com, All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the distribution.
*
* 3. The names of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
* *OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
**/
namespace sharpSshTest
{
public class SFTPHelper
{
private Session m_session;
private Channel m_channel;
private ChannelSftp m_sftp;
/// <summary>
/// 連接SFTP
/// </summary>
/// <param name="host">sftp地址</param>
/// <param name="user">用戶名</param>
/// <param name="pwd">密碼</param>
public SFTPHelper(string host, string user, string pwd)
{
string[] arr = host.Split(':');
string ip = arr[0];
int port = 22;
if (arr.Length > 1) port = Int32.Parse(arr[1]);
JSch jsch = new JSch();
m_session = jsch.getSession(user, ip, port);
MyUserInfo ui = new MyUserInfo();
ui.setPassword(pwd);
m_session.setUserInfo(ui);
}
/// <summary>
/// 判斷SFTP連接狀態
/// </summary>
public bool Connected { get { return m_session.isConnected(); } }
/// <summary>
/// 連接SFTP
/// </summary>
/// <returns></returns>
public bool Connect()
{
try
{
if (!Connected)
{
m_session.connect();
m_channel = m_session.openChannel("sftp");
m_channel.connect();
m_sftp = (ChannelSftp)m_channel;
}
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 斷開SFTP
/// </summary>
public void Disconnect()
{
if (Connected)
{
m_channel.disconnect();
m_session.disconnect();
}
}
/// <summary>
/// SFTP上傳文件
/// </summary>
/// <param name="remotePath">服務器路徑</param>
/// <param name="localPath">本地路徑</param>
/// <returns></returns>
public bool Put(string remotePath, string localPath)
{
try
{
Tamir.SharpSsh.java.String src = new Tamir.SharpSsh.java.String(localPath);
Tamir.SharpSsh.java.String dst = new Tamir.SharpSsh.java.String(remotePath);
m_sftp.put(src, dst);
return true;
}
catch(Exception e)
{
var a = e.Message;
return false;
}
}
/// <summary>
/// SFTP下載文件
/// </summary>
/// <param name="remotePath">服務器路徑</param>
/// <param name="localPath">本地路徑</param>
/// <returns>是否下載成功</returns>
public bool Get(string remotePath, string localPath)
{
try
{
Tamir.SharpSsh.java.String src = new Tamir.SharpSsh.java.String(remotePath);
Tamir.SharpSsh.java.String dst = new Tamir.SharpSsh.java.String(localPath);
m_sftp.get(src, dst);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// SFTP刪除文件
/// </summary>
/// <param name="remoteFile">服務器文件</param>
/// <returns>是否成功</returns>
public bool Delete(string remoteFile)
{
try
{
m_sftp.rm(remoteFile);
return true;
}
catch(Exception e)
{
MessageBox.Show(e.Message);
return false;
}
}
/// <summary>
/// 獲取SFTP文件列表
/// </summary>
/// <param name="remotePath">服務器路徑</param>
/// <param name="fileType">文件類型</param>
/// <returns>文件名列表</returns>
public ArrayList GetFileList(string remotePath, string fileType)
{
try
{
Tamir.SharpSsh.java.util.Vector vvv = m_sftp.ls(remotePath);
ArrayList objList = new ArrayList();
foreach (Tamir.SharpSsh.jsch.ChannelSftp.LsEntry qqq in vvv)
{
string sss = qqq.getFilename();
if (sss.Length > (fileType.Length + 1) && fileType == sss.Substring(sss.Length - fileType.Length))
{ objList.Add(sss); }
else { continue; }
}
return objList;
}
catch
{
return null;
}
}
/// <summary>
/// 登錄驗證信息
/// </summary>
public class MyUserInfo : UserInfo
{
private string passwd;
public String getPassword() { return passwd; }
public void setPassword(String passwd) { this.passwd = passwd; }
public String getPassphrase() { return null; }
public bool promptPassphrase(String message) { return true; }
public bool promptPassword(String message) { return true; }
public bool promptYesNo(String message) { return true; }
public void showMessage(String message) { }
}
}
/// <summary>
/// Summary description for sharpSshTest.
/// </summary>
public class sharpSshTest
{
const int JSCH_SHELL = 1;
const int JSCH_AES = 2;
const int JSCH_PUBKEY = 3;
const int JSCH_SFTP = 4;
const int JSCH_KEYEGEN = 5;
const int JSCH_KNOWN_HOSTS = 6;
const int JSCH_CHANGE_PASS = 7;
const int JSCH_PORT_FWD_L = 8;
const int JSCH_PORT_FWD_R = 9;
const int JSCH_STREAM_FWD = 10;
const int JSCH_STREAM_SUBSYSTEM = 11;
const int JSCH_VIA_HTTP = 12;
const int SHARP_SHELL = 13;
const int SHARP_EXPECT = 14;
const int SHARP_EXEC = 15;
const int SHARP_TRANSFER = 16;
const int EXIT = 17;
public static void Main()
{
//SFTPHelper sftp = new SFTPHelper("網址", "賬號", "密碼");
////注意文件的路徑
string remote = "/data/file/clients/relase for Win7 or above.zip";
string local = "D://relase for Win7 or above.zip";
SFTPHelper _helper = new SFTPHelper("網址", "賬號", "密碼");
if (!_helper.Connected)
{
_helper.Connect();
_helper.Get(remote, local);//下載
_helper.Put("/data/file/clients/", @"D:\test.zip");//上傳
var a= _helper.GetFileList("/data/file/clients","zip");//讀取
var b= _helper.Delete("/data/file/clients/test.zip");//刪除
}
_helper.Disconnect();
Console.WriteLine("OK");
Console.ReadLine();
}
public static string[] GetArgs(string[] args)
{
for(int i=0; i<args.Length; i++)
{
Console.Write("Enter {0}: ", args[i]);
args[i] = Console.ReadLine();
}
return args;
}
static void PrintVersoin()
{
try
{
System.Reflection.Assembly asm
= System.Reflection.Assembly.GetAssembly(typeof(Tamir.SharpSsh.SshBase));
Console.WriteLine("SharpSSH-"+asm.GetName().Version);
}
catch
{
Console.WriteLine("sharpSsh v1.0");
}
}
}
}
SFTP略微復雜.因為要去FSTP上下載一個案例.然後在那個案例上開發....
以上就是SFTP
InstallShield貌似在VS15版本上無法安裝打包
Smart Install Maker就可以滿足
在文件中加入Release編譯的dll.後選擇綠色的按鈕完成安裝包的制作