Link to home
Create AccountLog in
Avatar of Steve Williams
Steve WilliamsFlag for United States of America

asked on

Why does System.Diagnostics.Process.Start open file with errors?

Ok So I write this app to quickly find files in a source directory and all its sub directories. The search and find portion works great. The files that this app searches for are used for laser etching part numbers on our products. We have roughly 20,000+ product configurations available in the sub directories. The vb app I wrote has two major functions aside from the search and find, these include Edit & Burn. Each function requires a different .exe to complete the task. Here is a follow thru on how the app should work:
The user types a part number to search for an etching pattern to burn
The user now checks a radio button to choose a task either edit or burn
Then the user can either tab to and use the arrow keys and hit enter or double click the selected item in the list box to open the file
If the edit is chosen the edit software opens up the file and they make their changes, saves their work and then closes the edit application.
Either after they edit the file or just initially choose the burn radio button, the user can either tab to and use the arrow keys and hit enter or double click the selected item in the list box to open the file.
The user can now use the burn application to mark the products for the current order.
Then they can close the burn application and the next order is ready for the Etch-O-Matic application.


Due to licensing the installed editing and burn  software is only allowed to be installed on one computer with the required usb dongle. For this reason I created some blank ".vlm" files and sub directories to test the app on my development computer. I also use MS Excel 2016 for the edit program and MS Word 2016 for the burn program just to test functionality of the vb code.

Now the issues! When I open the file for edit or burn an error comes up saying it can't find the file "C:\Program.xlsx" for edit and "C:\Program.doc" for burn. Then the file does not open. Then I get a couple other errors. See the attached images for the errors that come up.

The development computer is running Windows 10 Professional and am using VB Community 2015 to write the code. The Laser Etching machine is running Windows XP SP3 with dot net 4.0.

Not sure if any of this makes sense or if any of you have heard of these errors before. I searched for fixes for all of the errors and I get nothing. I also tried to see if there was another way of opening the files other than using the System.Diagnostics.Process.Start.

Here is the code for the form frmEtchOMatic.vb
Imports System.IO
Imports System.ComponentModel
Imports System.Threading
Imports System.Diagnostics

Public Class frmEtchOMatic

    Private Const SourceFolder As String = "C:\Program Files\VisualLaserMarker\MarkingFiles"
    Private _bsFiles As New BindingSource()
    Private _dtFiles As New DataTable()

    Private Sub frmEtchOMatic_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If Not Directory.Exists(SourceFolder) Then Throw New Exception("Folder doesn't exist")
        _dtFiles.Columns.Add("FilePath")
        _dtFiles.Columns.Add("FileName")

        RefreshFiles()

        _bsFiles.DataSource = _dtFiles
        lstPartNum.DisplayMember = "FileName"
        lstPartNum.ValueMember = "FilePath"
        lstPartNum.DataSource = _bsFiles
        Me.Show()
        txtPartNum.Focus()
        Me.Text = "Etch-O-Matic  " & String.Format("Version {0}", My.Application.Info.Version.ToString)
    End Sub
    Private Sub RefreshFiles()
        'loop around each path and loads the files.
        For Each filePath As String In IO.Directory.GetFiles(SourceFolder, "*.*", SearchOption.AllDirectories).Where(Function(x)
                                                                                                                         Return (New String() {".vlm", ".VLM"}).
                                                                                       Contains(System.IO.Path.GetExtension(x))
                                                                                                                     End Function)
            _dtFiles.Rows.Add(New Object() {filePath, IO.Path.GetFileName(IO.Path.GetFileName(filePath))})
        Next
    End Sub

    Private Sub txtPartNum_TextChanged(sender As Object, e As EventArgs) Handles txtPartNum.TextChanged
        If String.IsNullOrWhiteSpace(txtPartNum.Text) Then
            _bsFiles.RemoveFilter()
        Else
            _bsFiles.Filter = String.Format("FileName LIKE '%{0}%'", txtPartNum.Text)
        End If
    End Sub
    Private Sub txtPartNum_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtPartNum.KeyPress
        If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Return) Then
            SendKeys.Send("{TAB}")
            e.Handled = True
        End If
    End Sub
    Private Sub lstPartNum_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lstPartNum.KeyDown
        Dim theFile = lstPartNum.SelectedValue
        If e.KeyCode = Keys.Enter Then
        End If
        If theFile IsNot Nothing And rbnEditPat.Checked Then
            System.Diagnostics.Process.Start("C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE", theFile)
        End If
        If theFile IsNot Nothing And rbnBurnPat.Checked Then
            System.Diagnostics.Process.Start("C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE", theFile)
        End If
    End Sub
    Private Sub lstPartNum_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles lstPartNum.MouseDoubleClick
        Dim theFile = lstPartNum.SelectedValue
        If theFile IsNot Nothing And rbnEditPat.Checked Then
            System.Diagnostics.Process.Start("C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE", theFile)
        End If
        If theFile IsNot Nothing And rbnBurnPat.Checked Then
            System.Diagnostics.Process.Start("C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE", theFile)
        End If
    End Sub
End Class

Open in new window


OK so I did some investigating and found another way to skin the cat. Problem is the new code comes up with the same errors. I changed the list box mouse double click sub to the following code:

 Private Sub lstPartNum_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles lstPartNum.MouseDoubleClick
        Dim theFile = lstPartNum.SelectedValue
        If theFile IsNot Nothing And rbnEditPat.Checked Then
            Dim procedit As New Process
            procedit.StartInfo.FileName = "C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE"
            procedit.StartInfo.Arguments = SourceFolder + theFile
            procedit.Start()
        End If
        If theFile IsNot Nothing And rbnBurnPat.Checked Then
            Dim procburn As New Process
            procburn.StartInfo.FileName = "C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE"
            procburn.StartInfo.Arguments = SourceFolder + theFile
            procburn.Start()
        End If
    End Sub

Open in new window

burn-error-1.jpg
burn-error-2.jpg
edit-error-1.jpg
edit-error-2.jpg
edit-error-3.jpg
form-ss.JPG
Avatar of ste5an
ste5an
Flag of Germany image

Your code need improvement.. then the problems are easier to spot..

1. SourceFolder + theFile? I head to search for that. The normal naming would be in this case SOURCE_PATH + FileName. A constant/variable named Path contains per convention always a trailing back slash. You SourceFolder constant does not. Thus the SourceFolder + theFile results in a no-existing path name, not a full file name.

2. Your using many redundant calls to Process.Start. Why is this a problem? Well, cannot speak for Office 2016 right now, but Office 2013 has not always the \root\ partial path. So you need a Office detection method and use this detected path.
Avatar of Steve Williams

ASKER

ste5an, Thanks for the Input I changed the Constant "SourceFolder to "SOURCE_PATH" and the variable "theFile" to "FileName". I also added a backslash to the line of code shown below:

   Private Const SOURCE_PATH As String = "C:\Program Files\VisualLaserMarker\MarkingFiles\"

Open in new window


This did not make any difference. It still errors out. After you mentioned that I need an MS Office  detection method, I decided to just use a couple basic text editor programs.These are only for testing the code to see if the radio buttons are changing the application.

I chose Sublime Text Editor and TextPad 8. I did however notice that the variable is mixing up the paths one for source and one for directory. Somewhere in my code I have two variables mixing information. I will post my discovery once I figure that out.

Attached are a couple screenshots from the the two text editors they show what I'm talking about. Look at the path on the top of each window it's highlighted yellow. The TextPad 8 App shows the correct path to the file, where as the Sublime Text editor mixes up that information which falls inline with the problem I'm having with the MS Office apps.
textpad-8.JPG
sublime-text.JPG
Ok So I wanted to know what was being created with the _dtFiles DataTable(). So I added some code to create a csv file of the output. Here is the result. It shows that the path also has the file name attached to the string. I need to rethink the logic of my code. Anyone have any input that might help point me in the right direction?

User generated image
Since you are passing this (effectively) as a command-line argument, you need to wrap the filename in quotation marks. By "filename" I am referring to the path that you are passing to WINWORD and EXCEL. Basically, whatever is inside of "theFile".

e.g.

theFile = """" & theFile & """"
procburn.StartInfo.FileName = "C:\Program Files (x86)\Microsoft Office\root\Office16\WINWORD.EXE"
procburn.StartInfo.Arguments = theFile

Open in new window

@käµfm³d 👽
 theFile is declared with this line of code
Dim theFile = lstPartNum.SelectedValue

Open in new window


I also noticed that the file path is being split in two! This is i'm sure what is causing the issue. I just haven't been able to pinpoint the reason.

If you look at this screenshot: User generated image
You will see that it is a mixture of two paths:
1st path is the project debug path it is: "\\dell2011\Engineering\VBApplications\EtchOMatic\Etch-O-Matic\Etch-O-Matic\bin\Debug"

2nd path is the actual file it is : "C:\Program Files\VisualLaserMarker\MarkingFiles\sub1\sub1-1234-1234.vlm"

If you look at this screenshot: User generated image
you will see the missing file is called "C:\Programs.xlsx" which is not an actual file "C:\Programs " is missing  "Files\VisualLaserMarker\MarkingFiles\sub1\sub1-1234-1234.vlm"

I don't get it what am I doing wrong? I just fail to see in the code where this is getting parsed into its two peices.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Thanks käµfm³d 👽,  that did the trick! Opens like it's supposed to now with no errors. Here is the final code that I changed that works:

    Private Sub lstPartNum_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles lstPartNum.MouseDoubleClick
        Dim theFile = lstPartNum.SelectedValue
        If theFile IsNot Nothing And rbnEditPat.Checked Then
            Dim procedit As New Process
            procedit.StartInfo.UseShellExecute = True
            procedit.StartInfo.FileName = EditAppPath
            procedit.StartInfo.Arguments = Chr(34) & theFile & Chr(34)
            procedit.Start()
        End If
        If theFile IsNot Nothing And rbnBurnPat.Checked Then
            Dim procburn As New Process
            procburn.StartInfo.UseShellExecute = True
            procburn.StartInfo.FileName = BurnAppPath
            procburn.StartInfo.Arguments = Chr(34) & theFile & Chr(34)

            procburn.Start()
        End If
    End Sub
    Private Sub lstPartNum_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lstPartNum.KeyDown
        Dim theFile = lstPartNum.SelectedValue
        If e.KeyCode = Keys.Enter Then
        End If
        If theFile IsNot Nothing And rbnEditPat.Checked Then
            Dim procedit As New Process
            procedit.StartInfo.UseShellExecute = True
            procedit.StartInfo.FileName = EditAppPath
            procedit.StartInfo.Arguments = Chr(34) & theFile & Chr(34)
            procedit.Start()
        End If
        If theFile IsNot Nothing And rbnBurnPat.Checked Then
            Dim procburn As New Process
            procburn.StartInfo.UseShellExecute = True
            procburn.StartInfo.FileName = BurnAppPath
            procburn.StartInfo.Arguments = Chr(34) & theFile & Chr(34)
            procburn.Start()
        End If
    End Sub
End Class

Open in new window

Appreciate all your help guys. I know how valuable your time is, it is greatly appreciated.