该计划旨在设置文件路径和想法是,当数据被设置,就应该使用这个功能:的FileStream抛出找不到文件
public void SendFile(String fileName, long fileSize, NetworkStream io)
{
SendFileNameToServer();
SendFileSizeToServer();
byte[] fileData;
try
{
if (!File.Exists(fileName))
{
throw new FileNotFoundException("File does not exist!");
}
FileStream openFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader bReader = new BinaryReader(openFileStream);
Int32 remainingSize = Convert.ToInt32(_fileSize);
do
{
fileData = bReader.ReadBytes(BUFSIZE);
io.Write(fileData, 0, BUFSIZE);
remainingSize -= BUFSIZE;
} while (remainingSize > BUFSIZE);
do
{
fileData = bReader.ReadBytes(remainingSize);
io.Write(fileData, 0, remainingSize);
remainingSize -= remainingSize;
} while (remainingSize > 0);
bReader.Close();
openFileStream.Flush();
openFileStream.Close();
io.Flush();
io.Close();
}
catch (Exception)
{
throw new Exception();
}
}
发送的文件路径指定的文件到接收文件数据的服务器端程序。
问题是,当它到达行FileStream openFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
它告诉我该文件没有找到。它给出的例外是Exception:Thrown: "The process cannot access the file 'D:\StepMania\Songs\Fragma\You Are Alive\Green.avi' because it is being used by another process." (System.IO.IOException) A System.IO.IOException was thrown: "The process cannot access the file 'D:\*FilePath*\Green.avi' because it is being used by another process." Time: 04-05-2013 21:11:39 Thread:Main Thread[5532]
,但我想不到在StepMania没有运行时使用此文件的任何进程。 我知道该文件在那里,因为我检查了文件路径,它在那里,因为它应该是。它工作得很好,如果该文件是在程序完全相同的文件夹,但除此之外,我似乎无法找到解决这个问题。有没有人有任何想法可能是错的?
如果您需要更多我的代码,请告诉我。
编辑: 我的服务器使用此代码接收文件:
public void ReceiveFile(String fileName, NetworkStream io)
{
// TO DO Din egen kode
byte[] fileData = new byte[BUFSIZE];
FileStream writeFileStream = new FileStream(fileName, FileMode.Create);
BinaryWriter bWrite = new BinaryWriter(writeFileStream);
int bytesRead = 0;
long remainingSize = Convert.ToInt32(_fileSize);
do
{
Console.WriteLine("Remaining number of bytes: {0}", remainingSize);
bytesRead = io.Read(fileData, 0, BUFSIZE); // Read max 1000 bytes from server via socket (actual value is placed in "bytesRead"
bWrite.Write(fileData, 0, bytesRead); // write the received bytes into file. the number of received bytes is placed in "bytesRead"
remainingSize -= bytesRead;
}
while (remainingSize > 0);
writeFileStream.Flush();
writeFileStream.Close();
bWrite.Close();
}
不要忘记将此答案标记为“已接受”。 – 2013-05-05 08:49:42
@MatthewWatson它说,必须等到明天才能接受我自己的答案...... – user2350376 2013-05-05 14:05:35