琐碎--选择文件夹(路径)+生产txt格式的log+数据库操作方式
记录日常工作常用到的一些方法:
在.Net中处理系统文件相关的几个类分别是File、Directory、FileInfo、DirectoryInfo、DriveInfo、FileSystemWatcher。本文介绍下这几个类的用法。
1.File类提供静态方法用来创建、移动、复制、删除文件的操作,并可以打开文件流
2.Directory类提供静态方法用来创建、移动、复制、删除目录的操作
3.FileInfo类用类实例实现创建、复制、移动、删除文件的操作
4.DirectoryInfo提供创建、移动、复制、删除目录的操作,并可以枚举子目录
5.DriveInfo可以获得windows操作系统中的磁盘信息
6.FileSystemWatcher用来监视文件或目录变化,并引发事件
7.Path类提供文件名目录名操作的静态方法
1 选择文件操作,并将文件的路径记录下来:
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = false;
ofd.Title = "请选择文件";
ofd.Filter = "(*.*)|*.*";
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
tb1.Text = ofd.FileName;//文件的路径
}
2 选择文件夹的操作,并将文件夹的路径记录下来:
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "请选择文件夹";
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string str = fbd.SelectedPath;//文件夹的路径
}
3 、生成txt格式的log:
public void WriteLog(string txt)
{
try
{
txt = DateTime.Now.ToString() + " " + txt; string str = System.IO.Directory.GetCurrentDirectory();
string firstStr = str.Substring(, );
//生成文件夹
string filesPath = firstStr + ":/倒计时日志";
if (!Directory.Exists(filesPath))
{
Directory.CreateDirectory(filesPath);
} string filePath = filesPath + "/" + DateTime.Now.ToLongDateString()+ ".txt";
System.IO.FileStream fs = new System.IO.FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.Write | FileShare.ReadWrite | FileShare.Read);
fs.Close();
System.IO.StreamWriter sw = new StreamWriter(@filePath, true, Encoding.Unicode);
sw.WriteLine(txt);
sw.Close();
}
catch (Exception ex)
{
}
}
4 操作数据库常用方法:
数据库连接:static string connectionString = "Data Source=192.168.100.46;Initial Catalog=ExamDB;User ID=sa;Password=123";//连接数据库语句
查询数据库,返回一个数据集:
public static DataSet Query(string sqlString)
{
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
sqlConnection.Open();
SqlDataAdapter sqlDataApater = new SqlDataAdapter(sqlString, sqlConnection);
sqlDataApater.Fill(ds, "ds");
}
catch (SqlException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
public static DataSet Query2(string sqlString)
{
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
{
DataSet ds = new DataSet();
try
{
sqlConnection.Open();
sqlCommand.CommandText = sqlString;
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
sqlDataAdapter.Fill(ds);
}
catch (SqlException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
}
执行数据库操作,返回影响的行数:
public static int ExecuteSql(string sqlString)
{
using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
using (SqlCommand sqlCommand = new SqlCommand(sqlString, sqlConnection))
{
try
{
sqlConnection.Open();
int rows = sqlCommand.ExecuteNonQuery();
return rows;
}
catch (SqlException ex)
{
sqlConnection.Close();
throw new Exception(ex.Message);
}
}
}
}
5、生成文件夹
string str = System.IO.Directory.GetCurrentDirectory();
string firstStr = str.Substring(, );
string filesPath = firstStr + ":/学习";
if (!Directory.Exists(filesPath))
{
Directory.CreateDirectory(filesPath);
}
6、获取指定文件内的文件
var imagedir = @"g:\ab/c";
var imageDirInfo = new DirectoryInfo(imagedir);
if (!imageDirInfo.Exists)
{
imageDirInfo.Create();
var everyOneSecuity = new DirectorySecurity(imagedir, AccessControlSections.Owner);
imageDirInfo.SetAccessControl(everyOneSecuity);
}
var filesInfo = imageDirInfo.GetFiles();
Array.Sort(filesInfo, new FIleLastTimeComparer());
var filesNameStr = string.Empty;
filesInfo.ToList().ForEach(f => filesNameStr += f.Name + "|");
string ss = filesNameStr;
获取指定路径path内格式为*.jpg的文件:
方法一:
string[] files = Directory.GetFiles(path, "*.jpg");
foreach (var dcmFile in files)
{
}
方法二:
DirectoryInfo dInfo = new DirectoryInfo(newPath);
foreach (FileInfo dcmFile in dInfo.GetFiles("*.jpg"))
{var newDcmFile = dcmFile.Name; }
7、执行.exe应用程序:
public static void Dcm2Img(string oriDcm, string descImg)
{
Process p = new Process();
p.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
p.StartInfo.FileName = AppDomain.CurrentDomain.BaseDirectory + @"dcmtk\dcmj2pnm.exe";
p.StartInfo.UseShellExecute = false; //是否使用外壳程序
p.StartInfo.CreateNoWindow = true; //是否在新窗口中启动该进程的值
p.StartInfo.RedirectStandardInput = true; //重定向输入流
p.StartInfo.RedirectStandardOutput = true; //重定向输出流
p.StartInfo.RedirectStandardError = true; //重定向错误流
//p.StartInfo.Arguments = @"+oj e:\a.dcm 1.jpg";
p.StartInfo.Arguments = string.Format("+oj {0} {1}", oriDcm, descImg);
p.Start();
p.WaitForExit();
p.Close();
}
//
引用:
.Net那点事儿系列:System.IO之windows文件操作
琐碎--选择文件夹(路径)+生产txt格式的log+数据库操作方式的更多相关文章
- VB6 选择文件夹路径
'--------------------------------------------------------------------------------------- ' Module : ...
- js选择文件夹路径
该方法只支持IE. 语法:strDir=Shell.BrowseForFolder(Hwnd,Title,Options,[RootFolder])参数:Hwnd:包含对话框的窗体句柄(handle) ...
- 修改ckeditor/ckfinder上传文件文件夹 路径以日期格式命名
修改/ckfinder/config.ascx文件: string dateDir = DateTime.Today.ToString("yyyyMM/"); ResourceTy ...
- Qt获取文件路径、文件夹路径
1.首先是选择文件 QString file_path = QFileDialog::getOpenFileName(this, "请选择文件路径...", "默认路径( ...
- C# 选择文件、选择文件夹、打开文件(或者文件夹) 路径中获取文件全路径、目录、扩展名、文件名称 追加、拷贝、删除、移动文件、创建目录 修改文件名、文件夹名!!
https://www.cnblogs.com/zhlziliaoku/p/5241097.html 1.选择文件用OpenDialog OpenFileDialog dialog = new Ope ...
- MFC 选择一个文件或者文件夹路径
//选择文件CFileDialog dlg(TRUE, 0, 0, OFN_HIDEREADONLY, "文本文件|*.txt|所有文件|*.*)||",0);if (dlg.Do ...
- java 弹出选择目录框(选择文件夹),获取选择的文件夹路径
java 弹出选择目录框(选择文件夹),获取选择的文件夹路径 java 弹出选择目录框(选择文件夹),获取选择的文件夹路径:int result = 0;File file = null;String ...
- 键盘录入一个文件夹路径,统计该文件夹(包含子文件夹)中每种类型的文件及个数,注意:用文件类型(后缀名,不包含.(点),如:"java","txt")作为key, 用个数作为value,放入到map集合中,遍历map集合
package cn.it.zuoye5; import java.io.File;import java.util.HashMap;import java.util.Iterator;import ...
- MFC 打开文件夹选择框并获取文件夹路径
CString FicowGetDirectory() { BROWSEINFO bi; char name[MAX_PATH]; ZeroMemory(&bi, sizeof(BROWSEI ...
随机推荐
- 给Apache增加SSI支持(shtml的奥秘)
什么是SSI? SSI是英文Server Side Includes的缩写,翻译成中文就是服务器端包含的意思.从技术角度上说,SSI就是在HTML文件中,可以通过注释行调用的命令或指针.SSI具有强大 ...
- I Hate It(线段数组基础题)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- Linux学习之三——操作档案与目录
一. 目录文档操作指令 1. pwd 显示目前所在目录 如果加上-P 的选项,则取得正确的目录名称,而不是以链接文件的路径来显示. 例如CentOS下,刚刚好/var/mail是/var/spool/ ...
- WCF MSMQ消息队列与离线操作
消息队列类型 公共队列:整个网络中的节点都可以访问到 专用队列:本地计算机,知道路径者可以访问 管理队列:包含确认在给定“消息队列”网络中的发送的消息回执的消息 相应队列:返回给发送程序的相应信息
- 1.素数判定(如何输出\n,\t,不用关键字冲突)
题目描述 Description 质数又称素数.指在一个大于1的自然数中,除了1和此整数自身外,不能被其他自然数整除的数. 素数在数论中有着很重要的地位.比1大但不是素数的数称为合数.1和0既非素数也 ...
- Network client/server
<Beginning Linux Programming_4th> chapter 15 Sockets 1 A simple local client/server 1) clie ...
- 图的遍历之深度优先搜索(DFS)
深度优先搜索(depth-first search)是对先序遍历(preorder traversal)的推广.”深度优先搜索“,顾名思义就是尽可能深的搜索一个图.想象你是身处一个迷宫的入口,迷宫中的 ...
- 边工作边刷题:70天一遍leetcode: day 76
Count Univalue Subtrees 要点:检测条件比较有意思:因为可能的情况比较多,只要违反了任意一条就return False,所以可以只考虑False的情况,最后return True ...
- 搜索服务solr 一二事(1) - solr-5.5 使用自带Jetty或者tomcat 搭建单机版搜索服务器
solr,什么是solr,就是你要吃的东西“馊了”,不能吃了,out of date~ 嘛...开个玩笑,发音就是‘搜了’,专门用于搜索的一个开源框架,lunce就不说了,不好用,麻烦 来讲讲solr ...
- Xcode中文乱码问题
老师给拷贝的程序用Xcode打开中文显示是乱码,而预览里面是正常显示的,Xcode默认编码UTF-8没错的,怎么办呢? 解决办法:用自带的文本编辑器打开,全选,复制,Xcode中打开文件,粘贴,ok~ ...