[英]How do I write at a specific index of a line?
Good day Stackoverflow, 美好的一天Stackoverflow,
I want to write some text at a specific point of a line in a txt-file. 我想在txt文件的一行的特定点写一些文本。 So I want to write something between two
;
所以我想在两个之间写点东西
;
in the line. 在行中。 The Line of the textfile is:
20180912_0149;KIV\\vischer;12.09.2018;01:49;;
文本文件的行是:
20180912_0149;KIV\\vischer;12.09.2018;01:49;;
. 。
I want to write it if I click the "Geht" Button. 如果我单击“ Geht”按钮,我想写它。 I tried different things with streamreader and writer but I don't get to a solution.
我用streamreader和writer尝试了不同的方法,但没有解决方案。
Here is my code of the aspx.cs and my helper class: 这是我的aspx.cs和助手类的代码:
Aspx.cs: Aspx.cs:
namespace Zieterfassung_0._0._2pre_alpha
{
public partial class Zeiten : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string sPath = @"C:\VSTO\Projects\Zeiterfassung\Zeiterfassung\obj\Debug\Zeiten.txt";
tb_User.Text = WindowsIdentity.GetCurrent().Name.ToString();
tb_Datum.Text = DateTime.Now.ToString("dd.MM.yyyy");
tb_Zeit.Text = DateTime.Now.ToString("hh:mm");
cZeile KommtDatumZeit = new cZeile();
if (File.Exists(sPath))
{
using (StreamReader sr = new StreamReader(sPath))
{
while (!sr.EndOfStream)
{
KommtDatumZeit = cZeiterfassung.GetZeileObjectFromZeileString(sr.ReadLine(), ";");
}
}
}
tb_Kommt.Text = KommtDatumZeit.dtKommt.ToString();
}
protected void btn_Geht_Click(object sender, EventArgs e)
{
string sAusgabeGeht = string.Format("{0:hh:mm}", tb_Zeit.Text);
string sPath = @"C:\VSTO\Projects\Zeiterfassung\Zeiterfassung\obj\Debug\Zeiten.txt";
FileInfo fi = new FileInfo(sPath);
cZeile Geht = new cZeile();
using (StreamReader sr = new StreamReader(sPath))
{
Geht = cZeiterfassung.GetZeileObjectFromZeileString(sr.ReadLine(), ";");
}
using(StreamWriter sw = new StreamWriter(sPath))
{
}
}
}
}
Helperclass for splitting and array: 拆分和数组的Helperclass:
namespace Prog
{
public static class cZeiterfassung
{
public static cZeile GetZeileObjectFromZeileString(string Zeile, string Trenner)
{
cZeile ZeileReturn = new cZeile();
string[] separators = { Trenner };
string[] arZeile = Zeile.Split(separators, StringSplitOptions.None);
ZeileReturn.ID = arZeile[0];
if (arZeile[1].IndexOf("\\") != -1)
{
ZeileReturn.Domain = arZeile[1].Substring(0, arZeile[1].IndexOf("\\"));
if (arZeile[1].Length >= arZeile[1].IndexOf("\\"))
ZeileReturn.User = arZeile[1].Substring(arZeile[1].IndexOf("\\") + 1);
}
else
ZeileReturn.User = arZeile[1];
ZeileReturn.Datum = arZeile[2];
ZeileReturn.Kommt = arZeile[3];
ZeileReturn.Geht = arZeile[4];
if(!string.IsNullOrEmpty(arZeile[2]))
ZeileReturn.dtDatum = Convert.ToDateTime(arZeile[2]);
if(!string.IsNullOrEmpty(arZeile[3]))
ZeileReturn.dtKommt = Convert.ToDateTime(arZeile[3]);
if (!string.IsNullOrEmpty(arZeile[4]))
ZeileReturn.dtGeht = Convert.ToDateTime(arZeile[4]);
return ZeileReturn;
}
}//cZeiterfassung
public class cZeile
{
public string ID { get; set; }
public string Domain { get; set; }
public string User { get; set; }
public string Datum { get; set; }
public string Kommt { get; set; }
public string Geht { get; set; }
public DateTime dtDatum { get; set; }
public DateTime dtKommt { get; set; }
public DateTime dtGeht { get; set; }
public string Dauer { get; set; }
}
}
I can't quite Point out what are you trying to do from your code, but from the question you asked: I would look into every line if you are looking for a specific line 我不能完全指出您要从代码中做什么,但是要从您提出的问题中指出:如果您要查找特定的行,我会仔细研究每一行
string Data = "";
using(StreamReader Sr = new StreamReader(Path))
{
while(!Sr.EndOfStream())
{
string UseMe = Sr.ReadLine();
Data += UseMe;
}
}
Now you could just validate each line. 现在您只需验证每一行。 With the Data-string I gave you a possibility to build each line into one string
使用数据字符串,我使您可以将每一行构建为一个字符串
I hope I could help you, otherwise contact me directly (I also speak german I guess this is easier for you) 希望我能为您提供帮助,否则请直接与我联系(我也说德语,我想这对您来说比较容易)
If your file isn't big, I propose you to write to new files with new data. 如果文件不大,建议您使用新数据写入新文件。
This is the idea 这是主意
string[] lines = File.ReadAllLines(sPath);
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].IndexOf(";;") != -1)
{
lines[i] = lines[i].Insert(line.IndexOf(";;"), "xxx");
}
}
File.WriteAllLines(sPathTemp, lines);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.