[英]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.