您的位置:首页 > 其它

I/O小总结

2015-04-11 14:18 113 查看
//判断不存在就创建目录 ,然后拷贝文件

DirectoryInfo di = null;

if (!Directory.Exists(n.Attribute("value").Value + goalPath))
{
di = Directory.CreateDirectory(n.Attribute("value").Value + goalPath);
FileInfo file = new FileInfo(path);
File.Copy(path, (di.FullName + "\\" + file.Name), true);
}
其中,

File.Copy方法表示吧文件从哪复制到哪,true的意思是可以覆盖之前有的文件,这个只能用于文件对文件的操作,不能用于文件对文件夹的操作



//显示进度条和统计数据发送多少
//countNum:总数
//value:当前数
//控件使用了跨线程的访问方式

public void ShowData(int countNum,int value)
{

//总数据
if (progressBar1.InvokeRequired)
{
progressBar1.Invoke(new Action<int>(s => { this.progressBar1.Maximum = s; }), countNum + 2);
}
else
{
this.progressBar1.Maximum = countNum+2;
}

//当前数据
if (progressBar1.InvokeRequired)
{
//value包括了标题行,value-1是去掉标题行
progressBar1.Invoke(new Action<int>(s => { this.progressBar1.Value = s; }), value-1);
}
else
{
this.progressBar1.Value = value-1;
}

//记录进度
if (lbRecord.InvokeRequired)
{
lbRecord.Invoke(new Action<string>(s => { this.lbRecord.Text = s; }), "当前数据一共:" + sheetCount + "条,已导入:" + (Convert.ToInt32(value - 1)) + "条");
}
else
{
this.lbRecord.Text = "当前数据一共:" + sheetCount + "条,已导入:" + (Convert.ToInt32(value-1)) + "条";
}
}


//使用config文件保存文件的根目录

//导入的时候用到的config里面的节点根目录

XDocument document = new XDocument();
//加载文件
document = XDocument.Load(@"..\..\App.config");
//读取根节点
XElement root = document.Root;

XElement n = root.Elements("rootFile").Where(u => u.Attribute("name").Value == "rf").ElementAt(0);

//config的设置,第一步注册

<configSections>
<section name="rootFile" type="System.Configuration.IgnoreSectionHandler"/>
</configSections>

//第二步创建节点

<rootFile name="rf" value="D:\影像文件\" />


//寻找对应的要导入的目录下的PDF文件
string[] dirs = Directory.GetFiles(this.txtFilePath.Text, fileNumber + ".pdf", SearchOption.AllDirectories);

这个可以查找各种格式的文件,是递归文件夹的查询方式,但是文件夹嵌套过多的时候还是建议使用递归的方式


//获取文件名,就是获取file.Extension扩展名以后替换成空字符串
file.Name.Replace(file.Extension, "")

//获取扩展名,这个扩展名是带点的,所以也要替换掉

file.Extension.Replace(".", "")
他们是由fileinfo类实例化来的


另外,执行多个操作数据库方法同时成功的时候要在同一个连接下使用事务con.open只执行一次就好了。trans.roolback要在异常块执行

//这段是点击按钮以后出现文件夹选项窗口,可以获取该文件夹下面的所有文件
FolderBrowserDialog fd = new FolderBrowserDialog();
if (fd.ShowDialog() == DialogResult.OK)
{
//操作
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: