Murhaf Sousli
Murhaf Sousli

Reputation: 13296

how do i send folder with its contents (subfolder/files) to server-client side?

I'm making a file transfer (server-client) application. im using TCP. (.net 4.0)

How do I send a Folder with all its contents(folder/files) to the other side ??

I have these methods that works fine:

  • Send(string srcPath, string destPath)  //sends a single file
    

  • Recieve(string destPath) //recieves a single file
    

    This is the Send method:

    public void Send(string srcPath, string destPath)
    {
        using (fs = new FileStream(srcPath, FileMode.Open, FileAccess.Read))
        {
            try
            {
                fileSize = fs.Length;
                while (sum < fileSize)
                {
                    if (fileSize - sum < packetSize)
                    {
                        count = fs.Read(data, 0, (int)(fileSize - sum));
                        network.Write(data, 0, (int)(fileSize - sum));
                    }
                    else
                    {
                        count = fs.Read(data, 0, data.Length);
                        network.Write(data, 0, data.Length);
                    }
                    fs.Seek(sum, SeekOrigin.Begin);
                    sum += count;
                }
                network.Flush();
            }
            finally
            {
                fs.Dispose();
            }
        }
    }
    

    and this's the Recieve Method :

        public void Recieve(string destPath)
        {
            using (fs = new FileStream(destPath, FileMode.Create, FileAccess.Write))
            {
                try
                {
                    while (sum < fileSize)
                    {
                        if (fileSize - sum < packetSize)
                        {
                            count = network.Read(data, 0, (int)(fileSize - sum));
                            fs.Write(data, 0, (int)(fileSize - sum));
                        }
                        else
                        {
                            count = network.Read(data, 0, data.Length);
                            fs.Write(data, 0, data.Length);
                        }
                        fs.Seek(sum, SeekOrigin.Begin);
                        sum += count;
                    }
                }
                finally
                {
                    fs.Dispose();
                }
            }
        }
    

    This is a common variables :

        const int packetSize = 1024 * 8;    //Packet Size.
        long sum;                           //Sum of sent data.
        long fileSize;                      //File Size.
        int count = 0;                      //data counter.
        static byte[] data = null;          //data buffer.
    
        NetworkStream network; 
        FileStream fs;
    

    I also got:

    bool IsFolder(string path) //that returns if the path is a folder or a file
    

    Upvotes: 1

    Views: 2791

  • Answers (2)

    Murhaf Sousli
    Murhaf Sousli

    Reputation: 13296

    I've figured out the answer

    public void SendFolder(string srcPath, string destPath)
        {
            string dirName = Path.Combine(destPath, Path.GetFileName(srcPath));
            CreateDirectory(dirName);  // consider that this method creates a directory at the server
            string[] fileEntries = Directory.GetFiles(srcPath);
            string[] subDirEntries = Directory.GetDirectories(srcPath);
            foreach (string filePath in fileEntries)
            {
                Send(srcPath, dirName);
            }
            foreach (string dirPath in subDirEntries)
            {
                SendFolder(dirPath, dirName);
            }
        }
    

    the server will first create a direcotory .. named the same name of the directory from the client side .. which is dirName

    then it will send all the files in that folder .. then it will recursively do the same for each folder .. problem solved

    Upvotes: 1

    2GDev
    2GDev

    Reputation: 2466

    With a Directory Path can do this :

        public void SendAll(string DirectoryPath)
        {
    
            if (Directory.Exists(DirectoryPath))
                {
                    string[] fileEntries = Directory.GetFiles(DirectoryPath);
                    string[] subdirEntries = Directory.GetDirectories(DirectoryPath);
    
                    foreach (string fileName in fileEntries)
                    {
                        Send(fileName);
    
    
        }
    
                foreach (string dirName in subdirEntries)
                {
                    SendAll(dirName);
                }
            }
    

    }

    Upvotes: 1

    Related Questions