您的位置:首页 > 编程语言 > C#

C# 上传文件

2015-08-27 16:24 531 查看
using System;

using System.Text;

using System.Data;

using System.Windows.Forms;

using System.IO;

//拷贝一份想要上传的文件放到指定的程序目录下

public string UploadFile()  //返回上传的文件名  包含路径

{

 OpenFileDialog ofd = new OpenFileDialog();

 ofd.Filter = "Excel文件(*.xls)|*.xls|所有文件(*.*)|*.*";

 ofd.Title = "选择你要上传的文件";

 ofd.ShowDialog();

 if (ofd.FileName.Length > 0)

 {

  string path = System.Windows.Forms.Application.ExecutablePath;

  FileInfo exeInfo = new FileInfo(path);

  path = exeInfo.DirectoryName + "\\";   //程序根目录

  string tempExcel = path + "user data\\temp\\"+ ofd.FileName.Split(new Char[] { '\\' }).Last();  //上传后保存在程序目录下的文件名

  if (File.Exists(tempExcel))

  {

   string just_name = ofd.FileName.Split(new Char[] { '\\' }).Last();  //不包含路径的文件名

   DialogResult result = MessageBox.Show(just_name + "已经存在,确认覆盖?", "文件上传提示", MessageBoxButtons.OKCancel);

   if (result == DialogResult.OK)

   {

    File.Copy(ofd.FileName, tempExcel, true);

    return ofd.FileName;

   }else{

    return "未选择文件!";

   }

  }else{

   File.Copy(ofd.FileName, tempExcel, true);

   return ofd.FileName;

  }

 }else{

  return "未选择文件!";

 }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C# 文件上传