none
c# how to make online chat multiplayer RRS feed

  • Question

  • How do i make add / remove friend like i want i will send to txt in ftp server and it will save from the user that connect the user that want to add.

    idk how to explain that good sorry for i think all of you didnt understand. just ask

    Thursday, November 22, 2018 2:24 PM

Answers

  • Hi Noam1248,

    Thank you for posting here.

    For your question, you want to Set ftp to add friends or delete friends.

    Please try this  code below.

    using System;
    
    using System.Collections;
    
    using System.Collections.Generic;
    
    using System.ComponentModel;
    
    using System.Data;
    
    using System.Drawing;
    
    using System.IO;
    
    using System.Linq;
    
    using System.Net;
    
    using System.Text;
    
    using System.Text.RegularExpressions;
    
    using System.Threading.Tasks;
    
    using System.Windows.Forms;
    
    namespace test2
    
    {
    
        public partial class Form1 : Form
    
        {
    
            public Form1()
    
            {
    
                InitializeComponent();
    
            }
    
            private void btnsend_Click(object sender, EventArgs e)
    
            {
    
                richTextBox1.AppendText("I Say:"+textBox2.Text + Environment.NewLine);
    
              
    
            }
    
            private Button btn = new Button();
    
            private void Form1_Load(object sender, EventArgs e)
    
            {
    
                string text = FtpdownloadString(path, username, password);
    
                string[] allname = text.Split('\n');
    
                ListViewItem[] lvs = new ListViewItem[allname.Length];
    
                for (int i = 0; i < lvs.Length; i++)
    
                {
    
                    lvs[i]=new ListViewItem(new string[] { allname[i] });
    
                    btn.Text = allname[i];
    
                }
    
                this.listView1.Items.AddRange(lvs);
    
                btn.Visible =false;
    
                btn.Click += this.button_Click;
    
                this.listView1.Controls.Add(btn);
    
                this.btn.Size = new Size(this.listView1.Items[0].SubItems[0].Bounds.Width,
    
                this.listView1.Items[0].SubItems[0].Bounds.Height);
    
            }
    
          
    
            private void button1_Click(object sender, EventArgs e)
    
            {
    
                listView1.Items.Clear();
    
                if (textBox2.Text == "")
    
                {
    
                    MessageBox.Show("please input full information");
    
                }
    
                else
    
                {
    
                    FtpUploadString(textBox2.Text, path, username, password);
    
                    string text = FtpdownloadString(path, username, password);
    
                    string[] allname = text.Split('\n');
    
                    ListViewItem[] lvs = new ListViewItem[allname.Length];
    
                    for (int i = 0; i < lvs.Length; i++)
    
                    {
    
                        lvs[i] = new ListViewItem(new string[] { allname[i] });
    
                        btn.Text = allname[i];
    
                    }
    
                    this.listView1.Items.AddRange(lvs);
    
                    btn.Visible = false;
    
                    btn.Click += this.button_Click;
    
                    this.listView1.Controls.Add(btn);
    
                    this.btn.Size = new Size(this.listView1.Items[0].SubItems[0].Bounds.Width,
    
                    this.listView1.Items[0].SubItems[0].Bounds.Height);
    
                    MessageBox.Show("success");
    
                }
    
               
    
            }
    
            private void button_Click(object sender, EventArgs e)
    
            {
    
                label1.Text = "you are talking with" + this.listView1.SelectedItems[0].SubItems[0].Text;
    
            }
    
            private void listView1_SelectedIndexChanged_1(object sender, EventArgs e)
    
            {
    
                if (this.listView1.SelectedItems.Count > 0)
    
                {
    
                    this.btn.Location = new Point(this.listView1.SelectedItems[0].SubItems[0].Bounds.Left,
    
                        this.listView1.SelectedItems[0].SubItems[0].Bounds.Top);
    
                    this.btn.Visible = true;
    
                }
    
            }
    
            private void FtpUploadString(string text, string to_uri, string user_name, string password)
    
            {
    
                FtpWebRequest request =
    
                    (FtpWebRequest)WebRequest.Create(to_uri);
    
                request.Method = WebRequestMethods.Ftp.UploadFile;
    
                request.Credentials =
    
                    new NetworkCredential(user_name, password);
    
                request.ContentLength = FtpdownloadString(to_uri, user_name, password).Length + Environment.NewLine.Length + text.Length;
    
                string fi = FtpdownloadString(to_uri, user_name, password);
    
                using (Stream request_stream = request.GetRequestStream())
    
                {
    
                    byte[] bytes = Encoding.UTF8.GetBytes(fi + Environment.NewLine + text);
    
                    request_stream.Write(bytes, 0, (fi + Environment.NewLine + text).Length);
    
                    request_stream.Close();
    
                }
    
           
    
            }
    
            private void removeFtpUploadString( string text,string to_uri, string user_name, string password)
    
            {
    
                FtpWebRequest request =
    
                    (FtpWebRequest)WebRequest.Create(to_uri);
    
                request.Method = WebRequestMethods.Ftp.UploadFile;
    
                request.Credentials =
    
                    new NetworkCredential(user_name, password);
    
                request.ContentLength = FtpdownloadString(to_uri, user_name, password).Length- Environment.NewLine.Length-text.Length ;
    
                string fi = FtpdownloadString(to_uri, user_name, password);
    
                string str = Regex.Replace(fi, text+Environment.NewLine, "");
    
                using (Stream request_stream = request.GetRequestStream())
    
                {
    
                    byte[] bytes = Encoding.UTF8.GetBytes(str +Environment.NewLine );
    
                    request_stream.Write(bytes, 0, (str + Environment.NewLine).Length);
    
                    request_stream.Close();
    
                }
    
            }
    
            private string FtpdownloadString(string to_uri, string user_name, string password)
    
            {
    
                FtpWebRequest request =
    
                    (FtpWebRequest)WebRequest.Create(to_uri);
    
                request.Method = WebRequestMethods.Ftp.DownloadFile;
    
                request.Credentials =
    
                    new NetworkCredential(user_name, password);
    
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    
                Stream ftpStream = response.GetResponseStream();
    
                StreamReader reader = new StreamReader(ftpStream);
    
                string filestr = reader.ReadToEnd();
    
                return filestr;
    
            }
    
            private void btnremove_Click(object sender, EventArgs e)
    
            {
    
                listView1.Items.Clear();
    
                if(textBox2.Text=="")
    
                {
    
                    MessageBox.Show("please input full information");
    
                }
    
                else
    
                {
    
                    removeFtpUploadString(textBox2.Text, path, username, password);
    
                    string text = FtpdownloadString(path, username, password);
    
                    string[] allname = text.Split('\n');
    
                    ListViewItem[] lvs = new ListViewItem[allname.Length];
    
                    for (int i = 0; i < lvs.Length; i++)
    
                    {
    
                        lvs[i] = new ListViewItem(new string[] { allname[i] });
    
                        btn.Text = allname[i];
    
                    }
    
                    this.listView1.Items.AddRange(lvs);
    
                    btn.Visible = false;
    
                    btn.Click += this.button_Click;
    
                    this.listView1.Controls.Add(btn);
    
                    this.btn.Size = new Size(this.listView1.Items[0].SubItems[0].Bounds.Width,
    
                    this.listView1.Items[0].SubItems[0].Bounds.Height);
    
                    MessageBox.Show("success");
    
                }
    
            }
    
            private void button2_Click(object sender, EventArgs e)
    
            {
    
                this.listView1.Items.Clear();
    
                string text = FtpdownloadString(path, username, password);
    
                string[] allname = text.Split('\n');
    
                var result = from info in allname where(info == textBox1.Text)
    
                             select info ;
    
                int count = 0;
    
                foreach (var item in result)
    
                {
    
                    count++;
    
                }
    
                ListViewItem[] lvs = new ListViewItem[count];
    
                string []all = result.ToArray();
    
                for (int i = 0; i < lvs.Length; i++)
    
                {
    
                    lvs[i] = new ListViewItem(new string[] { all[i] });
    
                    btn.Text = all[i];
    
                }
    
                this.listView1.Items.AddRange(lvs);
    
                btn.Visible = false;
    
                btn.Click += this.button_Click;
    
                this.listView1.Controls.Add(btn);
    
            }
    
        }
    
    }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 27, 2018 1:11 AM
    Moderator

All replies

  • Hi, Noam1248,

    Thank you for posting here.

    For your question, I want to confirm some questions with you.

    1. Is your “friend”  stored in the database, txt file, or others?

    2. What technology do you need to complete this process?

    Please provide detailed information so that we can help you better.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 23, 2018 7:00 AM
    Moderator
  • something like that: http://prntscr.com/llygn3

    if it helpes you, and all of the data will be on ftp server on txt or some other stuff.

    Friday, November 23, 2018 9:49 AM
  • Hi Noam1248,

    Thank you for posting here.

    For your question, you want to Set ftp to add friends or delete friends.

    Please try this  code below.

    using System;
    
    using System.Collections;
    
    using System.Collections.Generic;
    
    using System.ComponentModel;
    
    using System.Data;
    
    using System.Drawing;
    
    using System.IO;
    
    using System.Linq;
    
    using System.Net;
    
    using System.Text;
    
    using System.Text.RegularExpressions;
    
    using System.Threading.Tasks;
    
    using System.Windows.Forms;
    
    namespace test2
    
    {
    
        public partial class Form1 : Form
    
        {
    
            public Form1()
    
            {
    
                InitializeComponent();
    
            }
    
            private void btnsend_Click(object sender, EventArgs e)
    
            {
    
                richTextBox1.AppendText("I Say:"+textBox2.Text + Environment.NewLine);
    
              
    
            }
    
            private Button btn = new Button();
    
            private void Form1_Load(object sender, EventArgs e)
    
            {
    
                string text = FtpdownloadString(path, username, password);
    
                string[] allname = text.Split('\n');
    
                ListViewItem[] lvs = new ListViewItem[allname.Length];
    
                for (int i = 0; i < lvs.Length; i++)
    
                {
    
                    lvs[i]=new ListViewItem(new string[] { allname[i] });
    
                    btn.Text = allname[i];
    
                }
    
                this.listView1.Items.AddRange(lvs);
    
                btn.Visible =false;
    
                btn.Click += this.button_Click;
    
                this.listView1.Controls.Add(btn);
    
                this.btn.Size = new Size(this.listView1.Items[0].SubItems[0].Bounds.Width,
    
                this.listView1.Items[0].SubItems[0].Bounds.Height);
    
            }
    
          
    
            private void button1_Click(object sender, EventArgs e)
    
            {
    
                listView1.Items.Clear();
    
                if (textBox2.Text == "")
    
                {
    
                    MessageBox.Show("please input full information");
    
                }
    
                else
    
                {
    
                    FtpUploadString(textBox2.Text, path, username, password);
    
                    string text = FtpdownloadString(path, username, password);
    
                    string[] allname = text.Split('\n');
    
                    ListViewItem[] lvs = new ListViewItem[allname.Length];
    
                    for (int i = 0; i < lvs.Length; i++)
    
                    {
    
                        lvs[i] = new ListViewItem(new string[] { allname[i] });
    
                        btn.Text = allname[i];
    
                    }
    
                    this.listView1.Items.AddRange(lvs);
    
                    btn.Visible = false;
    
                    btn.Click += this.button_Click;
    
                    this.listView1.Controls.Add(btn);
    
                    this.btn.Size = new Size(this.listView1.Items[0].SubItems[0].Bounds.Width,
    
                    this.listView1.Items[0].SubItems[0].Bounds.Height);
    
                    MessageBox.Show("success");
    
                }
    
               
    
            }
    
            private void button_Click(object sender, EventArgs e)
    
            {
    
                label1.Text = "you are talking with" + this.listView1.SelectedItems[0].SubItems[0].Text;
    
            }
    
            private void listView1_SelectedIndexChanged_1(object sender, EventArgs e)
    
            {
    
                if (this.listView1.SelectedItems.Count > 0)
    
                {
    
                    this.btn.Location = new Point(this.listView1.SelectedItems[0].SubItems[0].Bounds.Left,
    
                        this.listView1.SelectedItems[0].SubItems[0].Bounds.Top);
    
                    this.btn.Visible = true;
    
                }
    
            }
    
            private void FtpUploadString(string text, string to_uri, string user_name, string password)
    
            {
    
                FtpWebRequest request =
    
                    (FtpWebRequest)WebRequest.Create(to_uri);
    
                request.Method = WebRequestMethods.Ftp.UploadFile;
    
                request.Credentials =
    
                    new NetworkCredential(user_name, password);
    
                request.ContentLength = FtpdownloadString(to_uri, user_name, password).Length + Environment.NewLine.Length + text.Length;
    
                string fi = FtpdownloadString(to_uri, user_name, password);
    
                using (Stream request_stream = request.GetRequestStream())
    
                {
    
                    byte[] bytes = Encoding.UTF8.GetBytes(fi + Environment.NewLine + text);
    
                    request_stream.Write(bytes, 0, (fi + Environment.NewLine + text).Length);
    
                    request_stream.Close();
    
                }
    
           
    
            }
    
            private void removeFtpUploadString( string text,string to_uri, string user_name, string password)
    
            {
    
                FtpWebRequest request =
    
                    (FtpWebRequest)WebRequest.Create(to_uri);
    
                request.Method = WebRequestMethods.Ftp.UploadFile;
    
                request.Credentials =
    
                    new NetworkCredential(user_name, password);
    
                request.ContentLength = FtpdownloadString(to_uri, user_name, password).Length- Environment.NewLine.Length-text.Length ;
    
                string fi = FtpdownloadString(to_uri, user_name, password);
    
                string str = Regex.Replace(fi, text+Environment.NewLine, "");
    
                using (Stream request_stream = request.GetRequestStream())
    
                {
    
                    byte[] bytes = Encoding.UTF8.GetBytes(str +Environment.NewLine );
    
                    request_stream.Write(bytes, 0, (str + Environment.NewLine).Length);
    
                    request_stream.Close();
    
                }
    
            }
    
            private string FtpdownloadString(string to_uri, string user_name, string password)
    
            {
    
                FtpWebRequest request =
    
                    (FtpWebRequest)WebRequest.Create(to_uri);
    
                request.Method = WebRequestMethods.Ftp.DownloadFile;
    
                request.Credentials =
    
                    new NetworkCredential(user_name, password);
    
                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    
                Stream ftpStream = response.GetResponseStream();
    
                StreamReader reader = new StreamReader(ftpStream);
    
                string filestr = reader.ReadToEnd();
    
                return filestr;
    
            }
    
            private void btnremove_Click(object sender, EventArgs e)
    
            {
    
                listView1.Items.Clear();
    
                if(textBox2.Text=="")
    
                {
    
                    MessageBox.Show("please input full information");
    
                }
    
                else
    
                {
    
                    removeFtpUploadString(textBox2.Text, path, username, password);
    
                    string text = FtpdownloadString(path, username, password);
    
                    string[] allname = text.Split('\n');
    
                    ListViewItem[] lvs = new ListViewItem[allname.Length];
    
                    for (int i = 0; i < lvs.Length; i++)
    
                    {
    
                        lvs[i] = new ListViewItem(new string[] { allname[i] });
    
                        btn.Text = allname[i];
    
                    }
    
                    this.listView1.Items.AddRange(lvs);
    
                    btn.Visible = false;
    
                    btn.Click += this.button_Click;
    
                    this.listView1.Controls.Add(btn);
    
                    this.btn.Size = new Size(this.listView1.Items[0].SubItems[0].Bounds.Width,
    
                    this.listView1.Items[0].SubItems[0].Bounds.Height);
    
                    MessageBox.Show("success");
    
                }
    
            }
    
            private void button2_Click(object sender, EventArgs e)
    
            {
    
                this.listView1.Items.Clear();
    
                string text = FtpdownloadString(path, username, password);
    
                string[] allname = text.Split('\n');
    
                var result = from info in allname where(info == textBox1.Text)
    
                             select info ;
    
                int count = 0;
    
                foreach (var item in result)
    
                {
    
                    count++;
    
                }
    
                ListViewItem[] lvs = new ListViewItem[count];
    
                string []all = result.ToArray();
    
                for (int i = 0; i < lvs.Length; i++)
    
                {
    
                    lvs[i] = new ListViewItem(new string[] { all[i] });
    
                    btn.Text = all[i];
    
                }
    
                this.listView1.Items.AddRange(lvs);
    
                btn.Visible = false;
    
                btn.Click += this.button_Click;
    
                this.listView1.Controls.Add(btn);
    
            }
    
        }
    
    }

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 27, 2018 1:11 AM
    Moderator