我試圖找到解決此錯誤的解決方法,因爲我正在比較從主機到本地系統上的文件大小,但我試試這個:C#<不能應用於'long'和'string'類型的操作數
if (CheckFileBytes < FileBytes)
它提供了以下錯誤
Operator '<' cannot be applied to operands of type 'long' and 'string'
下面是一些代碼:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if (File.Exists(Application.StartupPath + "\\checkfiles.lst"))
{
string path = Application.StartupPath + "\\checkfiles.lst";
// Open the file to read from.
foreach (string readText in File.ReadLines(path))
{
var elements = readText.Split('|');
ElementsFile = elements[0];
MD5Hash = elements[1];
FileBytes = elements[2];
//Console.WriteLine(ElementsFile + MD5Hash + FileBytes);
string fileName = Application.StartupPath + "\\" + ElementsFile;
byte[] buffer;
int bytesRead;
long size;
long totalBytesRead = 0;
using (Stream file = File.OpenRead(fileName))
{
size = file.Length;
//var fileName = Application.StartupPath + "\\" + ElementsFile;
FileInfo fi = new FileInfo(fileName);
var Filebytes = fi.Length;
using (HashAlgorithm hasher = MD5.Create())
{
do
{
buffer = new byte[4096];
bytesRead = file.Read(buffer, 0, buffer.Length);
totalBytesRead += bytesRead;
hasher.TransformBlock(buffer, 0, bytesRead, null, 0);
}
while (bytesRead != 0);
hasher.TransformFinalBlock(buffer, 0, 0);
HashString = MakeHashString(hasher.Hash);
}
//Return Local file size
long CheckFileBytes = Filebytes;
//Return local MD5 hash string
string CheckFileHash = HashString;
//Return local file path
if (CheckFileHash != MD5Hash && CheckFileBytes < FileBytes)
{
...
}
}
後續ING是在課程開始時宣佈:
string ElementsFile = null;
string MD5Hash = null;
string FileBytes = null;
string HashString = null;
所以,*究竟*不清楚?你有一個*號*('長')和一個*字符串*,它們之間沒有定義的順序(或'<'運算符)。我懷疑你會想*先把字符串轉換成數字,或者不是,但是錯誤的原因是一樣的。 – user2864740
'FileBytes'是一個聲明字符串..我們不能比較長的字符串..你的編譯器說的是正確的.. :) –
我明白,但我將如何改變他們周圍的工作? – tomirons