473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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?

Mar 15 '07 #1
7 10217
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
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John | last post by:
Hello, I need to know which programs are opn on another machine in my network, these programs are opened from a shared drive on the server. How can I do this ? I've already dowloaded a sample...
6
by: Job Lot | last post by:
Using VB.NET how can I find all available SQL Server on a local network and list all the databases in each server. Thanks
29
by: Tola | last post by:
In my case of study, my teacher gave me a project, after I analysed the problem I found that I had to used open the file on the other machine? Please help? Thank you in advance. Tola CHROUK
8
by: GeekBoy | last post by:
I understand the benefit of pushing the StateServer process onto another computer to "balance" the load and take some cpu and memory usage off the web server, but how much could it possibly help?...
27
by: Javier Martinez | last post by:
Hi I have asp application in a machine with a virtual directory referring a shared directory in another machine When I try to load any aspx page of my portal I get the following error: ...
1
by: gudmewarshivnath | last post by:
hi I need to access the files & folders from another machine which is in the network then how can i impliment it in vb 6 Is there any API Plz someone help me Shivnath
0
by: sang | last post by:
Hi i want to connect the another machine in mysql which is located in the network. by using this i want to access the another machine database with my system. The accessed machine is...
0
by: umeshpotdar | last post by:
hi friends i have created asp.net project i want to execute it on another machine which is connected to my machine.(i.e in LAN) so how can i execute this ? can you tell me?
22
by: hamarsheh | last post by:
please i need you'r help .. we are designing a web site and we need a critical code in php for security , we have to read users permissions on files in the local network ,to give them the real...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.