Beispiel #1
0
        public bool Read(string path)
        {
            // Read the torrent file into the program
            // Create a list to store each file part
            List <string> fileParts = new List <string>();

            // Set the torrent file path to the incoming path variable
            torrentFilePath = path;

            try
            {
                // Read the file and add each line to the list
                using (var fileStream = File.OpenRead(path))
                {
                    using (var streamReader = new StreamReader(fileStream, Encoding.UTF8, true))
                    {
                        String line;
                        while ((line = streamReader.ReadLine()) != null)
                        {
                            fileParts.Add(line);
                        }
                    }
                }

                // Parse each line of the file into the appropriate variables
                fileName = fileParts[0];

                // Convert hash of file into a bytes
                hash       = fileParts[1].Split('-').Select(b => Convert.ToByte(b, 16)).ToArray();
                hashString = BitConverter.ToString(hash);
                fileSize   = long.Parse(fileParts[2]);

                // Convert the file size to a readable format
                fileSizeUI = Utility.SetSizeUI(fileSize);
                pieces     = int.Parse(fileParts[3]);
                pieceSize  = int.Parse(fileParts[4]);
                tracker    = fileParts[5];

                // Create directories for the torrent file and content
                CreateFileDirectory(path);

                // Create an array of each hash entry
                hashArray = SpliceText(fileParts[6], 40);

                // Clear the list
                fileParts.Clear();

                // Add this file to the UI
                uiUpdate.AddToDataGrid(this);

                return(true);
            }

            catch (Exception e)
            {
                MessageBox.Show("The file selected was not a recognised torrent file");
                return(false);
            }
        }