将文件从openfiledialog保存到项目文件夹和数据库中的路径 [英] Save file from openfiledialog to project folder and path in database

查看:180
本文介绍了将文件从openfiledialog保存到项目文件夹和数据库中的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文件从openfiledialog保存到项目文件夹和数据库中的路径

请帮帮我。



我是什么尝试过:



i want to save file from openfiledialog to project folder and path in database
Please help me.

What I have tried:

 private void button6_Click(object sender, EventArgs e)

        {         
        
         DialogResult result = openFileDialog1.ShowDialog();
if (result == DialogResult.OK)
{
 
String file = openFileDialog1.FileName;
string[] f = file.Split('\\');
string fn = f[(f.Length) - 1];
string dest = @"C:\Users\admin\source\repos\Software of TE\Software of TE\Images\";

File.Copy(file, dest);

        }

推荐答案

要将文件复制到项目目录,您可以这样做:



To copy the file to your project directory, you can do:

string source = openFileDialog1.FileName;
string dest = @"C:\Users\admin\source\repos\Software of TE\Software of TE\Images\" + Path.GetFileName(source);
File.Copy(source, dest);





字符串 dest 现在保存可以存储在数据库中的文件名。

请注意,File.Copy可能会引发异常,您可能希望捕获该异常。



The string dest now holds the filename that you can store in your database.
Note that File.Copy may throw an exception, which you might want to catch.


这篇关于将文件从openfiledialog保存到项目文件夹和数据库中的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆