Opening a File Locally using the Browse for File Function

Opening a File Locally using the Browse for File Function
0.0 0

#1

I’m making a simple program in which, when you press a button the dialog box with all the directories appears so that you can select and open your document. There is no server or browser or web involvement. In fact, since I’m making the project in Visual Studio, I prefer that it opens in Visual Studio, or at least through it.

This is a bit of a code I wrote in Visual Basic, but it doesn’t actually open the file.

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim fd As OpenFileDialog = New OpenFileDialog()
        Dim strFileName As String

        fd.Title = "Open File Dialog"
        fd.InitialDirectory = "C:\"
        fd.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
        fd.FilterIndex = 2
        fd.RestoreDirectory = True

        If fd.ShowDialog() = DialogResult.OK Then
            strFileName = fd.FileName
            'fd.OpenFile() "strFileName"  "Microsoft Visual Studio\Frontend.vb"
        End If

    End Sub


End Class

Any help or suggestion is appreciated.


#2

I’m not a VB expert, but some stuff you may consider.

Not sure about string escaping in VB, but maybe “C:” should be “C:\”?

Also since it’s about C drive, you may get permission issues! try using another drive, or run the script/app as administrator.

For opening the file using the VS, (is supposed to be)you need simply call the visual studio exe file, and pass the file full path as argument to it. (search for visual studio command line arguments for more info)

Keep going on greta work, happy programming


#3

I will try your recommended changes. But the thing is I have discovered that VB is not very widely used these, even in areas such as software development. So I’m considering making this in JavaScript, and that’s basically what I’m asking. How can I open a file from computer through the browse button without any server/browser/web involvement, using JavaScript?


#4

From a browser(not your case), accessing any local resource is kind of impossible by js becasue of security stuffs.

But windows has jScript engine which allows you run a standalone .js javascript file, but I have never worked with it, and don’t know if it’s possible to call another native command with it or now.

You may read teh reference here, and remember, not all functions JScript supports are available for other browsers/engine and vice-versa.

Also check this link which like your situation maybe

I suggest for more common(and probably safer) way, you may have a look about CMD and Bash too.


#5

Unless you have access node on a user’s system, you can’t. Javascript only runs in the browser or node.

But if you have access to that environment or can package the node runtime, it’s extremely easy.

https://nodejs.org/api/fs.html#fs_fs_readfile_path_options_callback