lambda排序:
1.按照多个字段进行排序:xxxList.OrderBy(c => c.RoadCode).ThenBy(c => c.Qdzh),表示先按照RoadCode字段进行排序再按照Qdzh字段进行排序
2.自定义条件排序:xxxList.OrderBy(c=>c.RoadName.StartsWith("C")?"Z": c.RoadName),这里按照RoadCode字段进行排序,但是RoadCode字段的值以“C”开头的则替换字段的值为“Z”。等价于按照RoadCode字段排序,但是如果RoadCode字段的值以"C"开头则排到最后。
这里只是做两个示例,具体按自己的需求灵活编写
获取时间标识符:
var now = DateTime.Now.Ticks;//格式类似637327365574840364,表示到当前时间的毫秒总数
解压zip文件:
ZipFile.ExtractToDirectory(saveZipPath, extractDirectory);
获取文件夹下面的所有文件和文件夹:
DirectoryInfo root = new DirectoryInfo(directoryPath);
FileInfo[] files = root.GetFiles();
DirectoryInfo[] dics = root.GetDirectories();
从一个文件夹路径中获取该文件夹的名称:
DirectoryInfo root = new DirectoryInfo(directoryPath);
var name = root.name;//如路径时c://aaa/fff/vvv 结果是:vvv
从一个文件(或文件夹)路径中获取该文件所在的文件夹路径:
Path.GetDirectoryName(filePath);//如C://aa/a/b.txt 结果是C://aa/a
Path.GetDirectoryName(filePath);//如C://aa/a/ 结果是C://aa
删除文件夹:
Directory.Delete(path);//只能删除空文件夹
var dir= new DirectoryInfo(path);
dir.Delete(true);//删除文件夹以及下属的所有内容
创建文件夹:
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
读取txt文件:
var txt="";
var sr = new StreamReader(@"E: est.txt");
while (!sr.EndOfStream)
{
string str = sr.ReadLine();
txt += str + "
";
}
sr.Close();
写入txt文件:
StreamWriter sw = new StreamWriter("test.txt")
sw .WriteLine("test1");
sw .WriteLine("test2");
sw .Close();
复制文件:
string pLocalFilePath ="";//要复制的文件路径
string pSaveFilePath ="";//指定存储的路径
if (File.Exists(pLocalFilePath))//判断要复制的文件是否存在
{
File.Copy(pLocalFilePath, pSaveFilePath, true);//三个参数分别是源文件路径,存储路径,若存储路径有相同文件是否替换
}
将文件流保存为文件:
using (FileStream fs = new FileStream(savePath, FileMode.Create, FileAccess.Write))
{
file.CopyTo(fs);
fs.Flush();
}
枚举的写法:
public enum OgrDriverEmun
{
ESRI_Shapefile,
PostgreSQL,
GeoJSON
}
根据枚举值获取上面枚举中的字符串: var driverStr = Enum.Parse(typeof(OgrDriverEmun), Enum.GetName(typeof(OgrDriverEmun), OgrDriverEmun.PostgreSQL)).ToString();