Wednesday, April 11, 2012

Read file from internet location and read through the lines

public partial class MainPage : PhoneApplicationPage
{
WebClient _webClient; // Used for downloading
}

public MainPage()
{
InitializeComponent();
_webClient = new WebClient();



_webClient.OpenWriteCompleted += (s1, e1) =>
{
//image1.Opacity = 1;
 
};

// when asyncread is completed this event is called

_webClient.OpenReadCompleted += (s1, e1) =>
{
if (e1.Error == null)
{
try
{
// parse out the forward slashes to give a nice beautiful aesthetic filename Nigel
fileName = txtURL.Text.Substring(txtURL.Text.LastIndexOf("/") + 1).Trim();
//
fileNameDisplay = fileName.Split('.'); // make it look better nigel
//textcode.Text = fileNameDisplay[0];
 
 
//txtUrl.Text.Substring(txtUrl.Text.LastIndexOf(“/”)+1).Trim();
bool isSpaceAvailable = IsSpaceIsAvailable(e1.Result.Length);
bool filenotpresent = FileNotPresent(fileName);
 
if ((isSpaceAvailable) && (filenotpresent))
// if they downloaded previously no need to download again
{
// Save mp3 to Isolated Storage
 
 
using (var isfs = new IsolatedStorageFileStream(fileName,
FileMode.CreateNew,
IsolatedStorageFile.GetUserStoreForApplication()))
{
long fileLen = e1.Result.Length;
byte[] b = new byte[fileLen];
e1.Result.Read(b, 0, b.Length);
isfs.Write(b, 0, b.Length);
isfs.Flush();
}
 
 
 
 
}
else
{
MessageBox.Show("Not enough to save space available to store file.");
}
textBlock33.Text = "Downloadfinished";
// playthesound();
readthefile(); // once we have dowloaded we can read the file see readtehfile below.
 
 
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
MessageBox.Show(e1.Error.Message);
}
};

}


private void Button_DownloadTextFile_Click(object sender, System.Windows.RoutedEventArgs e)
{ // if (_webClient.IsBusy)
{ // button already clicked and webclient still downloading, just state still downloading and bail NIGEL
textBlock33.Text = "Still Downloading..."; }
else {//examples
// txtURL.Text = "http://home.yourwebsiteprovicer.net/~yournameforexample/thefilename.txt"; txtURL.Text = "http://www.gutenberg.org/cache/epub/1400/pg1400.txt"; textBlock33.Text = "Downloading. . . ";
try {
_webClient.OpenReadAsync(new Uri(txtURL.Text.ToString()));
} catch
{ MessageBox.Show("problem occurred");
} textBlock33.Text = "Download Done Nigel"; }

}




void readthefile()
{
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("nigel2.txt", FileMode.Open, FileAccess.Read))
{
//System.IO.Stream filestream = Application.GetResourceStream(new Uri("nigel2.txt", UriKind.Relative)).Stream;
using (StreamReader reader = new StreamReader(fileStream))
{ //Visualize the text data in a TextBlock text
// for (float soundtrip = .01f; soundtrip < 1f; soundtrip += .01f)
for (int nigelcounter = 0; nigelcounter < 4; nigelcounter += 1) // READ FIRST 4 LINES
{
theholder = reader.ReadLine();
textBlock33.Text += theholder;
}
// this.textBlock4.Text += reader.ReadLine();
}
fileStream.Close();
}

}

}

No comments:

Post a Comment