如何findexcel.exepath和notpad.exepath

在我的应用程序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 filepath不是这个顺序,所以它的throw exception和Notepad exe是静态添加到这里的。 我怎样才能得到的exe filepath在sysem?

不要担心确切的path,让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)