.net 我得到一个对象必须标记为静态错误时,我的c#程序中没有任何东西被标记为静态?

dvtswwa3  于 2022-12-20  发布在  .NET
关注(0)|答案(2)|浏览(100)

所以我正在编写一个C#程序,服务器将把选定的文件编码成字节,然后将其发送到客户端计算机。然而,由于我找不到的原因,我得到了一个“非静态字段、方法或属性需要对象引用”Encoding.GetBytes(字符串)'“错误,如下面的代码所示,这非常奇怪,整个代码中没有任何内容被设置为公共或静态,因此不确定为什么会生成此错误?Visual Studio抛出错误的行标记如下。感谢您的任何想法。谢谢。

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);
                }
            
          
            }
vs91vp4v

vs91vp4v1#

GetBytes()不是静态方法。您可能需要:

asen.GetBytes(file)

代替

ASCIIEncoding.GetBytes(file)
lnlaulya

lnlaulya2#

请尝试使用Encoding.ASCII.GetBytes(file),而不是ASCIIEncoding.GetBytes(file)

相关问题