I have this form to upload a txt file:
Button code, this is where I upload the file:
private void button1_Click(object sender, EventArgs e)
{
//lines contiene todas las lineas del archivo
string[] lines = System.IO.File.ReadAllLines(@"C:\\Users\\JR\\Desktop\\TXTS\\MX19080253005013GNNPN8ACYN.htd");
//estamos mandando las lineas a una cadena string a la clase read
r.lineas = lines;
//estamos llenando la el grid con la informacion
r.readFile(dataGridView1, ' ');
}
Result:
I have this read class:
class read
{
public string[] lineas; //creamos una cadena de string
public void readFile(DataGridView tabla, char caracter) //metodo para leer y llenar el grid
{
foreach (var linea in lineas) //mandamos la informacion y la muestra en el grid
{
int columnas = tabla.Columns.Count;
foreach (DataGridViewRow fila in tabla.Rows)
{
int vacios = 0;
for (int i = 0; i < columnas; i++) //quitar espacios
{
if (Convert.ToString(fila.Cells[i].Value) == string.Empty)
{
vacios++;
}
}
if (vacios == columnas)
{
tabla.Rows.RemoveAt(fila.Index);
}
}
AddRowDataGrid(tabla,linea,caracter);
}
} //public readFile
public static void nombrarTitulo(DataGridView tabla, string [] titulos)
{
int x = 0;
for (x = 0; x <= tabla.ColumnCount - 1; x++ )
{
tabla.Columns[x].HeaderText = titulos[x];
}
} // public nombrarTitulo
public static void AddRowDataGrid(DataGridView tabla, string linea, char caracter)
{
string[] arreglo = linea.Split(new string[] { caracter.ToString(),"\t" }, StringSplitOptions.RemoveEmptyEntries);
tabla.Rows.Add(arreglo);
} //public AddRowGrid
}
This is what the txt file looks like:
*** DATE: 3/7/2019 TIME: 6:42 AM SN: MX19080344405013GNNPN8ACYN SW VER: 9.07 Station: ATE08-FOXCONN TX Cal: 23.42 Individual Tests: FALSE ***
TEST DESCRIPTION MIN MEASURED MAX UNITS RESULTS TIME
**********************************************************************************************************************************
1 XXXXX XXXXX XXXXXX XXXX XXXX XXXX XXXXX
XXXXX XXXXX XXXXXX XXXX XXXX XXXX XXXXX
XXXXX XXXXX XXXXXX XXXX XXXX XXXX XXXXX
XXXXX XXXXX XXXXXX XXXX XXXX XXXX XXXXX
2 XXXXX XXXXX XXXXXX XXXX XXXX XXXX XXXXX
XXXXX XXXXX XXXXXX XXXX XXXX XXXX XXXXX
This is line 4:
*** DATE: 3/7/2019 TIME: 6:42 AM SN: MX19080344405013GNNPN8ACYN SW VER: 9.07 Station: ATE08-FOXCONN TX Cal: 23.42 Individual Tests: FALSE ***
What I need to do is obtain the information of each one, that is, 3/7/2019 from Date:, 6:42 AM from TIME: etc... and put them in the form where there are some labels which will be filled with said information, the name of these are:
labelDate
LabelTime LabelSn
LabelSw
LabelStation
LabelTx
How can I perform this action?
One way to obtain the values is to use regular expressions, the example that I leave below may not perfectly solve the inconvenience you have when obtaining the values, but it will work as a guide for you to adapt it to your code, relying on the links of references that I leave at the end of the post.
Method
GetValue
:Gets the value that is between the parameters, names or sub-expression. It returns the value if the expression matches, otherwise it will return an empty string.
Parameters :
Line
: is the string where the values will be obtained.upperLimit
: upper limit to search for (in this case the name of the value to search for). It is also possible to pass any sub-expression if need be.upperLimit
: lower limit to search for (in this case the next name of the value to search for). It is also possible to pass any sub-expression if need be.Pattern details:
(?<={0})(.*?)(?={1})
where{0} y {1}
are the values ofupperLimit y upperLimit
respectively . With this pattern we obtain the values that are between the upper limit and the lower limit.References :
This is as simple as breaking the string into the pieces you want:
Note that in this example, you will see what you need in each case.
You can do the same for the entire string and pass the data to the control you want. You don't need to unset the string or pass it to an array or anything like that.