-5

该程序旨在设置文件路径,其想法是设置数据时,应使用此功能:

    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();
    }
4

1 回答 1

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();
    }

再次感谢所有回复我帖子的人!

于 2013-05-05T08:37:21.030 回答