For most developers moving from console/WinForms project types to web projects, the most important things to learn are the typical differences. I wrote a lot in https://halfblood.pro/web-application-differences-in-visual-studio-and-iis-60fec7e311b3
So, back to your case. You can feel free to call OpenFileDialog
API in a web project anywhere you like, but the effect can be surprising,
- Everything should work when hosting on IIS Express/VS, as the process runs under your account (other answers agreed) and in your user session (other answers didn't even mention). You can see the dialog and click there.
- However, when hosting on full IIS, the worker process is in session 0 (please study Windows session isolation) so is the dialog. As you cannot see into that session by default, the exception occurs. Even if you dismiss the interactive exception, the dialog is showed on the session 0 desktop, which is invisible to you and you cannot click it.
Thus, the web app hangs there forever unless you kill the process.
OK. Enough background, but that explains why everyone says you shouldn't use OpenFileDialog
or any similar thing in a web project.
Note that you shouldn't use other WinForms API either, like System.Drawing
.
What exactly should you use
I think you just start to learn the web, but the modern web is now so vulnerable that in most cases a web app shouldn't attempt to touch users' local file system.
Therefore, your web app should first consider using a cloud based file storage API to help users read/write files. OneDrive/Dropbox/iCloud/Azure Files and others are all popular.
When you have to touch local file system through browser sandbox boundary, there are modern browser side API to pop up the dialogs, such as Window.showSaveFilePicker()
https://developer.mozilla.org/en-US/docs/Web/API/Window/showSaveFilePicker
However, consuming such browser API requires a modern web app type and ASP.NET Core/Blazor makes it rather simple),
https://learn.microsoft.com/en-us/aspnet/core/blazor/file-uploads?view=aspnetcore-7.0&pivots=server
You can of course find ways to do similar with the legacy ASP.NET WebForms you chose, but in the long run your investment isn't worth the while.
Probably too much for you to digest. Find a local teacher/experienced consultant to guide you through, and that can save you a lot of time in the end.