hi how are u
am new in programing at all :confused:
i have a code for a program that communecate with a DDe server
this code need some modefication to complete it(need to include the System.IO namespace to use a StreamWriter and Add a using System.IO; statement to the other using statements )
could u plz help in this? cos am totally confused and dont know what to do....
i use Visual C# 2008 Express

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using NDde.Client;


namespace _4XDDEClient
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            TheContainer.TheForm = this;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StartQuotes();
        }


        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (TheContainer.TheDdeClient != null)
            {
                TheContainer.TheDdeClient.Disconnect();
            }
        }

        private void Log(string pText)
        {
            //textBox1.AppendText(pText + "\r\n"); // does not put cr lf, why?

            textBox1.AppendText(pText);
            textBox1.AppendText("\n");
            }
        private void LogStatus(string pText)
            {                          
            textBox1.AppendText(pText+"\t");
            textBox1.AppendText("\n");
            }

        private void StartQuotes()
        {
            try
            {
                if (TheContainer.TheDdeClient == null)
                {
                    DdeClient client = new DdeClient("MT4", "QUOTE");
                    TheContainer.TheDdeClient = client;

                    // Subscribe to the Disconnected event.  This event will notify the application when a conversation has been terminated.
                    client.Disconnected += OnDisconnected;

                    // Connect to the server.  It must be running or an exception will be thrown.
                    client.Connect();

                    // Advise Loop
                    client.StartAdvise("USDCHF", 1, true, 60000);
                    client.StartAdvise("USDJPY", 1, true, 60000);
                    client.StartAdvise("EURUSD", 1, true, 60000);
                    client.StartAdvise("GBPUSD", 1, true, 60000);
                    client.StartAdvise("EURJPY", 1, true, 60000);
                    client.StartAdvise("EURCHF", 1, true, 60000);
                    client.StartAdvise("EURGBP", 1, true, 60000);
                    client.StartAdvise("USDCAD", 1, true, 60000);
                    client.StartAdvise("AUDUSD", 1, true, 60000);
                    client.StartAdvise("GBPCHF", 1, true, 60000);
                    client.StartAdvise("GBPJPY", 1, true, 60000);
                    client.StartAdvise("CHFJPY", 1, true, 60000);
                    client.StartAdvise("NZDUSD", 1, true, 60000);
                    client.StartAdvise("EURCAD", 1, true, 60000);
                    client.StartAdvise("AUDJPY", 1, true, 60000);
                    client.StartAdvise("EURAUD", 1, true, 60000);
                    client.StartAdvise("AUDCAD", 1, true, 60000);
                    client.StartAdvise("AUDNZD", 1, true, 60000);
                    client.StartAdvise("NZDJPY", 1, true, 60000);
                    client.Advise += OnAdvise;

                    Log("DDE Client Started");
                }
                else
                {
                    Log("DDE Client Already Started");
                }
            }
            catch (Exception)
            {
                TheContainer.TheDdeClient = null;
                Log("An exception was thrown during DDE connection");
                Log("Ensure Metatrader 4 is running and DDE is enabled");
                Log("To activate the DDE Server go to Tools -> Options");
                Log("On the Server tab, ensure \"Enable DDE server\" is checked");
            }
        }

        private void OnAdvise(object sender, DdeAdviseEventArgs args)
        {
            Log(args.Item+": " + args.Text);
            LogToFile(args.Item , args.Text);
        }
         private void LogToFile(string FileName,string 
                                dataToSave)
         {
                          StreamWriter theString = new 
              StreamWriter(FileName+".csv", true);
                                            
              theString.WriteLine(DateTime.Now+"\t"+ 
              dataToSave+"\t\n");
                           theString.Close();
                                           
               }
        private void OnDisconnected(object sender, DdeDisconnectedEventArgs args)
        {
            Log(
                "OnDisconnected: " +
                "IsServerInitiated=" + args.IsServerInitiated.ToString() + " " +
                "IsDisposed=" + args.IsDisposed.ToString());
            TheContainer.TheDdeClient = null;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (TheContainer.TheDdeClient != null)
            {
                TheContainer.TheDdeClient.Disconnect();
            }
            else
            {
                Log("DDE Client Not Started");
            }
        }
    }

    public class TheContainer
    {
        public static Form1 TheForm;
        public static DdeClient TheDdeClient;
    }
}

hi
any help plz?

Lines 111-112 have a StreamWriter. Your request is vague. What does this StreamWriter you need to add need to write?

The only thing I can think of would be the following as you want it wrapped in a using() statement though implementing a using() is very easy as below shows.

private void LogToFile(string FileName,string dataToSave)
{
   using(StreamWriter theString = new StreamWriter(FileName+".csv", true)))
   {
      theString.WriteLine(DateTime.Now+"\t"+ dataToSave+"\t\n");
      theString.Close();
   }
}

Hope this helps.

The only thing I can think of would be the following as you want it wrapped in a using() statement though implementing a using() is very easy as below shows.

private void LogToFile(string FileName,string dataToSave)
{
   using(StreamWriter theString = new StreamWriter(FileName+".csv", true)))
   {
      theString.WriteLine(DateTime.Now+"\t"+ dataToSave+"\t\n");
      theString.Close();
   }
}

Hope this helps.

hi.....thanks for ur help.......the code works without the word (using)

thanks for ur help agiane

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.