[英]Select and upload a file to a specific folder
我需要將一個選定的文件上傳到特定的文件夾。 我有以下代碼:
using System.IO;
namespace FTP_UPLOAD
{
public partial class FTPUPLOAD : Form
{
public FTPUPLOAD()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
var fileContent = string.Empty;
var filePath = string.Empty;
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.InitialDirectory = "c:\\";
openFileDialog.Filter = "zip files (*.zip)|*.zip";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
//Get the path of specified file
filePath = openFileDialog.FileName;
//Read the contents of the file into a stream
var fileStream = openFileDialog.OpenFile();
{
}
}
}
MessageBox.Show(fileContent, "File Content at path: " + filePath, MessageBoxButtons.OK);
}
}
}
我需要定義文件將被上傳到的位置(例如c:\\ ftp)。 該過程完成之后,我想向用戶顯示該文件所在的完整路徑,例如(filename = file.zip),而不是:ftp.mysite / file.zip
要復制文件,可以使用:
File.Copy(openFileDialog.FileName, "c:\\ftp\" + openFileDialog.SafeFileName,true);
獲取要保存的文件的路徑后,需要使用SaveFileDialog類。 using System.IO;
添加using System.IO;
使用FileInfo類。
private void SaveFile(string filePath)
{
FileInfo file = new FileInfo(filePath);
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
{
saveFileDialog.FileName = filePath;
if( saveFileDialog.ShowDialog() == DialogResult.OK)
{
MessageBox.Show("Your file was saved at " + saveFileDialog.FileName);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.