单机版程序上传,只能上传到本地目录,并不适合网络应用,看来只能单机版能这么用。网络程序要使用上传图片还得继续改造。
private void btnUpload_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "请选择上传的图片"; ofd.Filter = "图片格式|*.jpg"; //设置是否允许多选 ofd.Multiselect = false; if (ofd.ShowDialog()== System.Windows.Forms.DialogResult.OK) { //获得文件的完整路径(包括名字后后缀) string filePath = ofd.FileName; tbImgFile.Text = filePath; int position = filePath.LastIndexOf("\\"); string fileName = filePath.Substring(position+1); File.Copy(ofd.FileName, Application.StartupPath + "\\Upload\\" + fileName); pbImage.ImageLocation = Application.StartupPath + "\\Upload\\" + fileName; } }
以上是C# Winform程序上传图片到指定目录的示例代码的详细内容。更多信息请关注PHP中文网其他相关文章!