1.点击打开窗体 选择文件
OpenFileDialog dialog = new OpenFileDialog();
dialog.Multiselect = false;//该值确定是否可以选择多个文件
dialog.Title = "请选择*.csv文档";
//筛选文件类型
dialog.Filter = "所有文件(*.csv)|*.csv";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string a=System.IO.Path.GetFileName(dialog.FileName);//得到文件名
string b=System.IO.Path.GetDirectoryName(dialog.FileName);//得到路径
string c=System.IO.Path.GetFullPath(dialog.FileName);//绝对路径
string d = System.IO.Path.GetExtension(dialog.FileName);//文件扩展名
string ew = System.IO.Path.GetFileNameWithoutExtension(dialog.FileName); //文件名没有扩展名
string f = System.IO.Path.GetFileName(dialog.FileName);//得到文件
string g = System.IO.Path.GetDirectoryName(dialog.FileName);
textBox1.Text = dialog.FileName;
}
点击打开窗体 选择文件夹
FolderBrowserDialog P_File_Folder = new FolderBrowserDialog();
if (P_File_Folder.ShowDialog() == DialogResult.OK)
{
textBox2.Text = P_File_Folder.SelectedPath;
}
2.文件上传
2.1 c# 添加程序集引用 SSH.NET
2.2 SFTP上传方法
//实例化文件操作类(详情看备注一)
FileInfo f = new FileInfo(route);
// FullName只读属性,获取文件的名称
String uploadfile = f.FullName;
//1.SFTP建立连接
//参数 1.主机地址 2.端口 3.用户名 4.密码
var client = new SftpClient(SFTP_IP, Int32.Parse(SFTP_port), SFTP_username, SFTP_password);
//开启连接
client.Connect();
//判断连接是否成功
if (client.IsConnected)
{
//连接成功
Console.WriteLine("I AM CONNECTED");
}
var fileStream = new FileStream(uploadfile, FileMode.Open);
if (fileStream != null)
{
Console.WriteLine("YOU ARE NOT NULL");
}
client.BufferSize = 4 * 1024;
//上传方法 参数 1.Data input stream. 2.上传至文件地址+文件名 3.回调地址
//方法说过详情看备注二
client.UploadFile(fileStream, SFTP_catalog + f.Name, null);
//关闭连接
client.Disconnect();
//释放
client.Dispose();
完整代码
/// <summary>
/// 选择要上传的文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
//命名空间System.Configuration中不存在类型或命名空间名称ConfigurationManager
//手动添加这个程序集的引用,在项目右键添加引用选择System.Configuration即可.
String SFTP_IP = System.Configuration.ConfigurationManager.AppSettings["SFTP_IP"];
String SFTP_port = System.Configuration.ConfigurationManager.AppSettings["SFTP_port"];
String SFTP_username = System.Configuration.ConfigurationManager.AppSettings["SFTP_username"];
String SFTP_password = System.Configuration.ConfigurationManager.AppSettings["SFTP_password"];
String SFTP_catalog = System.Configuration.ConfigurationManager.AppSettings["SFTP_catalog"];
String route = "";
String name = "";
OpenFileDialog dialog = new OpenFileDialog();
dialog.Multiselect = false;//该值确定是否可以选择多个文件
dialog.Title = "请选择电子文档excel";
dialog.Filter = "所有文件(*.csv)|*.csv";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
name = System.IO.Path.GetFileName(dialog.FileName);//得到文件名
route = System.IO.Path.GetFullPath(dialog.FileName);//绝对路径
textBox1.Text = name;
}
FileInfo f = new FileInfo(route);
String uploadfile = f.FullName;
//1.SFTP建立连接
var client = new SftpClient(SFTP_IP, Int32.Parse(SFTP_port), SFTP_username, SFTP_password);
client.Connect();
if (client.IsConnected)
{
Console.WriteLine("I AM CONNECTED");
}
var fileStream = new FileStream(uploadfile, FileMode.Open);
if (fileStream != null)
{
Console.WriteLine("YOU ARE NOT NULL");
}
client.BufferSize = 4 * 1024;
client.UploadFile(fileStream, SFTP_catalog + f.Name, null);
client.Disconnect();
client.Dispose();
}
备注说明:
备注一
C# 语言中 File 类和 FileInfo 类都是用来操作文件的,并且作用相似,它们都能完成对文件的创建、更改文件的名称、删除文件、移动文件等操作。
File 类是静态类,其成员也是静态的,通过类名即可访问类的成员;FileInfo 类不是静态成员,其类的成员需要类的实例来访问。
文档操作类,点击查看详细介绍
备注二
//
// 摘要:
// Uploads stream into remote file.
//
// 参数:
// input:
// Data input stream.
//
// path:
// Remote file path.
//
// uploadCallback:
// The upload callback.
//
// 异常:
// T:System.ArgumentNullException:
// input is null.
//
// T:System.ArgumentException:
// path is null or contains only whitespace characters.
//
// T:Renci.SshNet.Common.SshConnectionException:
// Client is not connected.
//
// T:Renci.SshNet.Common.SftpPermissionDeniedException:
// Permission to upload the file was denied by the remote host.
// -or-
// A SSH command was denied by the server.
//
// T:Renci.SshNet.Common.SshException:
// A SSH error where System.Exception.Message is the message from the remote host.
//
// T:System.ObjectDisposedException:
// The method was called after the client was disposed.
//
// 言论:
// Method calls made by this method to input, may under certain conditions result
// in exceptions thrown by the stream.