private void listFolderContent(bool isRoot, List <FTPDirectoryItemInfo> dil) { FTPDirectoryItems.Clear(); StringBuilder sb = new StringBuilder(); foreach (string s in _folder) { sb.AppendFormat("/{0}", s); } CurrentPath = sb.ToString(); if (!isRoot) { FTPDirectoryItemInfo fdi = new FTPDirectoryItemInfo(); fdi.Folder = true; fdi.Name = ".."; FTPDirectoryItems.Add(fdi); } var li = from l in dil where l.Folder orderby l.Name select l; foreach (var l in li) { FTPDirectoryItems.Add(l); } li = from l in dil where !l.Folder orderby l.Name select l; foreach (var l in li) { FTPDirectoryItems.Add(l); } }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { FTPDirectoryItemInfo fi = e.Argument as FTPDirectoryItemInfo; DownloadedFileName = fi.Name; try { DownloadedFile = new TemporaryFile(false); string fn = "ftp://download.mapsforge.org/"; for (int i = 0; i < _folder.Count; i++) { fn = string.Format("{0}{1}/", fn, _folder[i]); } fn = string.Format("{0}{1}", fn, fi.Name); using (Utils.ProgressBlock prog = new Utils.ProgressBlock(_plugin, STR_DOWNLOADINGFILE, fn, (int)fi.Size, 0)) { FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(fn)); ftpRequest.Credentials = new NetworkCredential("anonymous", "*****@*****.**"); ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile; int bytesTotal = 0; using (FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse()) using (Stream responseStream = ftpResponse.GetResponseStream()) using (FileStream writeStream = new FileStream(DownloadedFile.Path, FileMode.Create)) { int Length = 2048 * 1000; Byte[] buffer = new Byte[Length]; int bytesRead = responseStream.Read(buffer, 0, Length); while (bytesRead > 0) { bytesTotal += bytesRead;// don't forget to increment bytesRead ! writeStream.Write(buffer, 0, bytesRead); bytesRead = responseStream.Read(buffer, 0, Length); prog.UpdateProgress(STR_DOWNLOADINGFILE, fn, (int)fi.Size, bytesTotal); } } if (bytesTotal != (int)fi.Size) { DownloadedFile = null; DownloadedFileName = ""; } } } catch { DownloadedFile = null; DownloadedFileName = ""; } }
private void button1_Click(object sender, EventArgs e) { if (listView1.SelectedItems.Count > 0) { FTPDirectoryItemInfo fi = listView1.SelectedItems[0].Tag as FTPDirectoryItemInfo; if (fi != null) { button1.Enabled = false; listView1.Enabled = false; this.backgroundWorker1.RunWorkerAsync(fi); } } }
public bool canDownload() { bool result = false; FTPDirectoryItemInfo si = listItems.SelectedItem as FTPDirectoryItemInfo; if (si != null) { if (!si.Folder) { result = true; } } return(result); }
private List <FTPDirectoryItemInfo> GetFileList(string fullPath) { label2.Text = Utils.LanguageSupport.Instance.GetTranslation(STR_RETRIEVINGLIST); label2.Refresh(); List <FTPDirectoryItemInfo> result = new List <FTPDirectoryItemInfo>(); this.Cursor = Cursors.WaitCursor; try { FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(fullPath)); ftpRequest.Credentials = new NetworkCredential("anonymous", "*****@*****.**"); ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails; ftpRequest.KeepAlive = false; string line; using (FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse()) using (StreamReader sr = new StreamReader(ftpResponse.GetResponseStream())) { while (!sr.EndOfStream) { line = sr.ReadLine(); FTPDirectoryItemInfo di = new FTPDirectoryItemInfo(); di.Folder = line.StartsWith("d"); di.Name = line.Substring(line.LastIndexOf(' ') + 1); di.Size = 0; string[] parts = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); try { di.Size = long.Parse(parts[4]); } catch { } if (di.Name != "0.2.4-archive") { result.Add(di); } } } } catch { result = null; } this.Cursor = Cursors.Default; label2.Text = ""; return(result); }
private List <FTPDirectoryItemInfo> GetFileList(string fullPath) { List <FTPDirectoryItemInfo> result = new List <FTPDirectoryItemInfo>(); try { FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(fullPath)); ftpRequest.Credentials = new NetworkCredential("anonymous", "*****@*****.**"); ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails; ftpRequest.KeepAlive = false; string line; using (FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse()) using (StreamReader sr = new StreamReader(ftpResponse.GetResponseStream())) { while (!sr.EndOfStream) { line = sr.ReadLine(); FTPDirectoryItemInfo di = new FTPDirectoryItemInfo(); di.Folder = line.StartsWith("d"); di.Name = line.Substring(line.LastIndexOf(' ') + 1); di.Size = 0; string[] parts = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); try { di.Size = long.Parse(parts[4]); } catch { } if (di.Name != "0.2.4-archive") { result.Add(di); } } } } catch (Exception e) { Core.ApplicationData.Instance.Logger.AddLog(this, e); result = null; } return(result); }
private void listView1_Click(object sender, EventArgs e) { if (listView1.SelectedIndices.Count > 0) { if (listView1.SelectedItems[0].Tag == null) { //move up string folder = "ftp://download.mapsforge.org/"; for (int i = 0; i < _folder.Count - 1; i++) { folder = string.Format("{0}{1}/", folder, _folder[i]); } List <FTPDirectoryItemInfo> fls = GetFileList(folder); if (fls != null) { _folder.RemoveAt(_folder.Count - 1); listFolderContent(_folder.Count < 2, fls); } button1.Enabled = false; } else { FTPDirectoryItemInfo fi = listView1.SelectedItems[0].Tag as FTPDirectoryItemInfo; if (fi.Folder) { string folder = "ftp://download.mapsforge.org/"; for (int i = 0; i < _folder.Count; i++) { folder = string.Format("{0}{1}/", folder, _folder[i]); } folder = string.Format("{0}{1}/", folder, fi.Name); List <FTPDirectoryItemInfo> fls = GetFileList(folder); if (fls != null) { _folder.Add(fi.Name); listFolderContent(false, fls); } button1.Enabled = false; } } } }
private async void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e) { FTPDirectoryItemInfo si = listItems.SelectedItem as FTPDirectoryItemInfo; if (si != null) { if (si.Name == "..") { //move up string folder = "ftp://download.mapsforge.org/"; for (int i = 0; i < _folder.Count - 1; i++) { folder = string.Format("{0}{1}/", folder, _folder[i]); } List <FTPDirectoryItemInfo> fls = await GetFileListAsync(folder); if (fls != null) { _folder.RemoveAt(_folder.Count - 1); listFolderContent(_folder.Count < 2, fls); } } else if (si.Folder) { string folder = "ftp://download.mapsforge.org/"; for (int i = 0; i < _folder.Count; i++) { folder = string.Format("{0}{1}/", folder, _folder[i]); } folder = string.Format("{0}{1}/", folder, si.Name); List <FTPDirectoryItemInfo> fls = await GetFileListAsync(folder); if (fls != null) { _folder.Add(si.Name); listFolderContent(false, fls); } } } }
public async Task DownloadMap() { DownloadedFilePath = null; FTPDirectoryItemInfo si = listItems.SelectedItem as FTPDirectoryItemInfo; if (si != null && !si.Folder) { await Task.Run(() => { try { using (TemporaryFile DownloadedFile = new TemporaryFile(false)) { string fn = "ftp://download.mapsforge.org/"; for (int i = 0; i < _folder.Count; i++) { fn = string.Format("{0}{1}/", fn, _folder[i]); } fn = string.Format("{0}{1}", fn, si.Name); DateTime nextUpdate = DateTime.Now.AddSeconds(1); using (Utils.ProgressBlock prog = new Utils.ProgressBlock("DownloadingFile", fn, (int)si.Size, 0)) { FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(fn)); ftpRequest.Credentials = new NetworkCredential("anonymous", "*****@*****.**"); ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile; int bytesTotal = 0; using (FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse()) using (Stream responseStream = ftpResponse.GetResponseStream()) using (FileStream writeStream = new FileStream(DownloadedFile.Path, FileMode.Create)) { int Length = 2048 * 1000; Byte[] buffer = new Byte[Length]; int bytesRead = responseStream.Read(buffer, 0, Length); while (bytesRead > 0) { bytesTotal += bytesRead;// don't forget to increment bytesRead ! writeStream.Write(buffer, 0, bytesRead); bytesRead = responseStream.Read(buffer, 0, Length); if (DateTime.Now >= nextUpdate) { prog.Update(fn, (int)si.Size, bytesTotal); nextUpdate = DateTime.Now.AddSeconds(1); } } } if (bytesTotal == (int)si.Size) { //copy to correct location string p = System.IO.Path.Combine(Core.Settings.Default.SettingsFolder, "Maps"); if (!Directory.Exists(p)) { Directory.CreateDirectory(p); } DownloadedFilePath = System.IO.Path.Combine(p, si.Name); File.Copy(DownloadedFile.Path, DownloadedFilePath, true); } } } } catch (Exception e) { DownloadedFilePath = null; Core.ApplicationData.Instance.Logger.AddLog(this, e); } }); } if (!string.IsNullOrEmpty(DownloadedFilePath)) { DialogResult = true; Close(); } }
private List<FTPDirectoryItemInfo> GetFileList(string fullPath) { label2.Text = Utils.LanguageSupport.Instance.GetTranslation(STR_RETRIEVINGLIST); label2.Refresh(); List<FTPDirectoryItemInfo> result = new List<FTPDirectoryItemInfo>(); this.Cursor = Cursors.WaitCursor; try { FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(fullPath)); ftpRequest.Credentials = new NetworkCredential("anonymous", "*****@*****.**"); ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails; ftpRequest.KeepAlive = false; string line; using (FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse()) using (StreamReader sr = new StreamReader(ftpResponse.GetResponseStream())) { while (!sr.EndOfStream) { line = sr.ReadLine(); FTPDirectoryItemInfo di = new FTPDirectoryItemInfo(); di.Folder = line.StartsWith("d"); di.Name = line.Substring(line.LastIndexOf(' ')+1); di.Size = 0; string[] parts = line.Split(new char[] {' ', '\t' }, StringSplitOptions.RemoveEmptyEntries); try { di.Size = long.Parse(parts[4]); } catch { } if (di.Name != "0.2.4-archive") { result.Add(di); } } } } catch { result = null; } this.Cursor = Cursors.Default; label2.Text = ""; return result; }