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