c#遍歷文件夾下的各種文件
private void ForeachFiles(string path)
{
DirectoryInfo theFolder = new DirectoryInfo(path);
DirectoryInfo[] dirInfo = theFolder.GetDirectories();//獲取所在目錄的文件夾
FileInfo[] file= theFolder.GetFiles();//獲取所在目錄的文件
foreach (FileInfo fileItem in file) //遍歷文件
{
var fileName = fileItem.Name;//文件名字
var dirName= fileItem.DirectoryName;//文件所在文件夾路徑
var sReader = fileItem.OpenText();
var contentStr = sReader.ReadToEnd();//讀取文件內(nèi)容,字符串
sReader.Close();//記得關(guān)閉
}
//遍歷文件夾
foreach (DirectoryInfo NextFolder in dirInfo)
{
ForeachFiles(NextFolder.FullName);
}
}
將一些log寫(xiě)入到文本文件中:
if (GUILayout.Button("拷貝日志", GUILayout.Width(200)))
{
string log = tipText.ToString();//要寫(xiě)入的字符串
if (!string.IsNullOrEmpty(log))
{
string tempFile = Path.GetTempFileName();//獲取一個(gè)臨時(shí)文件名
using (FileStream fs = new FileStream(tempFile, FileMode.Open))
{
fs.Seek(0, SeekOrigin.Begin);
fs.SetLength(0);//將文件流長(zhǎng)度設(shè)為0,清空其中的內(nèi)容
StreamWriter sw = new StreamWriter(fs);//專(zhuān)用于特定編碼的字符輸出
sw.Write(log);//寫(xiě)入
sw.Close();
fs.Close();
}
System.Diagnostics.Process.Start("notepad.exe", tempFile);//打開(kāi)這個(gè)臨時(shí)文本文件
}
}
fs.Seek(offset, whence);移動(dòng)文件讀取的指針到指定位置
offset:開(kāi)始的偏移量,也就是代表需要移動(dòng)偏移的字節(jié)數(shù)
whence:給offset參數(shù)一個(gè)定義,表示要從哪個(gè)位置開(kāi)始偏移;0代表從文件開(kāi)頭開(kāi)始算起,1代表從當(dāng)前位置開(kāi)始算起,2代表從文件末尾算起。whence值為空沒(méi)設(shè)置時(shí)會(huì)默認(rèn)為0。
System.Diagnostics.Process.Start(); 能做什么呢?它主要有以下幾個(gè)功能:
1、打開(kāi)某個(gè)鏈接網(wǎng)址(彈窗)。
2、定位打開(kāi)某個(gè)文件目錄。
3、打開(kāi)系統(tǒng)特殊文件夾,如“控制面板”等。
可見(jiàn)這篇博客:
http://t.csdn.cn/Q6G93
在editor上想輸出一些log:
private StringBuilder tipText = new StringBuilder();
private void PopupTipText(string newTip)
{
var time = System.DateTime.Now.ToLongTimeString();
if (tipText == null)
tipText = new StringBuilder();
tipText.Insert(0, time + "---" + newTip + "\n");
}
一些讀取目錄的方法:
Application.streamingAssetsPath:普通資源目錄
詳細(xì)的一些資料:http://t.csdn.cn/UbefR
AkBasePathGetter.DefaultBasePath:wwwise的資源文件路徑
讀取xml文件內(nèi)容相關(guān):
比如下列是讀取音頻相關(guān)內(nèi)容:
public static void GetWwiseProjectConfigStateData(out Dictionary<string, List<string>> audioConfigDict, string xmlPath)
{
audioConfigDict = new Dictionary<string, List<string>>();
string path = xmlPath;
if (File.Exists(path))
{
var doc = new System.Xml.XmlDocument();
doc.Load(path);
var StateGroups = doc.GetElementsByTagName("StateGroup");
for (var i = 0; i < StateGroups.Count; i++)
{
var nameStateGroups = StateGroups[i].Attributes["Name"].Value;//獲取標(biāo)簽名name對(duì)應(yīng)的內(nèi)容
var nameStateList = new List<string>();
var States = StateGroups[i].SelectNodes("States");
for (var j = 0; j < States.Count; j++)
{
var State = States[j].SelectNodes("State");
for (var k = 0; k < State.Count; k++)
{
var nameState = State[k].Attributes["Name"].Value;
nameStateList.Add(nameState);
if (audioConfigDict.ContainsKey(nameStateGroups) )
{
audioConfigDict[nameStateGroups] = nameStateList;
}
else
{
audioConfigDict.Add(nameStateGroups, nameStateList);
}
}
}
}
}
}
XmlDocument支持使用xpath表達(dá)式選擇文檔中節(jié)點(diǎn),方法:
SelectNodes(String expression)
SelectSingleNode(string expression)
SelectNodes 返回符合expression表達(dá)式的所有元素,返回值為XmlNodeList,比如:
XmlNodeList nodelist = xmlDoc.SelectNodes(“/CameraGroup/Camera”);//獲取所有的Camera節(jié)點(diǎn)。
SelectSingleNode只返回第一個(gè)符合expression表達(dá)式的節(jié)點(diǎn),如果沒(méi)有返回null值。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-430321.html
讀取一些表格:
public static List excelConfig = new List();文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-430321.html
public static Excel.ExcelTable GetExcelTable(string excelFileName, string tableName)
{
string excelPath = string.Format("/configs/{0}.xlsx", excelFileName);
var excel = new Excel.Excel(excelPath );
return excel.GetTable(tableName);
}
excelConfig.Add(GetExcelTable("表格名字", "表格分欄的sheet名字"));
到了這里,關(guān)于c#關(guān)于文件夾/文件/文本讀取遍歷,寫(xiě)入還有表格的讀取的一些方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!