By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,289 Members | 3,049 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,289 IT Pros & Developers. It's quick & easy.

Transferring files to another machine on a local network

P: n/a
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?

Mar 15 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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.
Mar 16 '07 #2

P: n/a
On Mar 15, 5:43 pm, "Ben Voigt" <r...@nospam.nospamwrote:
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.

Mar 16 '07 #3

P: n/a
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

"Ben Voigt" <rb*@nospam.nospamwrote in message
news:e9**************@TK2MSFTNGP03.phx.gbl...
>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.

Mar 16 '07 #4

P: n/a
"Lunchtimemama" <lu***********@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
>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.

Mar 16 '07 #5

P: n/a
On Mar 16, 5:13 am, "Willy Denoyette [MVP]"
<willy.denoye...@telenet.bewrote:
"Lunchtimemama" <lunchtimem...@gmail.comwrote in message

news:11**********************@l77g2000hsb.googlegr oups.com...
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.
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?

Mar 16 '07 #6

P: n/a
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.

Mar 16 '07 #7

P: n/a
On Mar 15, 5:21 pm, "Lunchtimemama" <lunchtimem...@gmail.comwrote:
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.

Mar 18 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.