Prbl bizarre fichier texte

Résolu
penchu Messages postés 167 Date d'inscription mercredi 11 février 2004 Statut Membre Dernière intervention 5 janvier 2006 - 24 févr. 2005 à 13:18
penchu Messages postés 167 Date d'inscription mercredi 11 février 2004 Statut Membre Dernière intervention 5 janvier 2006 - 25 févr. 2005 à 20:28
bonjour à tous,

ya un truc que je ne parviens pas à comprendre : dans ma form, j'ai 2 textbox et un bouton envoyer.

Quand je fais par exemple, un sw.writeline("coucou"); puis un sw.writeline("gamin"); ça fonctionne très bien.

Mais quand je fais ce code, il ne me copie pas le contenu de ma textbox2. Je ne comprends rien à rien ....



FileStream fs = new
FileStream(@"C:\fichier.log",FileMode.Append,FileAccess.Write);
StreamWriter sw = new
StreamWriter(fs);

StreamWriter sw2 = new StreamWriter(fs);

sw.WriteLine("Envoyé à "+this.textBox1.Text);

sw.WriteLine("Message : "+this.textBox2.Text);

sw.Close();

fs.Close();



Qqn a une solution?

Merci d'avance

17 réponses

MorpionMx Messages postés 3466 Date d'inscription lundi 16 octobre 2000 Statut Membre Dernière intervention 30 octobre 2008 57
24 févr. 2005 à 15:22
Ton TextBox2.Text est vide.... Et tu sais pourquoi ?

Tout simplement parce que dans ta méthode button1_click, tu as mis <!--StartFragment -->
this.textBox2.Text="";

Quelques lignes avant d'écrire dans le fichier...

Mx
3
cs_Bidou Messages postés 5487 Date d'inscription dimanche 4 août 2002 Statut Membre Dernière intervention 20 juin 2013 61
24 févr. 2005 à 14:54
Salut Pingu,
Chez moi, ce code fonctionne parfaitement, j'ai testé :

FileStream fs =
new FileStream(@"C:\fichier.log", FileMode.Append, FileAccess.Write);
StreamWriter sw =
new StreamWriter(fs);
sw.WriteLine("Envoyé à : " +
this.textBox1.Text);
sw.WriteLine("Message : " +
this.textBox2.Text);
sw.Close();
fs.Close();

Tu dois donc avoir une erreur ailleurs encore je pense.
Poste plus de code....

[Pub] http://www.csharpfr.com/auteurdetail.aspx?ID=13319 [\Pub]
C# forever
0
penchu Messages postés 167 Date d'inscription mercredi 11 février 2004 Statut Membre Dernière intervention 5 janvier 2006
24 févr. 2005 à 15:02
voici tout mon code complet : en fait c'est le netsender que j'ai posté il y a peu.


using System;


using System.Drawing;


using System.Collections;


using System.ComponentModel;


using System.Windows.Forms;


using System.Data;


using System.Diagnostics;


using System.IO;


namespace NetSender_1._0


{



/// <summary>



/// Description résumée de Form1.



/// </summary>



public
class Form1 : System.Windows.Forms.Form


{



private System.Windows.Forms.Button button1;



private System.Windows.Forms.Label label1;



private System.Windows.Forms.Label label2;



private System.Windows.Forms.TextBox textBox1;



private System.Windows.Forms.Button button2;



private System.Windows.Forms.Button button3;



private NETXP.Components.NotifyIconEx notifyIconEx1;



private System.Windows.Forms.ToolTip toolTip1;



private System.Windows.Forms.ComboBox comboBox1;



private System.Windows.Forms.TextBox textBox2;



private System.ComponentModel.IContainer components;



public Form1()


{



//



// Requis pour la prise en charge du Concepteur Windows Forms



//


InitializeComponent();



this.comboBox1.Items.Add("Contremaitres");



this.comboBox1.Items.Add("Jeremy");



this.comboBox1.Items.Add("Jlf");



this.comboBox1.Items.Add("Jose-xp");



this.comboBox1.Items.Add("Pc-damien");



this.comboBox1.Items.Add("Pc-encres");



this.comboBox1.Items.Add("Pc-koen");



this.comboBox1.Items.Add("Pc-magasin");



this.comboBox1.Items.Add("Pc-table");



this.comboBox1.Items.Add("Secretariat");



this.comboBox1.Items.Add("Thierry-penta");



//



// TODO : ajoutez le code du constructeur après l'appel à InitializeComponent



//


}



/// <summary>



/// Nettoyage des ressources utilisées.



/// </summary>



protected
override
void Dispose(
bool disposing )


{



if( disposing )


{



if (components !=
null)


{


components.Dispose();


}


}



base.Dispose( disposing );


}


#region Code généré par le Concepteur Windows Form



/// <summary>



/// Méthode requise pour la prise en charge du concepteur - ne modifiez pas



/// le contenu de cette méthode avec l'éditeur de code.



/// </summary>



private
void InitializeComponent()


{



this.components =
new System.ComponentModel.Container();


System.Resources.ResourceManager resources =
new System.Resources.ResourceManager(
typeof(Form1));



this.button1 =
new System.Windows.Forms.Button();



this.label1 =
new System.Windows.Forms.Label();



this.label2 =
new System.Windows.Forms.Label();



this.textBox1 =
new System.Windows.Forms.TextBox();



this.button2 =
new System.Windows.Forms.Button();



this.button3 =
new System.Windows.Forms.Button();



this.notifyIconEx1 =
new NETXP.Components.NotifyIconEx();



this.toolTip1 =
new System.Windows.Forms.ToolTip(
this.components);



this.comboBox1 =
new System.Windows.Forms.ComboBox();



this.textBox2 =
new System.Windows.Forms.TextBox();



this.SuspendLayout();



//



// button1



//



this.button1.BackColor = System.Drawing.Color.WhiteSmoke;



this.button1.FlatStyle = System.Windows.Forms.FlatStyle.Popup;



this.button1.Location =
new System.Drawing.Point(88, 152);



this.button1.Name = "button1";



this.button1.TabIndex = 2;



this.button1.Text = "&Envoyer";



this.toolTip1.SetToolTip(
this.button1, "Cliquez ici pour envoyer votre message");



this.button1.Click +=
new System.EventHandler(
this.button1_Click);



//



// label1



//



this.label1.Font =
new System.Drawing.Font("Comic Sans MS", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));



this.label1.ForeColor = System.Drawing.Color.WhiteSmoke;



this.label1.Location =
new System.Drawing.Point(16, 24);



this.label1.Name = "label1";



this.label1.Size =
new System.Drawing.Size(64, 23);



this.label1.TabIndex = 1;



this.label1.Text = "Nom";



//



// label2



//



this.label2.Font =
new System.Drawing.Font("Comic Sans MS", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));



this.label2.ForeColor = System.Drawing.Color.WhiteSmoke;



this.label2.Location =
new System.Drawing.Point(16, 56);



this.label2.Name = "label2";



this.label2.Size =
new System.Drawing.Size(64, 23);



this.label2.TabIndex = 2;



this.label2.Text = "Message";



//



// textBox1



//



this.textBox1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255)));



this.textBox1.Font =
new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));



this.textBox1.ForeColor = System.Drawing.Color.Maroon;



this.textBox1.Location =
new System.Drawing.Point(88, 24);



this.textBox1.Name = "textBox1";



this.textBox1.TabIndex = 0;



this.textBox1.Text = "";



//



// button2



//



this.button2.BackColor = System.Drawing.Color.WhiteSmoke;



this.button2.FlatStyle = System.Windows.Forms.FlatStyle.Popup;



this.button2.Location =
new System.Drawing.Point(248, 152);



this.button2.Name = "button2";



this.button2.Size =
new System.Drawing.Size(32, 23);



this.button2.TabIndex = 3;



this.button2.Text = "&?";



this.toolTip1.SetToolTip(
this.button2, "Afficher l\'aide");



this.button2.Click +=
new System.EventHandler(
this.button2_Click);



//



// button3



//



this.button3.BackColor = System.Drawing.Color.WhiteSmoke;



this.button3.FlatStyle = System.Windows.Forms.FlatStyle.Popup;



this.button3.Location =
new System.Drawing.Point(288, 152);



this.button3.Name = "button3";



this.button3.TabIndex = 4;



this.button3.Text = "&Quitter";



this.toolTip1.SetToolTip(
this.button3, "Quitter Net Sender 1.1");



this.button3.Click +=
new System.EventHandler(
this.button3_Click);



//



// notifyIconEx1



//



this.notifyIconEx1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIconEx1.Icon")));



this.notifyIconEx1.Text = "Net Sender 1.1";



this.notifyIconEx1.Visible =
true;



this.notifyIconEx1.RightClick +=
new System.EventHandler(
this.notifyIconEx1_RightClick);



this.notifyIconEx1.Click +=
new System.EventHandler(
this.notifyIconEx1_Click);



this.notifyIconEx1.DoubleClick +=
new System.EventHandler(
this.notifyIconEx1_DoubleClick);



//



// comboBox1



//



this.comboBox1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255)));



this.comboBox1.ForeColor = System.Drawing.Color.Maroon;



this.comboBox1.Location =
new System.Drawing.Point(248, 24);



this.comboBox1.Name = "comboBox1";



this.comboBox1.Size =
new System.Drawing.Size(121, 21);



this.comboBox1.TabIndex = 5;



this.comboBox1.Text = "Nom";



this.toolTip1.SetToolTip(
this.comboBox1, "Liste des ordinateurs présents sur le réseau");



this.comboBox1.SelectedIndexChanged +=
new System.EventHandler(
this.comboBox1_SelectedIndexChanged);



//



// textBox2



//



this.textBox2.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255)));



this.textBox2.Font =
new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));



this.textBox2.ForeColor = System.Drawing.Color.Maroon;



this.textBox2.Location =
new System.Drawing.Point(88, 56);



this.textBox2.Multiline =
true;



this.textBox2.Name = "textBox2";



this.textBox2.Size =
new System.Drawing.Size(280, 88);



this.textBox2.TabIndex = 6;



this.textBox2.Text = "";



//



// Form1



//



this.AutoScaleBaseSize =
new System.Drawing.Size(5, 13);



this.BackColor = System.Drawing.Color.DarkBlue;



this.ClientSize =
new System.Drawing.Size(376, 182);



this.Controls.Add(
this.textBox2);



this.Controls.Add(
this.comboBox1);



this.Controls.Add(
this.button3);



this.Controls.Add(
this.button2);



this.Controls.Add(
this.textBox1);



this.Controls.Add(
this.label2);



this.Controls.Add(
this.label1);



this.Controls.Add(
this.button1);



this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));



this.Name = "Form1";



this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;



this.Text = "NetSender 1.1";



this.Resize +=
new System.EventHandler(
this.Form1_Resize);



this.Load +=
new System.EventHandler(
this.Form1_Load);



this.ResumeLayout(
false);


}


#endregion



/// <summary>



/// Point d'entrée principal de l'application.



/// </summary>


[STAThread]



static
void Main()


{


Application.Run(
new Form1());


}



private
void button3_Click(
object sender, System.EventArgs e)


{



this.Close();


}



private
void button2_Click(
object sender, System.EventArgs e)


{


Form2 aide =
new Form2();


aide.Show();


}



private
void button1_Click(
object sender, System.EventArgs e)


{



if ((
this.textBox1.Text=="")&&(
this.textBox2.Text==""))


{


MessageBox.Show("Vous devez remplir les champs Nom d'hôte et Message pour pouvoir envoyer.","Champs vides");


}



//j'ai rajouté ce code (msdn power) qui permet de ne pas lancer de fenêtre DOS lors de l'envoi du message


Process send =
new Process();


ProcessStartInfo startInfo =
new ProcessStartInfo("net.exe");


startInfo.WindowStyle = ProcessWindowStyle.Hidden;


Process.Start(startInfo);


startInfo.Arguments = " send "+
this.textBox1.Text+" "+
this.textBox2.Text;


Process.Start(startInfo);



//this.textBox1.Text="";



this.textBox2.Text="";



this.textBox2.Focus();





//pour l'enregistrement dans le fichier log


FileStream fs =
new FileStream(@"C:\Program Files\NetSender 1.1\fichier.log",FileMode.Append,FileAccess.Write);


StreamWriter sw =
new StreamWriter(fs);


StreamWriter sw2 =
new StreamWriter(fs);


sw.WriteLine(System.DateTime.Now.ToLongDateString() +" "+System.DateTime.Now.ToLongTimeString());


sw.WriteLine("----------------------------------");


sw.WriteLine("Envoyé à "+
this.textBox1.Text.ToUpper());


sw.WriteLine("Message : "+
this.textBox2.Text);


sw.WriteLine();


sw.WriteLine();


sw.Close();


fs.Close();


}



private
void Form1_Resize(
object sender, System.EventArgs e)


{



if (
this.WindowState == FormWindowState.Minimized)


{



this.Hide();



this.ShowInTaskbar =
false;



this.textBox1.Text="";



this.textBox2.Text="";


}


}



private
void RevoirForm ()


{



this.ShowInTaskbar =
true;



this.Show();



this.WindowState = FormWindowState.Normal;


}



private
void notifyIconEx1_Click(
object sender, System.EventArgs e)


{


RevoirForm();


}



private
void notifyIconEx1_DoubleClick(
object sender, System.EventArgs e)


{


RevoirForm();


}



private
void notifyIconEx1_RightClick(
object sender, System.EventArgs e)


{


RevoirForm();


}



private
void Form1_Load(
object sender, System.EventArgs e)


{



this.notifyIconEx1.ShowBalloon("Net Sender 1.1","Cliquez sur cette icône pour renvoyer un message.",NETXP.Components.NotifyInfoFlags.Info,3000);


}
0
penchu Messages postés 167 Date d'inscription mercredi 11 février 2004 Statut Membre Dernière intervention 5 janvier 2006
24 févr. 2005 à 15:25
pas con le mec (lol)
un énorme merci à toi
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
MorpionMx Messages postés 3466 Date d'inscription lundi 16 octobre 2000 Statut Membre Dernière intervention 30 octobre 2008 57
24 févr. 2005 à 15:30
Petit conseil, quand tu construis le chemin de ton fichier log,
utilises Application.StartupPath() plutot que de mettre "Program
file\etc..." Comme ca si jamais un jour l'appli change de place, elle
retrouvera ses petits

Mx
0
penchu Messages postés 167 Date d'inscription mercredi 11 février 2004 Statut Membre Dernière intervention 5 janvier 2006
24 févr. 2005 à 15:35
ben justement, je suis en train de me poser une question.
J'ai envie de mettre les fichiers de log dans un dossier partagé avec accès en écriture sur un serveur. Mais le problème c'est que comme j'installe le programme sur plusieurs machines, il faudrait que le nom du fichier de log soit différent pour chaque machine. Je ne sais donc pas quelle solution prendre pour mettre ça en oeuvre : soit je fais un incrément genre fichier1.log, puis ce sera fichier2.log ou alors je prends le nom de la machine.log. Mais ça.... je ne sais pas du tout comment faire. T'as pas une idée?
0
cs_Bidou Messages postés 5487 Date d'inscription dimanche 4 août 2002 Statut Membre Dernière intervention 20 juin 2013 61
24 févr. 2005 à 18:05
Pour avoir le nom de la machine, et autres informations dans le même genre, regarde du côté des properties statiques de la class Environment. Tu devrais y trouver ton bonheur !


[Pub] http://www.csharpfr.com/auteurdetail.aspx?ID=13319 [\Pub]
C# forever
0
penchu Messages postés 167 Date d'inscription mercredi 11 février 2004 Statut Membre Dernière intervention 5 janvier 2006
24 févr. 2005 à 18:46
j'ai trouvé pas mal de truc la dessus : merci bcp pour l'info
0
penchu Messages postés 167 Date d'inscription mercredi 11 février 2004 Statut Membre Dernière intervention 5 janvier 2006
24 févr. 2005 à 18:48
une dernière chose : peut-être que vous savez comment faire. Comment puis-je récupérer le nom de toutes les machines qui se trouvent sur le réseaux chez moi? en sachant que j'ai un domaine workgroup, un domaine HAUT et un domaine ETAGE. Je voudrais pouvoir récupérer tous ces noms et les mettre dans une combobox.
Merci d'avance
0
penchu Messages postés 167 Date d'inscription mercredi 11 février 2004 Statut Membre Dernière intervention 5 janvier 2006
25 févr. 2005 à 09:46
et encore une autre chose. Si maintenant je veux écrire ce fichier de
log dans un fichier avec accès total sur mon espace web fourni par mon
isp, comment puis-je faire?

j'ai essayé ça :

FileStream fs
= new
FileStream("http://users.skynet.be/penchu/upload/fichier.log",FileMode.Append,FileAccess.Write);

mais ca ne marche pas : il ne me dit que le format d'url n'est pas pris en charge.

J'ai mis un using System.net pourtant...
0
MorpionMx Messages postés 3466 Date d'inscription lundi 16 octobre 2000 Statut Membre Dernière intervention 30 octobre 2008 57
25 févr. 2005 à 10:20
Tu dois pouvoir recuperer ton fichier via les classes HttpWebRequest /
HttpWebResponse (et donc un stream avec la méthode GetResponseStream()
).

Ce stream representera ton fichier.

Cependant, je ne sais pas du tout si tu pourras ecrire directement dedans, ca c'est a tester.



Autrement, as tu penser a comment gerer le fait que 2 personnes (ou
plus) ecrivent / veulent ecrire dans le fichier log en meme temps
(acces concurrents) ?


Mx
0
penchu Messages postés 167 Date d'inscription mercredi 11 février 2004 Statut Membre Dernière intervention 5 janvier 2006
25 févr. 2005 à 10:25
l'accès concurrent, c'est pas grave car le fichier log qui sera créé aura comme nom le nom de la machine qui l'envoie.Par exemple, le nom de ma machine étant Penchu, le log créé sera penchu.log

Tu n'as pas un exemple avec la méthode getresponsestream par hasard?
0
MorpionMx Messages postés 3466 Date d'inscription lundi 16 octobre 2000 Statut Membre Dernière intervention 30 octobre 2008 57
25 févr. 2005 à 10:28
http://www.csharpfr.com/forum.v2.aspx?ID=398158



Regarde ici.

Le sujet etait de recuperer une image via une url, mais le principe reste le meme

Mx
0
penchu Messages postés 167 Date d'inscription mercredi 11 février 2004 Statut Membre Dernière intervention 5 janvier 2006
25 févr. 2005 à 10:43
je vais aller voir ca
merci
0
penchu Messages postés 167 Date d'inscription mercredi 11 février 2004 Statut Membre Dernière intervention 5 janvier 2006
25 févr. 2005 à 18:29
j'ai réussi à mettre un fichier log pour chaque machine, le tout dans
un dossier du réseau (avec penchu.log, papa.log ....) et ça marche
nickel.

Maintenant, je voudrais bien récupéré le résultat de la commande exécutée.

Pour me faire comprendre, dans dos, quand le message est envoyé, il
renvoie "Le message a bien été envoyé à Penchu" ou un truc du genre.

J'ai essayé sous dos de faire un "net send penchu coucou >c:\result.txt"

Le fichier est bien créé et tout et tout.

Mais dans mon code, je ne parviens pas à le faire car je dois introduire textbox1 et textbox2 en paramètres.

Qqn peut m'aider svp?
0
cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
25 févr. 2005 à 19:22
en partant du fait que textbox1 contient le nom et textbox2 le texte essaie :

string params = " send " + textbox1.Text + " "" + textbox2.Text + "">c:\\result.txt";
Process.Start("net.exe", params);

Cocoricoooooooo !!!!
coq
MVP Visual C#
0
penchu Messages postés 167 Date d'inscription mercredi 11 février 2004 Statut Membre Dernière intervention 5 janvier 2006
25 févr. 2005 à 20:28
je comprend pas ce que tu veux faire en faisant ça (à quoi servent les ""). Et en plus il ne me crée pas mon fichier.
0
Rejoignez-nous