-2

Using VB on Windows 11 (64bits) I have a windows form application containing two buttons. One shows the OpenFileDialog and the other opens on Excel workbook with a name based on the current date (i.e. 230701_Data.xlsx). That worked but at some point something must have changed that caused that both methods of opening a file do not open the file(s) anymore eventhough no errors or exceptions are shown and the filenames are correct and the files can be opened from Explorer. The file based on the date is even read and written from the program!

What could be causing that the files can't be shown anymore?

Public Function selectFile()
    Dim fileInUse As String = My.Settings.fileInUse.Trim()
    Dim selectedFile As String = "" 
    Dim ofd As OpenFileDialog = New OpenFileDialog
    ofd.DefaultExt = "xlsx"
    ofd.FileName = ""           '// System.IO.Path.GetFileName(fileInUse)
    ofd.InitialDirectory = Path.GetDirectoryName(fileInUse)
    ofd.CheckFileExists = True
    ofd.Multiselect = False
    ofd.FilterIndex = 1
    ofd.Filter = "All Files (*.*) |*.*|Excel Files (*.xl*)|*.xl*|CSV Files (*.csv)|*.csv|Text Files (*.txt)|*.txt|Visual Studio Files(*.vb)|*.vb|TradingView Scripts(*.pine)|*.pine|PDF Files (*.pdf)|*.pdf"

    While selectedFile = ""
        If ofd.ShowDialog = DialogResult.OK Then
            If ofd.FileName = My.Settings.fileInUse Then
                MsgBox("The selected file is in use" + Environment.NewLine + fileInUse + Environment.NewLine + "cannot be opened")
            Else
                selectedFile = ofd.FileName
            End If
            Try
                ofd.OpenFile()
            Catch ex As Exception
                MsgBox("OpenFile failed Error= " & ex.Message)
            End Try

        Else
            Exit While                  '// Must allow to exit without choosing a file 
        End If
    End While

    Return selectedFile
End Function
SoftwareTester
  • 1,048
  • 1
  • 10
  • 25
  • You should be creating your `OpenFileDialog` with a `Using` statement, in which case it will be disposed at the end of the block. ALWAYS create disposable objects this way unless you specifically want to use them outside the scope in which they are created. – jmcilhinney Jul 01 '23 at 08:06
  • Instead of Dim ofd As OpenFileDialog = New OpenFileDialog I made a using block of it but it makes no difference, any file selected doesm't open – SoftwareTester Jul 02 '23 at 07:11
  • I didn't say that a `Using` block would fix the issue. That's why it's a comment and not an answer. It's just proper practice for any disposable object. – jmcilhinney Jul 02 '23 at 08:42
  • I got it working with Using proc As Process = New Process With proc.StartInfo .UseShellExecute = True '// Necessary, otherwise System.ComponentModel.Win32Exception ... The specified exceutable is not a valid appication for this OS platform .FileName = "EXCEL.EXE" '// .FileName = ofd.FileName will NOT use .Arguments /e /r .Arguments = "/e /r " & Chr(34) & ofd.FileName & Chr(34) End With proc.Start() End Using – SoftwareTester Jul 03 '23 at 11:29
  • but I'm not satisfied as I have to specify .FileName= "EXCEL.EXE" as I want opening the file in readonly mode instead of the general .FileName= ofd.FileName Thanks for the comments – SoftwareTester Jul 03 '23 at 11:29
  • Your level of satisfaction is not a magic wand that makes all things possible. If you want to open a file in the equivalent way to double-clicking it in File Explorer then call `Process.Start` and pass the file path. If you don't want that then you have to do something else. If you want functionality specific to an application then you have to specify that application. That you don't want it to be so doesn't make a difference. – jmcilhinney Jul 03 '23 at 11:39

1 Answers1

1

This is a perfect example of why you should ALWAYS read the relevant documentation. If you are posting a question here then you should already have done so. If you had done that then your problem would be obvious.

The documentation for the OpenFileDialog.OpenFile method makes it clear that the method returns a Stream and it's from that Stream that YOU are supposed to read the data the file contains. You are just ignoring the returned Stream so why would anything useful happen?

If what you actually want to happen is for the file to be opened in its default application, e.g. Excel for an XLSX file, then what you actually need to do is call Process.Start and pass the file path as an argument.

Actually, it occurs to me that maybe you are trying to open the file elsewhere, in code that you haven't shown us. In that case, it's bad that you haven't shown us code that's relevant to the issue. If the file is failing to open elsewhere then that would likely be caused by the fact that you are opening it in this code but not closing it. If it's opened exclusively here, obviously it can't be opened elsewhere. If you want to open it here, use the Stream you open. If you don't want that then don't open it here in the first place.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • I replaced my code with the code in the documentation and it makes no difference at all. You suppose I want to raed the file but I just want to open it.. Indeed i didn't provide you with all the code of the project, who does?I also tried to open files that have nothing to do with my project and those do not open as welll so it isn't because I opened the file elsewhere in my program. It worked before. You don't answer the question but provide very little general info. To me this is an almost useless answer not addressing the problem. I appreciate any comment but yours is low level – SoftwareTester Jul 02 '23 at 06:12
  • @SoftwareTester: *"You suppose I want to raed the file"*. Of course I do, because that's what the code you've provided indicates. We only know what you tell us, so if you don't provide the full picture then how are we supposed to know what you want? *"I just want to open it"*. What does that actually mean? The code you've provide DOES open the file. If it's failing to do so then there would be an exception, but you have mentioned that there is, so of course we assume there isn't. – jmcilhinney Jul 02 '23 at 08:40
  • @SoftwareTester, if you actually mean that you want the file to open in Excel or some other default application then I've already addressed that in my answer. Maybe you just didn't read it properly. Of course, there's nothing in your code that would make that happen and you haven't explained that that is the case, so how are we to know? If people don't understand your question, consider that it might be that your ex0planatiuon of the problem is inadequate. If you want the files to open in their default apps then don't call `OpenFile` and do call `Process.Start`, just as I said in the answer. – jmcilhinney Jul 02 '23 at 08:41