1.字符串转时间格式:
将一段字符串转换为时间,如图
圈出来的是字符串转换为时间的格式,如图
代码如下:
//首先将字符串转为时间
DateTime dateTimeWhatDay = Convert.ToDateTime(acquireTime[1].Substring(0, 4) +
"-" + acquireTime[1].Substring(4, 2) + "-" + acquireTime[1].Substring(6, 2) +
" " + acquireTime[1].Substring(9, 2) + ":" + acquireTime[1].Substring(11, 2) +
":" + acquireTime[1].Substring(13, 2));
//将时间缩写
string abbreviation = dateTimeWhatDay.ToString("r", DateTimeFormatInfo.InvariantInfo);
//获取当前星期几的缩写 Thu
string whatDay = abbreviation.Substring(0, 3);
//获取月份日期缩写 Dec 10
string month = abbreviation.Substring(8, 3) + " " + abbreviation.Substring(5, 2) + " ";
//获取时分秒毫 13:59:26.000
string dateTime = dateTimeWhatDay.ToString("HH:mm:ss") + ".000";
//12小时制,判断是上午还是下午 pm
string pmOrAm = Convert.ToInt32(dateTimeWhatDay.ToString("HH")) > 12 ? "pm " : "am ";
//获取年份 2020
string year = dateTimeWhatDay.Year.ToString();
//拼接得到时间格式 Thu Dec 10 13:59:26.000pm 2020
string timeFormat = whatDay + month + dateTime + pmOrAm + year;
2.将文件复制到其他目录下:
//执行方法
private void AllExportFileasc(object sender, RoutedEventArgs e)
{
//获取文件路径
string srcPath = System.AppDomain.CurrentDomain.BaseDirectory + "Files\\Temp\\";
if (!Directory.Exists(srcPath))
{
Directory.CreateDirectory(srcPath); //文件夹不存在:创建文件夹
}
DirectoryInfo dir = new DirectoryInfo(srcPath);
//获取目录下的文件(不包含子目录)
FileSystemInfo[] fileinfo = dir.GetFileSystemInfos();
if (fileinfo.Count() > 0)
{
using (System.Windows.Forms.FolderBrowserDialog folder = new System.Windows.Forms.FolderBrowserDialog())
{
folder.RootFolder = Environment.SpecialFolder.Desktop;
if (folder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
var saveFilePath = folder.SelectedPath;//获取存放文件夹的路径
foreach (FileSystemInfo item in fileinfo)
{
//复制文件
File.Copy(item.FullName, saveFilePath + "\\" + item.Name, true);
}
MessageBox.Show("复制文件成功!");
}
}
}
else
{
MessageBox.Show("没有可复制的文件!");
}
}