简体   繁体   English

当我的 c# 程序中没有任何内容标记为 static 时,我收到 object 必须标记为 static 错误?

[英]I am getting an object must be marked static error when nothing in my c# program is marked static?

So I am writing a C# program in which the server will encode a selected file into bytes and then send it to a client computer.所以我正在编写一个 C# 程序,其中服务器会将选定的文件编码为字节,然后将其发送到客户端计算机。 However, for a reason I can't find, I am getting a "An object reference is required for the non-static field, method, or property 'Encoding.GetBytes(string)' " error, which is very odd as seen in the code below, nothing in the entire code is set to public or static so not sure why it would be generating this error?但是,由于我找不到的原因,我收到“非静态字段、方法或属性‘Encoding.GetBytes(string)’需要一个 object 引用”错误,这非常奇怪,如下面的代码,整个代码中没有任何内容设置为 public 或 static 所以不确定为什么会产生这个错误? The line where Visual Studio throws the error is marked below.下面标记了 Visual Studio 抛出错误的行。 Any thoughts are appreciated.任何想法表示赞赏。 Thank you.谢谢你。

void SendFile_Click(object sender, EventArgs e)
        {

            try
            {
                FileSendProgress.Maximum = 100;
                IPAddress ipAd = IPAddress.Parse(ClientIP.Text); //use local m/c IP address, and use the same in the client

                /* Initializes the Listener */
                TcpListener myList = new TcpListener(ipAd, 8001);

                /* Start Listeneting at the specified port */
                myList.Start();

                MessageBox.Show("The server is running at port 8001...");
                MessageBox.Show("The local End point is  :" + myList.LocalEndpoint);
                MessageBox.Show("Looking for other computer");

                Socket s = myList.AcceptSocket();
                FSStatus.Text = "Connnected to client computer......Sending data now, hold on to your hats. " + FileSendProgress.Value.ToString();
                FileSendProgress.Value += 5;
                ASCIIEncoding asen = new ASCIIEncoding();
                s.Send(asen.GetBytes(DURL.Text)); // This is the line where the error is thrown.
                MessageBox.Show("The selected file : " + FileToSend.Text + "is now on it's way to the client computer. It may take a while if a large file is being sent.");

                byte[] b = new byte[100];
                int k = s.Receive(b);
                for (int i = 0; i < k; i++)
                    Console.Write(Convert.ToChar(b[i]));
                FileSendProgress.Value += 45;

                FSStatus.Text = "Encoding........ " + FileSendProgress.Value.ToString();
                /* clean up */



                byte[] fileContent = null;
                System.IO.FileStream fs = new System.IO.FileStream(FileToSend.Text, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(fs);
                long byteLength = new System.IO.FileInfo(FileToSend.Text).Length;
                fileContent = binaryReader.ReadBytes((Int32)byteLength);
                byte[] bb = new byte[1024];

                FSStatus.Text = "Transmitting now...... " + FileSendProgress.Value.ToString();
                string file = Encoding.UTF8.GetString(bb.AsSpan(0, k));
                FileSendProgress.Value += 15;
                s.Send(ASCIIEncoding.GetBytes(file));
                    fs.Close();
                    fs.Dispose();

                    binaryReader.Close();
                    s.Close();
                    myList.Stop(); 
                FileSendProgress.Value += 35;
             
                FSStatus.Text = "File Sent " + FileSendProgress.Value.ToString();


            }

                catch (Exception n)
                {
                    MessageBox.Show("Please make sure the file is selected, or the file is not supported. " + n);
                }
            
          
            }

GetBytes() is not a static method. GetBytes() 不是 static 方法。 You probably want to have:你可能想要:

asen.GetBytes(file)

instead of代替

ASCIIEncoding.GetBytes(file)

Try Encoding.ASCII.GetBytes(file) instead of ASCIIEncoding.GetBytes(file) .尝试Encoding.ASCII.GetBytes(file)而不是ASCIIEncoding.GetBytes(file)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我在C#中得到“非静态字段,方法或属性需要对象引用”错误? - Why am I getting “object reference is required for the non-static field, method, or property” error in C#? 常量不能被标记为静态 - The constant cannot be marked static 为什么我得到“程序集 &#39;*.dll&#39; 必须经过强签名才能被标记为先决条件。”? - Why am I getting 'Assembly '*.dll' must be strong signed in order to be marked as a prerequisite.'? C#必须声明一个主体,因为它没有被标记为抽象,外部或部分 - C# must declare a body because it is not marked abstract, extern, or partial C#DPAPI必须声明一个主体,因为它没有被标记为抽象 - C# DPAPI must declare a body because it is not marked abstract C#“必须声明一个主体,因为它没有被标记为抽象、外部或部分” - C# “must declare a body because it is not marked abstract, extern, or partial” 当我尝试将结果写入 textBox 时,我收到关于 Control.Invoke 的错误。 - when i try to write result into textBox i am getting error about Control.Invoke must be .... whats the wrong in my code i am also new in c# 出现错误“必须声明一个主体,因为它没有被标记为抽象或外部” - Getting error“must declare a body because it is not marked abstract or extern” 使用C#插入记录时,出现“必须声明标量变量“ @ Val1””错误 - I am getting the “Must declare the scalar variable ”@Val1“” error when inserting the record using C# C# 程序似乎正在修改标记为只读的参数 - C# program seems to be modifying a parameter marked as readonly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM