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