私は画像ファイルへのパスを取り、バイナリデータを文字列として返す関数を用意しています。呼び出し側は、返された文字列をXML要素の内部テキストに挿入します。バイナリデータと文字データの混同
は、この正しい?:
string dataAsString = "";
using (StreamReader sr = new StreamReader(new FileStream(mergedFile, FileMode.Open, FileAccess.Read, FileShare.Read, 2048, FileOptions.DeleteOnClose)))
dataAsString = sr.ReadToEnd();
return dataAsString;
これは、それが合法的にバイナリデータであるかもしれないようなものを返しますが、私は、対象ファイルのうち、内容をカット&ペーストした場合、新しいテストTIFファイルに貼り付けですtifイメージを開こうとすると、それは私には不幸です...私はデータの読み書き方法が間違っていると思います。
私は本当にこれを行う必要がありますか?
using (BinaryReader br = new BinaryReader(new FileStream(mergedFile, FileMode.Open, FileAccess.Read, FileShare.Read, 1024, FileOptions.None)))
{
int blockReadSz = 2048;
int bytesToRead = (int)br.BaseStream.Length;
int bytesRead = 0;
byte[] data = new byte[bytesToRead];
while (bytesRead < bytesToRead)
bytesRead += br.Read(data, bytesRead, blockReadSz);
}
もしそうなら、一度ファイルを読み終えたら、バイト[]から文字列を取り出すにはどうすればよいですか?
ありがとうございました! :D
File.ReadAllBytesが優れています。それを忘れてしまった! +1 – Keltex
ああ。 ReadAllBytesは便利です。 +1も同様です。 :D – cakeforcerberus