Search code examples
c#mediafire

How to upload multiple Files in c# using MediaFireApi


I am currently running code in my c# program to upload FidderCore Captures to MediaFire for remote storage. I have several questions:

  1. How can I Set the location to where the file is located; as the default location is where the application is running at vs where the default fiddlercore captures are than located.

2.) Since the file name changes; how can I upload these files that'll either obtain it's name first or except the files without knowing the names. Please note; the function does work if I'm setting the file names to be static and in a designated location; from wherever the current application is located. Here is my current code:

        public async void UPLOADER()
        {
            var listfiles = Environment.SpecialFolder.MyDocuments;
            var moveinto = Path.Combine(listfiles + "FiddlerCore" + "Captures");
            using var client = new Client(new ClientSettings());
            // Login
            await client.Login("Email", "PASSWORD");

            // Get user info
            //var userInfo = await client.UserGetInfo();
            // Get folder content
            //var folderContent = await client.FolderGetContent(Client.RootFolderKey, contentType: FolderContentType.Folders);
            // Create a folder
            //var newFolderKey = await client.FolderCreate(Client.RootFolderKey, name: "test");
            // Delete a folder
            //var folderDelete = await client.FolderDelete(new[] { newFolderKey });
            // Purge a folder
            //await client.FolderPurge(new[] { newFolderKey });
            // Get file info
            //var info = (await client.FileGetInfo(filePath: "/Documents/test.mp3"))?.ToList();
            // Get file direct download link
            //var links = await client.DownloadDirectLink(new[] { info[0].QuickKey });
            //var url = links.First().DirectDownload;
            // Upload a file
            using (var fs = new FileStream("test.mp3", FileMode.Open, FileAccess.Read))
            {
                //await client.UploadSimple(fs, "audio/mpeg", "test.mp3", fs.Length, path: "/Music");
            }

            // Logout
            await client.Logout();
        }

I've tried setting a var to that location; and then calling that location. When doing so; the application crashes. I am needing help.


Solution

  • public async void UPLOADER()
    {
        var listfiles = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        var moveinto = Path.Combine(listfiles, "FiddlerCore", "Captures");
    
        Directory.SetCurrentDirectory(moveinto);
    
        //GetFiles on DirectoryInfo returns a FileInfo object.
        var pdfFiles = new DirectoryInfo(moveinto).GetFiles("*.htm");
    
        //FileInfo has a Name property that only contains the filename part.
        var name = pdfFiles[0].Name;
    
        using var client = new Client(new ClientSettings());
        
        // Login
        await client.Login("Email", "Password");
    
        // Get user info
        //var userInfo = await client.UserGetInfo();
        // Get folder content
        //var folderContent = await client.FolderGetContent(Client.RootFolderKey, contentType: FolderContentType.Folders);
        // Create a folder
        //var newFolderKey = await client.FolderCreate(Client.RootFolderKey, name: "test");
        // Delete a folder
        //var folderDelete = await client.FolderDelete(new[] { newFolderKey });
        // Purge a folder
        //await client.FolderPurge(new[] { newFolderKey });
        // Get file info
        //var info = (await client.FileGetInfo(filePath: "/Documents/test.mp3"))?.ToList();
        // Get file direct download link
        //var links = await client.DownloadDirectLink(new[] { info[0].QuickKey });
        //var url = links.First().DirectDownload;
    
        // Upload a file
        using var fs = new FileStream(name, FileMode.Open, FileAccess.Read);
        
        await client.UploadSimple(fs, "audio/mpeg", name, fs.Length, path: "/Documents");
        
        // Logout
        await client.Logout();
    }