首页 > 代码库 > C# 读取指定文件夹中的全部文件,并按规则生成SQL语句!

C# 读取指定文件夹中的全部文件,并按规则生成SQL语句!

本实例的目的在于: 1 了解怎样遍历指定文件夹中的全部文件 2 控制台怎样输入和输出数据 代码:
using System;
using System.IO;

namespace ToSql{
class ToSql{
static void Main(){
string path = System.Environment.CurrentDirectory;
System.Console.WriteLine("输入要读取的文件夹名");
string name = Console.ReadLine();
path += "\\" + name;
GetAllFile(path);
}

public static void GetAllFile(string path){
DirectoryInfo di = new DirectoryInfo(path);
DirectoryInfo[] dis = di.GetDirectories();
foreach(DirectoryInfo xdi in dis){
System.Console.WriteLine(xdi.Name);
GetAllFile(xdi.FullName);
}

FileInfo[] files = di.GetFiles();
string sql = "";
string picurl = "";
string id = "";
foreach(FileInfo file in files){
id = (file.Name).Replace("S_","").Replace(".jpg","").Replace(".png","");
picurl = "/uploads/supplyer/" + file.Name;
sql = "UPDATE `supplyer` SET picurl=‘"+ picurl +"‘ WHERE id=" + id + ";";
System.Console.WriteLine(sql);
}
}
}
}
<script type="text/javascript"> $(function () { $(‘pre.prettyprint code‘).each(function () { var lines = $(this).text().split(‘\n‘).length; var $numbering = $(‘
    ‘).addClass(‘pre-numbering‘).hide(); $(this).addClass(‘has-numbering‘).parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($(‘
  • ‘).text(i)); }; $numbering.fadeIn(1700); }); }); </script>

C# 读取指定文件夹中的全部文件,并按规则生成SQL语句!