//private void lvFiles_SelectedIndexChanged(object sender, EventArgs e)
        //{

        //}

        private void FileList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (peer.ServiceProxy != null)
            {
                if (FileList.SelectedItems.Count == 0)
                {
                    MessageBox.Show("You must select a file to download");
                }
                else
                {
                    ListViewItem item = FileList.SelectedItems[0];

                    // Strip off 'Root' from the full path
                    string path = item.SubItems[1].Text;

                    // Ask where it should be saved
                    SaveFileDialog dlg = new SaveFileDialog()
                    {
                        RestoreDirectory = true,
                        OverwritePrompt  = true,
                        Title            = "Save as...",
                        FileName         = Path.GetFileName(path)
                    };

                    dlg.ShowDialog(this);

                    if (!string.IsNullOrEmpty(dlg.FileName))
                    {
                        // Get the file from the server
                        using (FileStream output = new FileStream(dlg.FileName, FileMode.Create))
                        {
                            Stream downloadStream;

                            //using (FileRepositoryServiceClient client = new FileRepositoryServiceClient())
                            //{
                            //    downloadStream = client.GetFile(path);
                            //}
                            DownloadRequest dr = new DownloadRequest();
                            dr.FileName = path;
                            RemoteFileInfo rfi = peer.ServiceProxy.DownloadFile(dr);
                            downloadStream = rfi.FileByteStream;
                            downloadStream.CopyTo(output);
                        }

                        // Process.Start(dlg.FileName);
                    }
                }
            }
        }
Esempio n. 2
0
        public RemoteFileInfo DownloadFile(DownloadRequest request)
        {
            var filePath = request.FileName;
            var fileInfo = new FileInfo(filePath);

            if (fileInfo.Exists == false)
            {
                throw new FileNotFoundException("File not found", request.FileName);
            }

            var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

            var result = new RemoteFileInfo
            {
                FileName       = request.FileName,
                Length         = fileInfo.Length,
                FileByteStream = stream
            };

            return(result);
        }
Esempio n. 3
0
        private void send(string fileName)
        {
            //var client = new FileTransferClient.FileTransferServiceClient();
            //var client =
            int             peernum   = new Random().Next(0, Vault.Peers.Count - 1);
            PeerEntry       peerEntry = Vault.Peers.ElementAt(peernum);
            DownloadRequest dr        = new DownloadRequest();

            dr.FileName = fileName;

            RemoteFileInfo info        = peerEntry.ServiceProxy.DownloadFile(dr);
            Stream         inputStream = info.FileByteStream;

            //using (var writeStream = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write))
            //string outfile = Path.GetFileName(fileName);
            string outfile = Path.GetFileName(fileName);

            using (var writeStream = new FileStream(outfile, FileMode.CreateNew, FileAccess.Write))
            {
                const int bufferSize = 2048;
                var       buffer     = new byte[bufferSize];

                do
                {
                    int bytesRead = inputStream.Read(buffer, 0, bufferSize);
                    if (bytesRead == 0)
                    {
                        break;
                    }

                    writeStream.Write(buffer, 0, bytesRead);
                    //progressBar1.Value = (int)(writeStream.Position * 100 / length);
                }while (true);

                writeStream.Close();
            }

            inputStream.Dispose();
        }
Esempio n. 4
0
 public RemoteFileInfo EchoFile(DownloadRequest request)
 {
     //throw (new NotImplementedException());
     return(new RemoteFileInfo());
 }