C# 选择文件并将其上载到特定文件夹
我需要上传一个选定的文件到一个特定的文件夹。我有以下代码:C# 选择文件并将其上载到特定文件夹,c#,C#,我需要上传一个选定的文件到一个特定的文件夹。我有以下代码: using System.IO; namespace FTP_UPLOAD { public partial class FTPUPLOAD : Form { public FTPUPLOAD() { InitializeComponent(); } private void Button1_Click(object sender
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);
要复制文件,可以使用:
File.Copy(openFileDialog.FileName, "c:\\ftp\" + openFileDialog.SafeFileName,true);
获取要保存的文件的路径后,需要使用SaveFileDialog类。使用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);
}
}
}
获取要保存的文件的路径后,需要使用SaveFileDialog类。使用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);
}
}
}
那你有什么问题?您得到了什么?您期望得到什么?我可以选择该文件,但我需要将其复制到该文件夹(c:\ftp)。完成后,显示该文件的路径。那么您的问题是什么?您得到了什么?您期望得到什么?我可以选择该文件,但我需要将其复制到该文件夹(c:\ftp)。完成后,显示该文件的路径。请确认哪个答案对您有帮助。请确认哪个答案对您有帮助。