Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm kinda new to programming and trying to write a sample program and this is the line generating the error:

JavaScript
var bytes = File.ReadAllBytes(@"http://localhost/test.exe");


The error is Unhandled Exception: System.ArgumentException: URI formats are not supported.

A local path works, but this isn't what I'm trying to do. Need to fetch from http or ftp. Trying to keep it lean and clean - let me know what ideas you might have. Could someone please give me code to paste in to replace/fix this line so that it will compile?

I tried various things but can't make it work without using a local path.

What I have tried:

I tried using local path which does work, but prefer http or ftp.
Posted
Updated 12-Aug-23 2:56am
v3

That's because the File and Directory classes do not work with web URLs. It's local filesystem, or network shares, only.

You have to download the file from the http or ftp address to work with it using the File class methods. See the documentation on the HttpClient class[^] for how to use it to download the file.

For downloading from FTP, you can use the How to: Download files with FTP - .NET Framework | Microsoft Learn[^], but with .NET 6.0 and above, FTP is no longer supported.
 
Share this answer
 
In conjunction to what Dave posted, which is 100% correct, the 'File.ReadAllBytes' method does not directly support reading files from a URL. It's designed for reading files from local paths on your filesystem.

You need to use a different approach, involving a web request to download the file contents first. You can use the 'HttpClient' class to achieve this, the example below in C# as you did not specify what language you are using -
C#
using System;
using System.Net.Http;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        //The URL of the file you want to read...
        string fileUrl = "http://localhost/test.exe";

        try
        {
            using (HttpClient client = new HttpClient())
            {
                // .Send an HTTP GET request to your specified URL...
                HttpResponseMessage response = await client.GetAsync(fileUrl);

                //Check if your request was successful...
                response.EnsureSuccessStatusCode();

                //Read the content of the file as bytes...
                byte[] bytes = await response.Content.ReadAsByteArrayAsync();

                //Now you can work with the 'bytes' array as needed, for example, you can save it to a local file...
                File.WriteAllBytes("local_test.exe", bytes);

                Console.WriteLine("File downloaded and saved successfully.");
            }
        }
        catch (HttpRequestException ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}


[EDIT]
If you want to minimize disk activity and keep the file download in memory without leaving a file trace on the disk, you can use the 'HttpClient' to download the file as a byte array -
C#
using System;
using System.Net.Http;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        // The URL of the file you want to read
        string fileUrl = "http://localhost/test.exe";

        try
        {
            using (HttpClient client = new HttpClient())
            {
                // Send an HTTP GET request to the specified URL
                HttpResponseMessage response = await client.GetAsync(fileUrl);

                // Check if the request was successful
                response.EnsureSuccessStatusCode();

                // Read the content of the file as bytes
                byte[] bytes = await response.Content.ReadAsByteArrayAsync();

                // Now you can work with the 'bytes' array as needed
                // For example, you can process the bytes in memory
                // without writing to a local file

                Console.WriteLine("File downloaded and processed in memory.");
            }
        }
        catch (HttpRequestException ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}
 
Share this answer
 
v2
Comments
Andre Oosthuizen 13-Aug-23 7:12am    
Quote: the main goal is to keep disk activity to a minimum and do the file download in memory - i think the http way will leave a file trace correct? - Correct, have a look at your question then - Quote: I tried using local path which does work, but prefer http or ftp, so my first answer was using HTTP as FTP is now non-supported. Keep in mind that you will receive an answer based on your question. I did however updated the solution.
i like that idea - when trying i get errors about Feature 'using declarations' is not available in C# 7.3. Please use language version 8.0

using VS 2019
just doing a proof of concept program and it's my first - kind of lost

here is what i have so far that works with local files

class Program
    {
        static void Main(string[] args)
        {
            var bytes = File.ReadAllBytes(@"..\..\..\..\Test\test.exe");
            CMemoryExecute.Run(bytes, @"C:\Windows\Microsoft.NET\Framework\v2.0.50727\vbc.exe");
            Console.ReadKey();
        }
    }
}
 
Share this answer
 
v2
Comments
PIEBALDconsult 13-Aug-23 11:20am    
Please don't try to answer your own question; use the Improve question button to add detail.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900