该程序旨在设置文件路径,并且想法是在设置数据时,它应该使用此函数:
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();
}
答案 0 :(得分:0)
好的,我发现了问题:我的服务器端程序与我的客户端程序有关。这是我的客户端程序的SendFile代码的固定代码:
public void SendFile(String fileName, long fileSize, NetworkStream io)
{
SendFileNameToServer();
SendFileSizeToServer();
byte[] fileData;
try
{
FileStream openFileStream = File.OpenRead(fileName);
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);
openFileStream.Flush();
bReader.Close();
openFileStream.Close();
io.Flush();
io.Close();
}
catch (Exception)
{
throw new Exception();
}
}
这是我服务器的ReceiveFile代码:
public void ReceiveFile(String fileName, NetworkStream io)
{
// TO DO Din egen kode
byte[] fileData = new byte[BUFSIZE];
FileStream writeFileStream = new FileStream(LIB.extractFileName(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();
}
再次感谢所有回复我帖子的人!