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();
sReader.Close();
}
foreach (DirectoryInfo NextFolder in dirInfo)
{
ForeachFiles(NextFolder.FullName);
}
}
将一些log写入到文本文件中:
if (GUILayout.Button("拷贝日志", GUILayout.Width(200)))
{
string log = tipText.ToString();
if (!string.IsNullOrEmpty(log))
{
string tempFile = Path.GetTempFileName();
using (FileStream fs = new FileStream(tempFile, FileMode.Open))
{
fs.Seek(0, SeekOrigin.Begin);
fs.SetLength(0);
StreamWriter sw = new StreamWriter(fs);
sw.Write(log);
sw.Close();
fs.Close();
}
System.Diagnostics.Process.Start("notepad.exe", tempFile);
}
}
fs.Seek(offset, whence);移动文件读取的指针到指定位置 offset:开始的偏移量,也就是代表需要移动偏移的字节数 whence:给offset参数一个定义,表示要从哪个位置开始偏移;0代表从文件开头开始算起,1代表从当前位置开始算起,2代表从文件末尾算起。whence值为空没设置时会默认为0。
System.Diagnostics.Process.Start(); 能做什么呢?它主要有以下几个功能: 1、打开某个链接网址(弹窗)。 2、定位打开某个文件目录。 3、打开系统特殊文件夹,如“控制面板”等。 可见这篇博客: 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:普通资源目录 详细的一些资料:http://t.csdn.cn/UbefR AkBasePathGetter.DefaultBasePath:wwwise的资源文件路径
读取xml文件内容相关: 比如下列是读取音频相关内容:
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;
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表达式选择文档中节点,方法: SelectNodes(String expression) SelectSingleNode(string expression) SelectNodes 返回符合expression表达式的所有元素,返回值为XmlNodeList,比如: XmlNodeList nodelist = xmlDoc.SelectNodes(“/CameraGroup/Camera”);//获取所有的Camera节点。 SelectSingleNode只返回第一个符合expression表达式的节点,如果没有返回null值。
读取一些表格: public static List excelConfig = new List();
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名字"));
|