2007年8月21日火曜日

バイナリの取り扱い

using System;
using System.IO;
class MyStream
{
    private const string FILE_NAME = "Test.data";
    public static void Main(String[] args)
    {
        // Create the new, empty data file.
        if (File.Exists(FILE_NAME))
        {
            Console.WriteLine("{0} already exists!", FILE_NAME);
            return;
        }
        FileStream fs = new FileStream(FILE_NAME, FileMode.CreateNew);
        // Create the writer for data.
        BinaryWriter w = new BinaryWriter(fs);
        // Write data to Test.data.
        for (int i = 0; i < 11; i++)
        {
            w.Write( (int) i);
        }
        w.Close();
        fs.Close();
        // Create the reader for data.
        fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
        BinaryReader r = new BinaryReader(fs);
        // Read data from Test.data.
        for (int i = 0; i < 11; i++)
        {
            Console.WriteLine(r.ReadInt32());
        }
        r.Close();
        fs.Close();
    }
}
----------------------------------------------------------------------
を参考にして
/// <summary>
/// ダウンロードバイナリ
/// </summary>
/// <param name="ServiceName"></param>
/// <param name="strFileName"></param>
public void Download_bin(String ServiceName, String strFileName)
{
        // パラメータの作成
        byte[] data = Encoding.ASCII.GetBytes("");
        // リクエストの作成
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strWebAppDomain + ServiceName);
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        req.ContentLength = data.Length;
        WebResponse res = req.GetResponse();
        // レスポンスの読み取り
        Stream resStream = res.GetResponseStream();
        ArrayList al = new ArrayList();
        int a = 0;
        while (a != -1)
        {
                a = resStream.ReadByte();
                al.Add(a);
        }
        resStream.Close();
        // 書き込み
        String FILE_NAME = strFileName;
        File.Delete(FILE_NAME);
        FileStream fs = new FileStream(FILE_NAME, FileMode.CreateNew);
        // Create the writer for data.
        BinaryWriter w = new BinaryWriter(fs);
        // Write data to Test.data.
        foreach(int i in al)
        {
                w.Write((byte)i);
        }
        w.Close();
        fs.Close();
}
なんだけど、下のほうのソースは動かないソース、今日は眠いのでアイデアだけ示して寝る~



0 件のコメント:

コメントを投稿