C#从.txt文件中获取文本,该文件为';它被添加到程序中
在我的项目中,我添加了一个C#从.txt文件中获取文本,该文件为';它被添加到程序中,c#,string,file,stream,C#,String,File,Stream,在我的项目中,我添加了一个.txt文件。我需要把内容放在里面,一行接一行,然后逐行删减。我已经准备好了代码,可以像我想要的那样滑动行并整体处理.txt文件的内容,我需要的是访问添加文件的内容 我必须在计算机中以txt文件的形式处理文本的代码: public static string[] loc_file = File.ReadAllLines(AppDomain.CurrentDomain.BaseDirectory + "\\loc.txt", Encoding.UTF8);
.txt
文件。我需要把内容放在里面,一行接一行,然后逐行删减。我已经准备好了代码,可以像我想要的那样滑动行并整体处理.txt
文件的内容,我需要的是访问添加文件的内容
我必须在计算机中以txt文件的形式处理文本的代码:
public static string[] loc_file = File.ReadAllLines(AppDomain.CurrentDomain.BaseDirectory + "\\loc.txt", Encoding.UTF8);
public static string loc_up = string.Join("|", loc_file);
public static string[] loc_p = loc_up.Split('|');
public static string[] loc = loc_p.Where((c, i) => i % 2 == 0).ToArray<string>();
public static string[] loc_txt = loc_p.Where((c, i) => i % 2 != 0).ToArray<string>();
然后从文件中读取文本
谢谢
编辑1:
我自己编写这段代码,不确定是否有效,但我会发布公告,如果最终有效,我可能会帮助他人:
public bool get_file(string file)
{
string filePath = Application.StartupPath.ToString() + file;
if (File.Exists(filePath))
{
try
{
Stream stream = GetType().Assembly.GetManifestResourceStream(file);
string[] a = GetType().Assembly.GetManifestResourceNames();
byte[] bytes = new byte[(int)stream.Length];
stream.Read(bytes, 0, bytes.Length);
File.WriteAllBytes(filePath, bytes);
return true;
}
catch { return false; }
}
else { return false; }
}
编辑2:
我刚刚意识到stringfilepath=Application.StartupPath.ToString()+文件;
如果(File.Exists(filePath))
将给我错误,因为在的层没有文件,让我们称之为循环。因此,我将删除该部分以查看文件是否存在,因为它没有意义,代码保留为:
public bool get_file(string file)
{
string filePath = Application.StartupPath.ToString() + file;
try
{
Stream stream = GetType().Assembly.GetManifestResourceStream(file);
string[] a = GetType().Assembly.GetManifestResourceNames();
byte[] bytes = new byte[(int)stream.Length];
stream.Read(bytes, 0, bytes.Length);
File.WriteAllBytes(filePath, bytes);
return true;
}
catch { return false; }
}
这将帮助你非常感谢!
public bool get_file(string file)
{
string filePath = Application.StartupPath.ToString() + file;
try
{
Stream stream = GetType().Assembly.GetManifestResourceStream(file);
string[] a = GetType().Assembly.GetManifestResourceNames();
byte[] bytes = new byte[(int)stream.Length];
stream.Read(bytes, 0, bytes.Length);
File.WriteAllBytes(filePath, bytes);
return true;
}
catch { return false; }
}