Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old March 15th, 2007, 11:35 PM
Lunchtimemama
Guest
 
Posts: n/a
Default Transferring files to another machine on a local network

I need to move a jpeg file to another machine on a local network (for
which I have full permissions). The following works:

*****************
string inputPath = @"C:/test.jpg";
string outputPath = @"\\OTHERCOMPUTER\test.jpg";

FileStream fin = new FileInfo(inputPath, FileMode.Open);
FileStream fout = File.Create(outputPath);

BinaryReader br = new BinaryReader(fin);
BinaryWriter bw = new BinaryWriter(fout);

long length = fin.Length; // cache this value
int size = 1048576; // 1 megabyte
byte[] bytes = new byte[size];
long position = 0;

while (position < length)
{
bytes = br.ReadBytes(size);
bw.Write(bytes);
position += size;
}

br.Close();
bw.Close();

fin.Close();
fout.Close();

new FileInfo(inputPath).Delete(); // get rid of old file
*****************

It works, but it is INCREDIBLY slow. It takes something like 200
seconds to transfer a 1MB photo (ANTS profiler says
Win32Native.WriteFile(SafeFileHandle, byte*, int, out int, IntPtr) is
the holdup). When I copy-paste these files in the Windows Explorer,
they transfer in under a second, so I thought perhaps the kernel was
being smart about the SMB networking, so I tried this:

*****************
[DllImport("kernel32.dll")]
static extern bool MoveFile(string lpExistingFileName, string
lpNewFileName);

private void Move(string inputPath, string outputPath)
{
MoveFile(inputPath, outputPath);
}
*****************

But this was just as slow. Apparently, Windows Explorer is smart about
browsing remote folders and will use some SMB-or-other API function
when copy/pasting files. Does anyone know what that API call is, and
how I ought to wrap it?

  #2  
Old March 16th, 2007, 04:45 AM
Ben Voigt
Guest
 
Posts: n/a
Default Re: Transferring files to another machine on a local network

But this was just as slow. Apparently, Windows Explorer is smart about
Quote:
browsing remote folders and will use some SMB-or-other API function
when copy/pasting files. Does anyone know what that API call is, and
how I ought to wrap it?
Are you sure Explorer has finished copying the file? If you disconnected
the network, you might find that the copy didn't finish so fast after all.


  #3  
Old March 16th, 2007, 04:45 AM
Lunchtimemama
Guest
 
Posts: n/a
Default Re: Transferring files to another machine on a local network

On Mar 15, 5:43 pm, "Ben Voigt" <r...@nospam.nospamwrote:
Quote:
Quote:
But this was just as slow. Apparently, Windows Explorer is smart about
browsing remote folders and will use some SMB-or-other API function
when copy/pasting files. Does anyone know what that API call is, and
how I ought to wrap it?
>
Are you sure Explorer has finished copying the file? If you disconnected
the network, you might find that the copy didn't finish so fast after all.
Yes, it really does transfer in a second or two.

  #4  
Old March 16th, 2007, 04:45 AM
VJ
Guest
 
Posts: n/a
Default Re: Transferring files to another machine on a local network

Oh explorer takes progress out before finishing copying.. that is scary
Ok I know you did not mean that.. but I got it like that, and jumped a
little...

Although I have seen explorer do it for USB locations.. and corrupted a
drive of mine...

VJ

"Ben Voigt" <rbv@nospam.nospamwrote in message
news:e9cYKM1ZHHA.3928@TK2MSFTNGP03.phx.gbl...
Quote:
Quote:
>But this was just as slow. Apparently, Windows Explorer is smart about
>browsing remote folders and will use some SMB-or-other API function
>when copy/pasting files. Does anyone know what that API call is, and
>how I ought to wrap it?
>
Are you sure Explorer has finished copying the file? If you disconnected
the network, you might find that the copy didn't finish so fast after all.
>

  #5  
Old March 16th, 2007, 01:05 PM
Willy Denoyette [MVP]
Guest
 
Posts: n/a
Default Re: Transferring files to another machine on a local network

"Lunchtimemama" <lunchtimemama@gmail.comwrote in message
news:1173997277.025830.155950@l77g2000hsb.googlegr oups.com...
Quote:
>I need to move a jpeg file to another machine on a local network (for
which I have full permissions). The following works:
>
*****************
string inputPath = @"C:/test.jpg";
string outputPath = @"\\OTHERCOMPUTER\test.jpg";
>
FileStream fin = new FileInfo(inputPath, FileMode.Open);
FileStream fout = File.Create(outputPath);
>
BinaryReader br = new BinaryReader(fin);
BinaryWriter bw = new BinaryWriter(fout);
>
long length = fin.Length; // cache this value
int size = 1048576; // 1 megabyte
byte[] bytes = new byte[size];
long position = 0;
>
while (position < length)
{
bytes = br.ReadBytes(size);
bw.Write(bytes);
position += size;
}
>
br.Close();
bw.Close();
>
fin.Close();
fout.Close();
>
new FileInfo(inputPath).Delete(); // get rid of old file
*****************
>
It works, but it is INCREDIBLY slow. It takes something like 200
seconds to transfer a 1MB photo (ANTS profiler says
Win32Native.WriteFile(SafeFileHandle, byte*, int, out int, IntPtr) is
the holdup). When I copy-paste these files in the Windows Explorer,
they transfer in under a second, so I thought perhaps the kernel was
being smart about the SMB networking, so I tried this:
>
*****************
[DllImport("kernel32.dll")]
static extern bool MoveFile(string lpExistingFileName, string
lpNewFileName);
>
private void Move(string inputPath, string outputPath)
{
MoveFile(inputPath, outputPath);
}
*****************
>
But this was just as slow. Apparently, Windows Explorer is smart about
browsing remote folders and will use some SMB-or-other API function
when copy/pasting files. Does anyone know what that API call is, and
how I ought to wrap it?
>


There is no tricky API used by Explorer. Didn't look at your code, but there must be
something wrong with it.
Using following takes less than 10 seconds to copy a 30MB file to a network share over a
100Mbit ethernet link

....
string outPath = @"\\othercomputer\test.jpg";
string inPath = @"c:\test.jpg";
{
using (FileStream fso = File.OpenWrite(outPath))
using (FileStream fsi = File.OpenRead(inPath))
{
byte[] b = new byte[4096];
int offset = 0;
int bytesRead = 0;
while ((bytesRead = fsi.Read(b, offset, b.Length)) 0)
{
fso.Write(b, offset, bytesRead);
}
}
}


Willy.

  #6  
Old March 16th, 2007, 11:55 PM
Lunchtimemama
Guest
 
Posts: n/a
Default Re: Transferring files to another machine on a local network

On Mar 16, 5:13 am, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.bewrote:
Quote:
"Lunchtimemama" <lunchtimem...@gmail.comwrote in message
>
news:1173997277.025830.155950@l77g2000hsb.googlegr oups.com...
>
>
>
Quote:
I need to move a jpeg file to another machine on a local network (for
which I have full permissions). The following works:
>
Quote:
*****************
string inputPath = @"C:/test.jpg";
string outputPath = @"\\OTHERCOMPUTER\test.jpg";
>
Quote:
FileStream fin = new FileInfo(inputPath, FileMode.Open);
FileStream fout = File.Create(outputPath);
>
Quote:
BinaryReader br = new BinaryReader(fin);
BinaryWriter bw = new BinaryWriter(fout);
>
Quote:
long length = fin.Length; // cache this value
int size = 1048576; // 1 megabyte
byte[] bytes = new byte[size];
long position = 0;
>
Quote:
while (position < length)
{
bytes = br.ReadBytes(size);
bw.Write(bytes);
position += size;
}
>
Quote:
br.Close();
bw.Close();
>
Quote:
fin.Close();
fout.Close();
>
Quote:
new FileInfo(inputPath).Delete(); // get rid of old file
*****************
>
Quote:
It works, but it is INCREDIBLY slow. It takes something like 200
seconds to transfer a 1MB photo (ANTS profiler says
Win32Native.WriteFile(SafeFileHandle, byte*, int, out int, IntPtr) is
the holdup). When I copy-paste these files in the Windows Explorer,
they transfer in under a second, so I thought perhaps the kernel was
being smart about the SMB networking, so I tried this:
>
Quote:
*****************
[DllImport("kernel32.dll")]
static extern bool MoveFile(string lpExistingFileName, string
lpNewFileName);
>
Quote:
private void Move(string inputPath, string outputPath)
{
MoveFile(inputPath, outputPath);
}
*****************
>
Quote:
But this was just as slow. Apparently, Windows Explorer is smart about
browsing remote folders and will use some SMB-or-other API function
when copy/pasting files. Does anyone know what that API call is, and
how I ought to wrap it?
>
There is no tricky API used by Explorer. Didn't look at your code, but there must be
something wrong with it.
Using following takes less than 10 seconds to copy a 30MB file to a network share over a
100Mbit ethernet link
>
...
string outPath = @"\\othercomputer\test.jpg";
string inPath = @"c:\test.jpg";
{
using (FileStream fso = File.OpenWrite(outPath))
using (FileStream fsi = File.OpenRead(inPath))
{
byte[] b = new byte[4096];
int offset = 0;
int bytesRead = 0;
while ((bytesRead = fsi.Read(b, offset, b.Length)) 0)
{
fso.Write(b, offset, bytesRead);
}
}
}
>
Willy.
Using that exact code, it still takes forever. Perhaps it's something
with my network. Is there any reason Win32Native.WriteFile might get
hung up?

  #7  
Old March 17th, 2007, 12:35 AM
JS
Guest
 
Posts: n/a
Default Re: Transferring files to another machine on a local network

I have an application where I'm doing a similar file copy (never timed
it but it did not seem slow). I had a problem which was a bit
different. The other computer required a different username/password,
so the copy would not work unless I 'seeded' Windows by first opening
an explorer window to the other computer, then entering the username
and password. After this, Windows knew I was trusted I guess. Still,
I'd like to be able to somehow do the file copy in code without having
this prerequisite. Does anyone know how to do a file copy to a share
on a different computer where the other computer has a different
username/password?

Thanks.

  #8  
Old March 18th, 2007, 08:45 AM
Lunchtimemama
Guest
 
Posts: n/a
Default Re: Transferring files to another machine on a local network

On Mar 15, 5:21 pm, "Lunchtimemama" <lunchtimem...@gmail.comwrote:
Quote:
I need to move a jpeg file to another machine on a local network (for
which I have full permissions). The following works:
>
*****************
string inputPath = @"C:/test.jpg";
string outputPath = @"\\OTHERCOMPUTER\test.jpg";
>
FileStream fin = new FileInfo(inputPath, FileMode.Open);
FileStream fout = File.Create(outputPath);
>
BinaryReader br = new BinaryReader(fin);
BinaryWriter bw = new BinaryWriter(fout);
>
long length = fin.Length; // cache this value
int size = 1048576; // 1 megabyte
byte[] bytes = new byte[size];
long position = 0;
>
while (position < length)
{
bytes = br.ReadBytes(size);
bw.Write(bytes);
position += size;
>
}
>
br.Close();
bw.Close();
>
fin.Close();
fout.Close();
>
new FileInfo(inputPath).Delete(); // get rid of old file
*****************
>
It works, but it is INCREDIBLY slow. It takes something like 200
seconds to transfer a 1MB photo (ANTS profiler says
Win32Native.WriteFile(SafeFileHandle, byte*, int, out int, IntPtr) is
the holdup). When I copy-paste these files in the Windows Explorer,
they transfer in under a second, so I thought perhaps the kernel was
being smart about the SMB networking, so I tried this:
>
*****************
[DllImport("kernel32.dll")]
static extern bool MoveFile(string lpExistingFileName, string
lpNewFileName);
>
private void Move(string inputPath, string outputPath)
{
MoveFile(inputPath, outputPath);}
>
*****************
>
But this was just as slow. Apparently, Windows Explorer is smart about
browsing remote folders and will use some SMB-or-other API function
when copy/pasting files. Does anyone know what that API call is, and
how I ought to wrap it?
I'm on a wireless network, if that makes a difference.

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,248 network members.