我正在为手持RFID阅读器(windows CE)开发一个Web应用程序, 我正在尝试将RFID文件从RFID阅读器发送到笔记本电脑无线网络或GPRS。该代码适用于MS visual studio上的“Windows窗体应用程序”,但当我尝试将其与“智能设备应用程序”一起使用时,它不起作用...“ReadAllBytes”方法出现错误:
System.IO.File dose not contain a definition for ReadAllBytes
请帮我处理这个错误。 感谢。
代码:
private void button1_Click(object sender, EventArgs e)
{
try
{
string IpAddressString = "10.1.1.104";
IPEndPoint ipEnd_client = new
IPEndPoint(IPAddress.Parse(IpAddressString), 5656);
Socket clientSock_client = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.IP);
string fileName = "student.XML";
string filePath =@"My Device\";
fileName = fileName.Replace("\\", "/");
while (fileName.IndexOf("/") > -1)
{
filePath += fileName.Substring(0, fileName.IndexOf("/") + 1);
fileName = fileName.Substring(fileName.IndexOf("/") + 1);
}
byte[] fileNameByte = Encoding.UTF8.GetBytes(fileName);
if (fileNameByte.Length > 5000 * 1024)
{
curMsg_client = "File size is more than 5Mb,
please try with small file.";
MessageBox.Show("File size is more than 5Mb,
please try with small file.");
return;
}
MessageBox.Show("Buffering ...");
string fullPath = filePath + fileName;
byte[] fileData =File.ReadAllBytes(fullPath);
byte[] clientData = new byte[4 + fileNameByte.Length +
fileData.Length];
//byte[] clientData = new byte[4 + fileNameByte.Length];
byte[] fileNameLen = BitConverter.GetBytes(fileNameByte.Length);
fileNameLen.CopyTo(clientData, 0);
fileNameByte.CopyTo(clientData, 4);
fileData.CopyTo(clientData, 4 + fileNameByte.Length);
MessageBox.Show("Connection to server ...");
clientSock_client.Connect(ipEnd_client);
MessageBox.Show("File sending...");
clientSock_client.Send(clientData, 0, clientData.Length, 0);
MessageBox.Show("Disconnecting...");
clientSock_client.Close();
MessageBox.Show ("File [" + fullPath + "] transferred.");
}
catch (Exception ex)
{
if (ex.Message == "No connection could be made
because the target machine actively refused it")
{
MessageBox.Show ("File Sending fail. Because
server not running.");
}
else
{
MessageBox.Show ("File Sending fail." +
ex.Message.ToString());
}
}
}
答案 0 :(得分:3)
这是因为,正如错误所述,Compact Framework中不存在ReadAllBytes。您必须使用Read的重载来获取数据。
这些方面的东西:
using (var reader = File.OpenRead(filePath))
{
var fileData = new byte[reader.Length];
reader.Read(fileData, 0, fileData.Length);
}