1. 程式人生 > >C# 讀取指定文件夾中的全部文件,並按規則生成SQL語句!

C# 讀取指定文件夾中的全部文件,並按規則生成SQL語句!

fun .text line -m ava inf hid 語句 ade

本實例的目的在於: 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);
}
}
}
}
‘).text(i)); }; $numbering.fadeIn(1700); }); });

C# 讀取指定文件夾中的全部文件,並按規則生成SQL語句!