如何找到excel.exe路径和notpad.exe路径

时间:2013-09-12 12:43:55

标签: vb.net excel csv exe

export excel or csv file之后的应用程序中,然后在excel或记事本中显示该文件。 在这种情况下,使用

Excel中:

  Dim xExcelFilePath As String = System.Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + "\Microsoft Office"
            xDir = New DirectoryInfo(xExcelFilePath)
            For Each xDirectory As DirectoryInfo In xDir.GetDirectories ' it use for find any version of excel is installed or not
                If xDirectory.Name.Count < 6 Then Continue For
                If xDirectory.Name.Trim.Substring(0, 6).ToUpper = "OFFICE" Then
                    If System.IO.File.Exists(xExcelFilePath & "\" & xDirectory.Name & "\EXCEL.EXE") Then
                        xExcelFilePath = xExcelFilePath & "\" & xDirectory.Name & "\EXCEL.EXE"
                        Exit For
                    End If
                End If
            Next
            If System.IO.File.Exists(xExcelFilePath) Then
                Dim p As New Process() ' xExcelFilePath means start and stop the local system process
                p.StartInfo.UseShellExecute = True
                p.StartInfo.WindowStyle = ProcessWindowStyle.Maximized
                p.StartInfo.FileName = xExcelFilePath ' Assaign the file name
                p.StartInfo.Arguments = """" + xDestinationPath + """"
                Grid1.SaveExcel(xDestinationPath, FarPoint.Win.Spread.Model.IncludeHeaders.ColumnHeadersCustomOnly) ' Export the Excel File
                p.Start()

            Else
                Msg.Err("Could not find Excel installed on this system; file saved to:" + xExcelFilePath + ".")
            End If

记事本中:

 Dim p As New Process() ' xExcelFilePath means start and stop the local system process
            p.StartInfo.UseShellExecute = True
            p.StartInfo.WindowStyle = ProcessWindowStyle.Maximized
            p.StartInfo.FileName = "C:\windows\notepad.exe"
            p.StartInfo.Arguments = """" + Application.StartupPath & Grid1.ActiveSheet.SheetName & ".csv" + """"
            xCSVSheet.SaveTextFile(Application.StartupPath & Grid1.ActiveSheet.SheetName & ".csv", TextFileFlags.None, Model.IncludeHeaders.BothCustomOnly, "", ",", "")
            p.Start()

在某些系统excel file路径中的上述代码中,此路径不按此顺序排列,因此此处静态添加了抛出异常和Notepad exe。如何在sysem中获得exe file路径?

2 个答案:

答案 0 :(得分:2)

不要担心确切的路径,让Windows处理它。如果要使用记事本打开文件,只需使用以下代码:

Process.Start("notepad.exe", Application.StartupPath & Grid1.ActiveSheet.SheetName & ".csv")

要启动程序最大化,您必须将其更改为以下内容:

    Dim startInfo As New ProcessStartInfo("notepad.exe")
    startInfo.WindowStyle = ProcessWindowStyle.Maximized
    startInfo.Arguments = """" & Application.StartupPath & Grid1.ActiveSheet.SheetName & ".csv"""
    Process.Start(startInfo)

答案 1 :(得分:0)

试试这个:

 Dim excelpath = Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\excel.exe", "Path", "Key does not exist")
                  excelpath=  excelpath & "EXCEL.EXE" 
     Shell(Chr(34) & excelpath & Chr(34) & " " & Chr(34) & FLE & Chr(34), vbNormalFocus)

在我的情况下,FLE是XML文件