C# StreamReader中的子字符串错误
您好,我在为Unity3D编写编辑器时遇到了问题,我遇到了一个问题:我从一个包含常规字符串的.txt文件中读取行,然后在每个常规字符串下面读取文件扩展名(表示扩展名的类别)。当我试图在分配给下一行的字符串上运行子字符串时,问题就出现了。当我尝试使用任何子字符串时,文件显示为未成功打开,但如果没有它,则可以正常打开C# StreamReader中的子字符串错误,c#,unity3d,substring,streamreader,unity5,C#,Unity3d,Substring,Streamreader,Unity5,您好,我在为Unity3D编写编辑器时遇到了问题,我遇到了一个问题:我从一个包含常规字符串的.txt文件中读取行,然后在每个常规字符串下面读取文件扩展名(表示扩展名的类别)。当我试图在分配给下一行的字符串上运行子字符串时,问题就出现了。当我尝试使用任何子字符串时,文件显示为未成功打开,但如果没有它,则可以正常打开 public bool PopulateList() { bool success = true; string path = "Assets/Scripts/Edit
public bool PopulateList()
{
bool success = true;
string path = "Assets/Scripts/Editor/Extensions.txt";
sourceFile = new FileInfo("Assets/Scripts/Editor/Extensions.txt");
if (!File.Exists(path))
{
Debug.Log("File Does Not Exist");
TextWriter tw = new StreamWriter(path, true);
tw.Close();
}
string line;
ExtensionUnit anExtension;
try
{
StreamReader myStreamReader = sourceFile.OpenText();
line = myStreamReader.ReadLine();
while (!myStreamReader.EndOfStream)
{
anExtension = new ExtensionUnit();
anExtension.Categories = line;
line = myStreamReader.ReadLine();
/*if(line.Substring(0,1) == ".")
{
//Debug.Log(line.Substring(0, 1));
}*/
//Debug.Log(line.Substring(0, 1));
/*while(line.Substring(0,1) == ".")
{
anExtension.Extensions = line;
theExtensions.Add(anExtension);
//Next extension
line = myStreamReader.ReadLine();
}*/
//Empty blank space
line = myStreamReader.ReadLine();
}
myStreamReader.Close();
}
catch
{
success = false;
}
return success;
}
}尝试在不打开文件的情况下执行此操作:
StreamReader myStreamReader = new StreamReader(sourceFile.FullName);
首先读取所有文件数据,然后执行所有逻辑(子字符串e.t.c.)
wellcome到堆栈溢出。你能展示文本文件的例子吗?你能不能不仅仅使用文本资源?@JoeBlow我会使用文本资源,但我不使用Unity引擎库,只使用Unity编辑器库
public bool PopulateList()
{
var success = true;
var path = "Assets/Scripts/Editor/Extensions.txt";
if (File.Exists(path))
{
try
{
var fileContent = File.ReadAllLines(path);
foreach (var line in fileContent)
{
// Define what lines do you need and get needed extensions
}
}
catch (Exception ex)
{
Log(ex); // it`s better to know the reason at least
success = false;
}
}
return succes;
}