Question

The program is meant to set the file path and the idea is that when the data is set, it should use this function:

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

to send the file given in the file path to a server-side program which receives the file data.

The problem is that when it gets to the line FileStream openFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read); it tells me that the file is not found. The exception it gives is 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] but I can't think of any process that would use this file when StepMania is not running. I know that the file is there because I check the file path and it's there as it should be. It works just fine if the file is in the exact same folder as the program but other than that, I can't seem to find the solution to this problem. Does anyone have any ideas as to what could be wrong?

If you need any more of my code, please tell me.

Edit: My server uses this code to receive the file:

    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();
    }
Was it helpful?

Solution

Ok, I found the problem: My server-side program interefered with my client-side program. Here's the fixed code for the SendFile code of my client program:

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

Here's the ReceiveFile code for my server:

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

Again, thank you everyone who answered my post!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top