首先设置Form的AllowDrop=true
public partial class Form1 : Form{
public Form1()
{
InitializeComponent()
}
private void btnStart_Click(object sender, EventArgs e)
{
}
private void Form1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Link
}
else
{
e.Effect = DragDropEffects.None
}
}
private void Form1_DragDrop(object sender, DragEventArgs e)
{
string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString()
}
}
在DragDrop事件中能够得到拖放到窗体上的文件路径,然后使用filestream就可以上传了。
//文件写入流private void ReadFile()
{
Byte[] MesageFile
string path =@"c:\123.XML"
FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read)
int size = Convert.ToInt32(stream.Length)
MesageFile = new Byte[size]
stream.Read(MesageFile, 0, size)
stream.Close()
string fileName =path.Substring(path.LastIndexOf("\\") + 1, path.Length path.LastIndexOf("\\") - 1)
WriteFile(MesageFile, fileName)
}
//写入文件
private void WriteFile(Byte[] fileByte,string fileName)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "\\UpLoad\\" + DateTime.Now.ToString("yyyy-MM-dd")+"\\"
if (!Directory.Exists(path))
Directory.CreateDirectory(path)
string savepath = path + fileName
FileStream fos = new FileStream(savepath, FileMode.OpenOrCreate, FileAccess.ReadWrite)
fos.Write(MesageFile, 0, MesageFile.Length)
fos.Close()
}
上传的文件格式不限。
string src=""if(openFileDialog.showDialog==DialogResult.OK)
{
src=openFileDialog.filename.toString()
image.save("图片名",src)
}
具体放哪你看着办吧!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)