Welcome Guest! To access all forums & features, please register an account or sign-in. → Why register?



[C#] nubby math question


3 replies to this topic - - - - -

#1 Xerax

    Flashbomb

  • 1,593 posts
  • Joined: 04-December 10
  • Location: London, UK
  • OS: Windows 7601, Windows 8102

Posted 28 March 2012 - 23:50

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.


#2 +Asik

  • 5,970 posts
  • Joined: 26-October 05

Posted 29 March 2012 - 01:10

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?

#3 OP Xerax

    Flashbomb

  • 1,593 posts
  • Joined: 04-December 10
  • Location: London, UK
  • OS: Windows 7601, Windows 8102

Posted 29 March 2012 - 01:30

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.

#4 +Asik

  • 5,970 posts
  • Joined: 26-October 05

Posted 29 March 2012 - 01:32

You can't. If you want to output progress you'll have to use custom code. Glad you could sort it out!