Transferring files to another machine on a local network

L

Lunchtimemama

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?
 
B

Ben Voigt

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.
 
V

VJ

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
 
L

Lunchtimemama

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.
 
W

Willy Denoyette [MVP]

Lunchtimemama said:
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.
 
L

Lunchtimemama

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;
}


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.

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?
 
J

JS

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.
 
L

Lunchtimemama

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top