FileStream抛出文件未找到

本文关键字:文件 FileStream | 更新日期: 2023-09-27 18:12:03

该程序用于设置文件路径,其思想是当设置数据时,它应该使用以下函数:

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

FileStream抛出文件未找到

好的,我发现了问题:我的服务器端程序干扰了我的客户端程序。下面是我的客户端程序的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();
    }
再次感谢所有回复我帖子的人!