views:

50

answers:

0

i try to make TCP/Ip Application to listen any other roemote computer to recieve any file. So i try to get files. i can do that. on the other hand every sample on google about giving SaveDialogBox to recived path folder.Forexample my old server.cs is that:



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;
using System.Net;
using System.Net.Sockets;
using System.Threading;


namespace Server3
{
    public partial class Form1 : Form
    {
        Thread kanal;
        public Form1()
        {
            InitializeComponent();

            try
            {
                kanal = new Thread(new ThreadStart(Dinle));
                kanal.Start();
                kanal.Priority = ThreadPriority.Normal;
                this.Text = "Kanal Çalıştı";
            }
            catch (Exception ex)
            {
                this.Text = "kanal çalışmadı";
                MessageBox.Show("hata:" + ex.ToString());
                kanal.Abort();
                throw;
            }
        }

        void Dinle()
        {
            TcpListener server = null;
            try
            {

                Int32 port = 51124;
                IPAddress localAddr = IPAddress.Parse("127.0.0.1");
                server = new TcpListener(localAddr, port);
                server.Start();
                Byte[] bytes = new Byte[1024 * 250000];
               // string ReceivedPath = "C:/recieved";


                while (true)
                {
                    MessageBox.Show("Waiting for a connection... ");

                    TcpClient client = server.AcceptTcpClient();
                    MessageBox.Show("Connected!");

                    NetworkStream stream = client.GetStream();
                    if (stream.CanRead)
                    {
                        saveFileDialog1.ShowDialog();


                        string pathfolder = saveFileDialog1.FileName;
                        StreamWriter yaz = new StreamWriter(pathfolder);

                        string satir;
                        StreamReader oku = new StreamReader(stream);

                        while ((satir = oku.ReadLine()) != null)
                        {


                            satir = satir + (char)13 + (char)10;
                            yaz.WriteLine(satir);

                        }
                        oku.Close();
                        yaz.Close();
                        client.Close();
                    }
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            finally
            {
                // Stop listening for new clients.
                server.Stop();
            }


            Console.WriteLine("\nHit enter to continue...");
            Console.Read();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Dinle();
        }
    }
}

i want to give automatically folder without SAVEDIALOGBOX. Also i want to learn my file name om my stream.Like that:


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;
using System.Net;
using System.Net.Sockets;
using System.Threading;


namespace Server3
{
    public partial class Form1 : Form
    {
        Thread kanal;
        public Form1()
        {
            InitializeComponent();

            try
            {
                kanal = new Thread(new ThreadStart(Dinle));
                kanal.Start();
                kanal.Priority = ThreadPriority.Normal;
                this.Text = "Kanal Çalıştı";
            }
            catch (Exception ex)
            {
                this.Text = "kanal çalışmadı";
                MessageBox.Show("hata:" + ex.ToString());
                kanal.Abort();
                throw;
            }
        }

        void Dinle()
        {
            TcpListener server = null;
            try
            {

                Int32 port = 51124;
                IPAddress localAddr = IPAddress.Parse("127.0.0.1");
                server = new TcpListener(localAddr, port);
                server.Start();
                Byte[] bytes = new Byte[1024 * 250000];
                string ReceivedPath = "C:/recieved";


                while (true)
                {
                    MessageBox.Show("Waiting for a connection... ");

                    TcpClient client = server.AcceptTcpClient();
                    MessageBox.Show("Connected!");

                    NetworkStream stream = client.GetStream();
                    if (stream.CanRead)
                    {
                        saveFileDialog1.ShowDialog();


                        string pathfolder = " i have to give property creating path and want to learn file name";
                        StreamWriter yaz = new StreamWriter(pathfolder);

                        string satir;
                        StreamReader oku = new StreamReader(stream);

                        while ((satir = oku.ReadLine()) != null)
                        {


                            satir = satir + (char)13 + (char)10;
                            yaz.WriteLine(satir);

                        }
                        oku.Close();
                        yaz.Close();
                        client.Close();
                    }
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }
            finally
            {
                // Stop listening for new clients.
                server.Stop();
            }


            Console.WriteLine("\nHit enter to continue...");
            Console.Read();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Dinle();
        }
    }
}





Also i need :

  FileStream fs;

            FileInfo fi = new FileInfo(@"c:/recieved");

            if (fi.Exists)
                fs = new FileStream(fi.FullName, FileMode.Append);
            else
                fs = new FileStream(fi.FullName, FileMode.Create);
            StreamWriter yazici = new StreamWriter(fs);>

How can i do that. Creating C:/recieved if it does not exist. And how can i learn File name on my network stream sending File Name.