【问题标题】:FileStream throws File not foundFileStream 抛出找不到文件
【发布时间】:2013-04-28 22:56:15
【问题描述】:

程序是用来设置文件路径的,思路是在设置数据的时候,应该使用这个函数:

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

【问题讨论】:

    标签: c# filestream sendfile


    【解决方案1】:

    好的,我发现了问题:我的服务器端程序与我的客户端程序相互干扰。这是我的客户端程序的 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();
        }
    

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

    【讨论】:

    • @MatthewWatson 它说必须等到明天才能接受我自己的答案......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 2016-05-14
    • 2015-03-19
    • 2021-11-25
    • 2016-09-13
    • 2020-01-19
    • 2023-04-06
    相关资源
    最近更新 更多