• 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

    • Why should simply opening the Action Center ... require any significant amount of CPU cycles? Too many are ignoring the elephant in the room: today's CPUs may have similar clock frequencies to CPUs from a decade ago (~4 to 5 GHz), but they run with far higher performance per clock, even on the smallest E-cores. The Action Center is a static, minor desktop element. It ought to open instantly on any CPU made in the past 2.5 decades at even low clocks. Opening a program? Boost away. Unzipping a file? Boost to your heart's content. Right-clicking the notification area? This should be an ultra-efficient, low-impact event that opens instantly. Compare today's UI elements to Windows 7 UI elements. Today's UI elements hardly do anything more substantial or important or critical, yet now require modern-day processors to jump half a GHz to reduce their latency.
    • This is not an option lmao: it is part of the KB5094126 update, pushed to all users. There is no GUI to configure it. Reading the article should be required for commenting.
    • Can you recommend me a good Pixel 7 on eBay? Because some are saying Gen 1, and I don't think that's what I want, right?
    • Close, but it is for any application starting, not just during boot time. In fact, it probably will not improve boot time at all because during a boot, the CPU is already boosting, so a pre-boost wouldn't change anything. I agree that it isn't exciting (especially considering every other OS already does it), but it is a good thing, even if they are just playing catch up.
    • Any so called performance increase will be in milliseconds, which nobody will actually notice in real world usage.
  • Recent Achievements

    • One Year In
      slackerzz earned a badge
      One Year In
    • One Year In
      highriskpaym earned a badge
      One Year In
    • One Month Later
      highriskpaym earned a badge
      One Month Later
    • Week One Done
      highriskpaym earned a badge
      Week One Done
    • Week One Done
      FBSPL earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      501
    2. 2
      PsYcHoKiLLa
      198
    3. 3
      +Edouard
      156
    4. 4
      Steven P.
      84
    5. 5
      ATLien_0
      71
  • Tell a friend

    Love Neowin? Tell a friend!