• 0

[C#] nubby math question


Question

I'm creating a dump from an external drive. I have the code for reading from the drive written, but I need to work out the best and fastest way to read from the drive. The code I have at the moment is:


public void beginBackup()
	    {
		    Reader r = _fatxDrive.Reader();
		    if (System.IO.File.Exists(_fileName))
			    System.IO.File.Delete(_fileName);
		    Writer w = new Writer(new System.IO.FileStream(_fileName, System.IO.FileMode.Create));
		    long ReadLength = 0x100000;
		    Dispatcher.Invoke(new Action(delegate { pbBackup.Maximum = r.BaseStream.Length; }));
		    while(r.BaseStream.Position != r.BaseStream.Length)
		    {
			    if (_cancelOp)
				    break;
			    if (r.BaseStream.Position + ReadLength > r.BaseStream.Length)
				    ReadLength = r.BaseStream.Length - r.BaseStream.Position;
			    w.Write(r.ReadBytes(Convert.ToInt32(ReadLength)));
		    }
		    w.Close();
		    Dispatcher.Invoke(new Action(delegate
		    {
			    FormFadeOut.Begin();
			    Backend.SettingsVault.Home.TaskbarItemInfo.ProgressState = System.Windows.Shell.TaskbarItemProgressState.None;
		    }));

But this tells me I'm exceeding the length of the drive.

Link to comment
https://www.neowin.net/forum/topic/1066796-c-nubby-math-question/
Share on other sites

3 answers to this question

Recommended Posts

  • 0

The fastest most simple way is to create your input and output streams, and then call

input.CopyTo(output);

Only available in .NET 4.0+, though. I'm not sure why your code would exceed the length of the drive, only thing that's strange is ReadLength constant, where does that value come from? How do you specify that you want to read from the very start of the drive?

  • 0

ReadLength is a buffer I'm using. And I was forgetting to set the Basestream.Position to 0 before the read. Although the class I'm using did that for me. After tweaking my code a bit I managed to get it working. But, how would I set up CopyTo to be able to output progress? If I can't, or can't easily. I'll just stick to this method.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Nice devices but my Surface Pro 9 got bricked from a Windows Update efter only three years. Never buying a device from MS again.
    • Icaros Shell Extensions 3.3.6 by Razvan Serea Icaros is a collection of lightweight, high quality, Windows Shell Extensions. Icaros can provide Windows Explorer thumbnails, for essentially any video media format supported by FFmpeg, this includes popular filetypes such as: Mkv, Flv, Avi, Mp4, Mov, Rmvb, M2ts, Ogm etc. Besides the FFmpeg supported filetypes, Icaros also contains custom parsers, which can produce Cover Art thumbnails for Mkv, Flac, Ape, Mpc and several other filetypes. Icaros also provides Windows Explorer properties for the following popular filetypes: Mkv, Mk3d, Mp4, Mov, Webm, Ogm, Ogg, Flv, Avi, Rm, Rmvb, Flac, Opus, Spx, Ape, Mp3, Mpc, Mka, Tak, Tta, Ofr, Wav and more. Properties refer to the info shown for each file in Explorer, such as length, width, height, title, channels and so on. Icaros works on Vista, Windows 7, Windows 8/8.1, Windows 10 and Windows 11. Icaros Shell Extensions 3.3.6 changelog: Added support for Animated WEBP thumbnailing Added support for UTF-8 filenames in CBX files (Comic book files) Added support for rotation of MKV thumbnails Added support for "Sort title" property in MKV files Fixed some wav files not showing properties Fixed some old h264 files producing green thumbnails Fixed default level of MKV tags (now set to 50) Updated FFmpeg Download: Icaros Shell Extensions 3.3.6 | 12.8 MB (Freeware) Download: Portable Icaros 3.3.6 View: Icaros Shell Extension Home Page | Screenshots 1 | 2 Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Week One Done
      Huge Trailer earned a badge
      Week One Done
    • Week One Done
      Classifyskilleducation earned a badge
      Week One Done
    • One Month Later
      eurospharma62 earned a badge
      One Month Later
    • Week One Done
      With What earned a badge
      Week One Done
    • Week One Done
      Harris Gilbert earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      536
    2. 2
      +Edouard
      168
    3. 3
      PsYcHoKiLLa
      71
    4. 4
      neufuse
      64
    5. 5
      ATLien_0
      63
  • Tell a friend

    Love Neowin? Tell a friend!