(Ping Zheng)
1/15/2003 9:41:00 AM
Hi,
My understanding of this problem is: You want to read a file from other
comptuer. In fact, you can use the classes of file operation provided by
.NET directly, they are more simple in this situation. Because you are
needed to organize the data package as the file by yourself if you used the
TCP/IP classes.
I listed the classes for your reference:
System.IO.File
System.IO.FileStream: it is used to open the file
System.IO.BinaryReader: it is used to read the file content
System.IO.BinaryWriter: it is used to write the file
In addition, I listed the code snip for your reference:
..
bool bRetVal = false;
if(File.Exists(srcfile) && Directory.Exists(destdir))
{
FileStream fs = new FileStream(srcfile, FileMode.Open);
BinaryReader r = new BinaryReader(fs);
//construct the destination file name
string destFile = null;
destFile = destdir;
destFile +="\\";
destFile += srcfile;
//create the file stream
//FileMode.Create means always create the file
FileStream fs1 = new FileStream(destFile, FileMode.Create);
BinaryWriter w = new BinaryWriter(fs1);
r.BaseStream.Seek(0,SeekOrigin.Begin); //set the file pointer to the
beginning
long filelen = r.BaseStream.Length;
long readlen = 0;
byte[] content = null;
while(readlen < filelen)
{
int tempcount = (int)(filelen-readlen);
if(tempcount > 100)
{
tempcount = 100;
}
content = r.ReadBytes(tempcount);
w.Write(content);
readlen += tempcount;
}
//close the read file handle
r.Close();
fs.Close();
//close the write file handle
w.Close();
fs1.Close();
bRetVal = true;
}
else
bRetVal = false;
return bRetVal;
}
}
}
This posting is provided "AS IS" with no warranties, and confers no rights.
George Zheng [MSFT]