C# winform 中文件存读:
1、点击添加,打开浏览框选择文件(如:.doc文件)。2、点击保存,复制1中选定的文件到指定目录(在程序中设定的绝对路径,如:D:\123\1),如果指定目录不
存在,则自动新建该目录,同时,将文件路径存入数据库。
3、点击查看,读取数据库中的文件路径,并调用相关程序打开(如:是.doc文件就自动调用word打开)。 --------------------编程问答-------------------- 这个很好做的,都是取路径,但打开word文件应该要用到浏览器控件,这个控件支持很多格式的,包括像PDF,最近我也刚做与这个差不多一样的功能 --------------------编程问答-------------------- --------------------编程问答-------------------- openfiledialog 打开、浏览文件控件 很简单获取路径
system.io 读写文件命名空间
filestream 操作文件的流对象
filestream fs=new filestream(路径);
fs.很多方法,自己 慢慢看 --------------------编程问答-------------------- 学习。。。
--------------------编程问答--------------------
--------------------编程问答-------------------- 楼上你说的是什么东西啊 --------------------编程问答--------------------
private void button1_Click(object sender, EventArgs e)//添加按钮
{
//选择文件名 就是你所要的添加
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title= "请选择要复制的文件";
ofd.ShowDialog();
string strRes = ofd.FileName;
textBox1.Text = strRes;
}
private void button2_Click(object sender, EventArgs e)//保存
{
string strDes = @"D:\test";
string strRes = textBox1.Text.ToString();
//保存并插入数据库
bool IsSuccess=CopyFile(strRes, strDes + "\\" + Path.GetFileName(strRes));
if(IsSuccess==true)
{
using(SqlConnection cn=new SqlConnection("server=(local);Initial Catalog=test;uid=sa;pwd=sa"))
{
try
{
//建表 ID自增 FileName Directory
string strsql = "insert into test(FileName,Directory) values(@FileName,@Directory)";
SqlCommand cmd = new SqlCommand(strsql, cn);
cmd.Parameters.AddWithValue("@FileName", Path.GetFileName(strRes));
cmd.Parameters.AddWithValue("@Directory",strDes);
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
}
catch
{
}
}
}
//复制
public bool CopyFile(string strRes,string strDes)
{
if (!File.Exists(strRes))
{
MessageBox.Show("文件不存在");
return false;
}
if(!Directory.Exists(Path.GetDirectoryName(strDes)))
{
Directory.CreateDirectory(Path.GetDirectoryName(strDes));
}
try
{
File.Copy(strRes, strDes, false);
return true;
}
catch
{
MessageBox.Show("拷贝失败");
return false;
}
}
private void button2_Click(object sender, EventArgs e)
{
string strPath = "";
//这个路径用Select从表中取出,拼接 你自己做吧 照着“保存”那里的方法
//查看
System.Diagnostics.Process.Start(strRes);
}
//判断目录无效 则自动创建。
private void CreateDir(string cPath)
{
string[] sPath = cPath.Split('\\');
string onePath=String.Empty;
for (int i = 0; i < sPath.Length-1; i++)
{
if (sPath[i].Trim() != String.Empty)
{
if (onePath.Trim() != String.Empty)
{
onePath = onePath + "\\" + sPath[i];
}
else
{
onePath = onePath + sPath[i];
}
if (!Directory.Exists(onePath))
{
Directory.CreateDirectory(onePath);
}
}
}
}
--------------------编程问答-------------------- 都是 C# 的文件操作啊,去调用相关类好了
补充:.NET技术 , C#