1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WinFormatFichierTexte
{
public partial class Form2 : Form
{
private string pathFile=Directory.GetCurrentDirectory()+ @"\myfile.txt";
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string data = String.Format("{0,0} {1,-10} {2,-25} {3, -10} \n",
"Title", "Publisher", "Price", "Year");
string nom = "Villahardouin"; //13 caracteres,il faudra stripper 5 caracteres
string prenom = "Geoffroi"; //8 caracteres
string age = 56.ToString(); //2 caracteres
string mail = "jacqoulecroquant@gmail.com"; //26 caracteres
using (StreamWriter sw = new StreamWriter(pathFile))
{
// 1er exemple illustration
sw.Write(data);
//ne tient pas compte du fait que nom depasse 10 caracteres
string data1 = String.Format("{0,0} {1,-10} {2,-20} {3, -25} \n",
nom, prenom, age, mail);
sw.Write(data1);
// tient compte du fait que nom depasse 10 caracteres d'ou le remove
string data2 = String.Format("{0,0} {1,-10}| {2,-20} {3, -25} \n",
nom.Remove (10), prenom, age, mail);
data2 += data2;
//textBox2.Text = data2;
sw.Write(data2);
}
}
}
} |