0

我正在为手持式 RFID 阅读器(Windows CE)开发一个 Web 应用程序,我正在尝试通过无线网络或 GPRS 将 XML 文件从 RFID 阅读器发送到笔记本电脑。该代码在 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());
}
}
}
4

1 回答 1

3

这是因为,正如错误所述,ReadAllBytes 在 Compact Framework 中不存在。您必须使用 Read 的重载来获取数据。

这些方面的东西:

using (var reader = File.OpenRead(filePath))
{
    var fileData = new byte[reader.Length];
    reader.Read(fileData, 0, fileData.Length);
}
于 2013-04-01T17:03:48.997 回答