IIS に Web をデプロイしたところ、WPF 側でそこからデータをダウンロードしたいので、次のようにします。
public StartWindow()
{
InitializeComponent();
label11.Width = Double.NaN;
stackPanelTitle.Width = Double.NaN;
DownloadData("http://localhost/iStellarMobile_deploy/Puzzle/CrossTest.txt"); // null exception
}
protected void DownloadData(string strFileUrlToDownload)
{
byte[] myDataBuffer = client.DownloadData((new Uri(strFileUrlToDownload))); // null exception
MemoryStream storeStream = new MemoryStream();
storeStream.SetLength(myDataBuffer.Length);
storeStream.Write(myDataBuffer, 0, (int)storeStream.Length);
storeStream.Flush();
using (FileStream file = new FileStream(@"C:\TestFile.txt", FileMode.Create, System.IO.FileAccess.Write))
{
byte[] bytes = new byte[storeStream.Length];
storeStream.Read(bytes, 0, (int)storeStream.Length);
file.Write(bytes, 0, bytes.Length);
storeStream.Close();
}
//TO save into certain file must exist on Local
//storeStream.SaveAs(storeStream, @"C:\TestFile.txt");
//The below Getstring method to get data in raw format and manipulate it as per requirement
string download = Encoding.ASCII.GetString(myDataBuffer);
}
これは正しい方法ですか?http://www.c-sharpcorner.com/UploadFile/97fc7a/reading-files-from-given-specific-url-using-webclient/から参照します
コメントした2行でnull例外が発生します。