English 中文(简体)
Cache And Compare Files In C#
原标题:

Ok, I m trying to make an application for an online Radio Station.

I have it set to read the song title and artist and write it to a text file on the webserver.

I want to have the application store the text in a string or a cache, and then reread it every 15 seconds and if it isn t the same then update the info box.

Where the text is stored: http://xcastradio.com/stats/nowplaying.txt

I don t need it coded for me. I would just like to know how to store text in a string from a website.

最佳回答

See the example for System.Net.WebRequest.

Pulled from those docs (and modified for your applcation):

public String GetData(String url) {
    WebRequest request = WebRequest.Create(url);
    HttpWebResponse response = (HttpWebResponse) request.GetResponse();
    Stream dataStream = response.GetResponseStream();
    StreamReader reader = new StreamReader(dataStream);
    String data = reader.ReadToEnd();
    reader.Close();
    dataStream.Close();
    response.Close();
    return data;
}

Call it as:

String data = GetData("http://xcastradio.com/stats/nowplaying.txt");
问题回答

Use an HttpWebRequest/HttpWebResponse, use GetResponseStream, read it until no more bytes can be read, and put that into a byte array.

After you have the string, you then open a FileStream to a local file, and write that byte array with the Write method.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签