Jump to content

Is it possible to have class files of a program in one computer and use them from a different computer? (C#)

if i want to display a image from database in a form, and if database is located in one PC and if the forms are located on another PC, is it possible for me to have class files in the same computer where database is in and call these class files in the forms through a network and use to display things like images and text in the forms?

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Erik Sieghart said:

What you've described as what you want to do isn't really solved by what you're proposing. Thinking about the problem more specifically, you probably shouldn't be storing images into a database to start, as it's better to store their filepaths there. Second, what you're trying to do can be accomplished more simply by making a database call. This is kinda why databases are a thing, and what they're designed to do (get request, give stuff back that matches request).

 

2 hours ago, Shammikit said:

if i want to display a image from database in a form, and if database is located in one PC and if the forms are located on another PC, is it possible for me to have class files in the same computer where database is in and call these class files in the forms through a network and use to display things like images and text in the forms?

Storing paths to files in databases is one of the worst things to maintain in the database. 

 

If you use MS SQL Server, you can use something called Filestream that utilizes filesystem to store files and stores their reference in the database. You can access files by simply downloading their raw data and then using them in your program. It's the best way to store files in the database. If both of your computers are connected to the network then just open a TCP protocol in Sql Server and you can connect to the DB from another computer and download images.

Try, fail, learn, repeat...

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, Shammikit said:

if i want to display a image from database in a form, and if database is located in one PC and if the forms are located on another PC, is it possible for me to have class files in the same computer where database is in and call these class files in the forms through a network and use to display things like images and text in the forms?

Yes, like Erik says, the plain class files aren't of any use to a running application.

 

You would need to take the class files, compile them into an assembly, and then call the code from that assembly. StackOverflow: How to load a class from a .cs file contains some information on the subject. But this is overly complicated and probably only actually needed in very rare situations (ie: it's very unlikely you need to do it).

 

I expect what you're trying to achieve can be done in a much better/easier way.

 

For example, most databases can be set up and accessed remotely from other computers. This may be good enough for your purposes but be aware that in can introduce security issues.

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, Erik Sieghart said:

Short answer: no

Long answer:
What you want isn't exactly possible because .NET doesn't work that way. Once you compile your program into binaries, it's no longer a list of different files and instead in a new language altogether called common intermediate language. C# really needs the entire scope of the program to compile correctly, and I don't know of any way to get around this through any reasonable method.

 

In an interpreted language like python it would be possible to scp the files from a server to the python directory, since individual files can be replaced as part of the program's execution, so long as your careful with your import statements, in any case.

What you've described as what you want to do isn't really solved by what you're proposing. Thinking about the problem more specifically, you probably shouldn't be storing images into a database to start, as it's better to store their filepaths there. Second, what you're trying to do can be accomplished more simply by making a database call. This is kinda why databases are a thing, and what they're designed to do (get request, give stuff back that matches request).

 

8 hours ago, zwirek2201 said:

 

Storing paths to files in databases is one of the worst things to maintain in the database. 

 

If you use MS SQL Server, you can use something called Filestream that utilizes filesystem to store files and stores their reference in the database. You can access files by simply downloading their raw data and then using them in your program. It's the best way to store files in the database. If both of your computers are connected to the network then just open a TCP protocol in Sql Server and you can connect to the DB from another computer and download images.

 

8 hours ago, madknight3 said:

Yes, like Erik says, the plain class files aren't of any use to a running application.

 

You would need to take the class files, compile them into an assembly, and then call the code from that assembly. StackOverflow: How to load a class from a .cs file contains some information on the subject. But this is overly complicated and probably only actually needed in very rare situations (ie: it's very unlikely you need to do it).

 

I expect what you're trying to achieve can be done in a much better/easier way.

 

For example, most databases can be set up and accessed remotely from other computers. This may be good enough for your purposes but be aware that in can introduce security issues.

 

ok then what if i have 2 separate applications and i send user input from one application through a network to the other and make that process everything and send back the data to display to the user? I did a search on this and i came across this tcp client/server application thing. I also found a basic chat application where u build and open the application twice (that is if u r running the application on the same computer), have to enter the port numbers and it is possible to send one message at a time. i want to know if its possible to send multiple strings to a method in a specific class in the other application using this way?Here is the code i have tried:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace chat
{
    public partial class Form1 : Form
    {
        Socket sck;
        EndPoint epLocal, epRemote;

        public Form1()
        {
            InitializeComponent();
            sck = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            sck.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            txb_c1_ip.Text = GetLocalIP();
            txb_c2_ip.Text = GetLocalIP();
        }

       
       
        private string GetLocalIP()
        {
            IPHostEntry host;
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily == AddressFamily.InterNetwork)
                    return ip.ToString();
            }
            return "127.0.0.1";
        }
        private void MessageCallBack(IAsyncResult aResult)
        {
            try
            {
                int size = sck.EndReceiveFrom(aResult, ref epRemote);
                if (size > 0)
                {
                    byte[] receiveData = new byte[1464];
                    receiveData = (byte[])aResult.AsyncState;
                    UTF8Encoding enc = new UTF8Encoding();
 
                    string receiveM = enc.GetString(receiveData);
                    txb_msg.Text = receiveM; 
                }
                byte[] buffer = new byte[1500];
                sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
          
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

   

        private void Btn_Start_Click_1(object sender, EventArgs e)
        {
            try
            {
                epLocal = new IPEndPoint(IPAddress.Parse(txb_c1_ip.Text), Convert.ToInt32(txb_c1_p.Text));
                sck.Bind(epLocal);
                epRemote = new IPEndPoint(IPAddress.Parse(txb_c2_ip.Text), Convert.ToInt32(txb_c2_p.Text));
                sck.Connect(epRemote);
                byte[] buffer = new byte[1500];
                sck.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref epRemote, new AsyncCallback(MessageCallBack), buffer);
                Btn_Start.Text = "Connected";
                Btn_Start.Enabled = false;
                Btn_send.Enabled = true;
                txb_msg.Focus();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void Btn_send_Click_1(object sender, EventArgs e)
        {
            try
            {
                UTF8Encoding enc = new UTF8Encoding();
                byte[] msg = new byte[1500];
                msg = enc.GetBytes(txb_msg.Text);
                sck.Send(msg);
                Lst_msg.Items.Add("You: " + txb_msg.Text);
                txb_msg.Clear();

         
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

       
    }
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, Shammikit said:

 

 

 

ok then what if i have 2 separate applications and i send user input from one application through a network to the other and make that process everything and send back the data to display to the user? I did a search on this and i came across this tcp client/server application thing. I also found a basic chat application where u build and open the application twice (that is if u r running the application on the same computer), have to enter the port numbers and it is possible to send one message at a time. i want to know if its possible to send multiple strings to a method in a specific class in the other application using this way?Here is the code i have tried:

 

https://www.codeproject.com/Articles/429144/Simple-Instant-Messenger-with-SSL-Encryption-in-Cs

 

Here you have a simple chat application that allows multiple users to send/receive messages. It also has some SSL functionality.

 

If you want to be able to send and receive message on one client at the same time, you should use two threads with TCPClient classes running in them. One responsible for receiving messages and one for sending messages. 

 

I used that example application to make my bachelor program which got out of hand a little bit and is running 4 threads for each client to send/receive messages and at the same time upload/download files. It works great even with multiple clients.

 

If you have questions - PM me.

Try, fail, learn, repeat...

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×