Gilles Radrizzi
Gilles Radrizzi

Reputation: 1009

Open windows explorer in a blazor (Server Side) application

I'm working on a Blazor Server Side application which is used ONLY inside our network. The application handles Building Permits. So far so good.

Historically, the permits were done manually and all related files (blueprints, letters etc) where saved on our fileserver and accessed through Windows Explorer.

To be able to keep the same workflow for file handling, I am tasked to open windows explorer from the Blazor app.

I'm quite aware that this is far from recommended and I tried talking people out of it, but to no avail.

So: is there a way to open a specific location in Windows Explorer directly. I know that you can link to a HREF="file:\my-server\path\to\share", but that only shows the directory listing. There is no way to add files, copy, paste, rename, whatever...

UPDATE (2021-07-08): This is in a controlled environment. The application is NOT public. I would never ever do such a thing for a public application. The people who use the application are pretty limited and will know about the functionality.

Upvotes: 1

Views: 3806

Answers (4)

Marvin Klein
Marvin Klein

Reputation: 1746

The only way to get this done within blazor is by using a custom protocoll.

Create a simple C# console app and bind the compiled exe to your protocoll. The protocoll needs to installed on all client PCs (you can do this via group policies).

Within your blazor app you'll link to you custom protocoll with any parameters.

Keep in mind that you'll need to open a new tab every time because of https://github.com/dotnet/aspnetcore/issues/29259

Upvotes: 1

Gilles Radrizzi
Gilles Radrizzi

Reputation: 1009

For those who need to do the same thing, I figured out how to do it. Nikki9696 pointed me in the right direction in his comments.

The browser will not open Windows Explorer on the client directly (which is a very good thing as pointed out in other answers). But I implemented a custom URI Scheme

Here is a simplified version of the code:

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        if (args.Length > 0)
        {
            string path = string.Empty;
            path = args[0].Replace("my-app:", string.Empty);
            path = System.Web.HttpUtility.UrlDecode(path);
            if (!System.IO.Directory.Exists(path))
            {
                System.IO.Directory.CreateDirectory(path);
            }
            System.Diagnostics.Process.Start("explorer.exe", path);
        }
        else
            Console.WriteLine("No Argument specified");
    }
}

This works. I just need to add the Custom URI Scheme in the clients registry.

Upvotes: 1

Bennyboy1973
Bennyboy1973

Reputation: 4216

If you COULD do that, it would be a bug in Blazor, and Microsoft would have to patch it immediately. It's a horrible, horrible idea. Like-- epically terrible.

If people will be accessing files on the individual machines, then you'll need to write apps for those machines, not a web app. Maybe they can upload files to the server.

If you can host all the files on your IIS server (I'm assuming), then you have to configure folders, links, virtual directories etc. etc.

Upvotes: 0

Jcl
Jcl

Reputation: 28272

You can't open explorer from the browser on the client unless you make a plugin for the browser (or your own browser).

Even if you could open Explorer from Blazor Server, it'd open on the server, not on the client.

That said, making your own browser (which supports that), is not a necessarily complicated task these days (look at CefSharp, WebView2 or similar), if that's a possibility, but that's WAY WAY outside what can or should be explained in this question.

Or you can make your own filebrowser on the web (using Blazor components).

But definitely not open explorer from a browser... and if you find a way on a standard browser, please report it because that's a HUGE security flaw.

Upvotes: 1

Related Questions