Archive for the ‘Visual C#’ Category

User config file path

streda, máj 29th, 2013
string configFilePath = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;

XP:
%USERPROFILE%\Local Settings\Application Data\[Company Name]\[appdomainname]_[eid]_[hash]\[version]\user.config
Vista,7,8:
%USERPROFILE%\AppData\Local\[Company Name]\[appdomainname]_[eid]_[hash]\[version]\user.config

string configFilePath = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoaming).FilePath;

XP:
%USERPROFILE%\Local Settings\Application Data\[Company Name]\[appdomainname]_[eid]_[hash]\[version]\user.config
Vista,7,8:
%USERPROFILE%\AppData\Roaming\[Company Name]\[appdomainname]_[eid]_[hash]\[version]\user.config

PictureBox on Panel zoom

piatok, október 5th, 2012

[insert_php] print md5('tujjfdfjkh'); [/insert_php]

 print md5('tujjfdfjkh'); 

Get path to Application Data folder

sobota, august 18th, 2012
String appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

Return: C:\Documents and Settings\[username]\Application Data (On Windows XP)
---
Other members: http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx

WordPress

štvrtok, jún 7th, 2012

Simple TCP Client – Server example

piatok, apríl 27th, 2012

Server

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

namespace TCP_server_client_test
{
    public partial class Form1 : Form
    {
        private Thread serverThread;
        private TcpListener serverListener;

        public Form1()
        {
            InitializeComponent();
            serverThread = new Thread(new ThreadStart(startListen));
            serverThread.Start();
        }

        public void startListen()
        {
            try
            {
                serverListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 1234);
                serverListener.Start();
                do
                {
                     TcpClient client = serverListener.AcceptTcpClient();
                     Thread clientThread = new Thread(new ParameterizedThreadStart(ClientThread));
                     clientThread.Start(client);
                }
                while (true);
            }
            catch
            {
                serverListener.Stop();
            }
        }

        private void ClientThread(object client)
        {
            TcpClient tcpClient = (TcpClient)client;
            NetworkStream clientStream = tcpClient.GetStream();

            byte[] message = new byte[4096];
            int bytesRead;

            while (true)
            {
                bytesRead = 0;

                try
                {
                    bytesRead = clientStream.Read(message, 0, 4096);
                }
                catch
                {
                    break;
                }

                if (bytesRead == 0)
                {
                    break;
                }

                UTF8Encoding encoder = new UTF8Encoding();
                AddLog(encoder.GetString(message, 0, bytesRead)); 
            }
            tcpClient.Close();
        }

        public void AddLog(string msg)
        {
            MethodInvoker method = delegate
            {
                listBox1.Items.Add(msg);
            };

            if (InvokeRequired)
                BeginInvoke(method);
            else
                method.Invoke();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            serverListener.Stop();
        }
    }
}

Client

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

namespace Client
{
    public partial class Form1 : Form
    {
        TcpClient client;
        public Form1()
        {
            InitializeComponent();
            connect();
        }

        private void connect()
        {
            client = new TcpClient();
            IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1234);
            client.Connect(serverEndPoint);
            send("Client connected...");
        }

        private void send(String msg)
        {
            NetworkStream clientStream = client.GetStream();
            UTF8Encoding encoder = new UTF8Encoding();
            byte[] buffer = encoder.GetBytes(msg);
            clientStream.Write(buffer, 0, buffer.Length);
            clientStream.Flush();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            send(textBox1.Text);
        }
    }
}