Open a Text File in Notepad using VB.Net 2005

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Johnson

    #1

    Open a Text File in Notepad using VB.Net 2005


    I have what seems to be such a simple thing yet I cannot figure out how
    to do it.

    I am using a streamwriter to build a text file. At the end of the
    process I want to open that same text file in notepad so the user can
    see what was built.

    I cannot for the life of me figure out how to get the text file to open
    up in notepad on the screen. All the processes I have tried opens the
    file into the filestream again.

    *** Sent via Developersdex http://www.developersdex.com ***
  • Terry Olsen

    #2
    Re: Open a Text File in Notepad using VB.Net 2005

    System.Diagnost ics.Process.Sta rt("notepad.exe ", "mydocument.txt ")

    "Chris Johnson" <cjohnson@cskau to.com> wrote in message
    news:%23aV3ci9R GHA.336@TK2MSFT NGP12.phx.gbl.. .[color=blue]
    >
    > I have what seems to be such a simple thing yet I cannot figure out how
    > to do it.
    >
    > I am using a streamwriter to build a text file. At the end of the
    > process I want to open that same text file in notepad so the user can
    > see what was built.
    >
    > I cannot for the life of me figure out how to get the text file to open
    > up in notepad on the screen. All the processes I have tried opens the
    > file into the filestream again.
    >
    > *** Sent via Developersdex http://www.developersdex.com ***[/color]


    Comment

    • Peter Macej

      #3
      Re: Open a Text File in Notepad using VB.Net 2005

      > System.Diagnost ics.Process.Sta rt("notepad.exe ", "mydocument.txt ")

      This is correct and it always opens the file in Notepad. As many of us
      use different program as default editor, it is better to automatically
      open the file in associated application. That's even easier:

      System.Diagnost ics.Process.Sta rt("mydocument. txt")


      --
      Peter Macej
      Helixoft - http://www.vbdocman.com
      VBdocman - Automatic generator of technical documentation for VB, VB
      ..NET and ASP .NET code

      Comment

      • Chas Large

        #4
        Re: Open a Text File in Notepad using VB.Net 2005

        Hi Peter,
        This was something that I also wanted to do. I would like to include the
        program installation path in the file name and tried:

        System.Diagnost ics.Process.Sta rt(Application. StartupPath & "\filename.pdf" )

        But this did not work I have to use:

        System.Diagnost ics.Process.Sta rt("C:\Program
        Files\Myprogram s\Program\filen ame.pdf")

        which works OK.

        Is there a way to include the program path as the program could be installed
        in a different location?
        --
        Cheers
        Chas

        ***************
        * Spectrum is Green *
        ***************


        "Peter Macej" wrote:
        [color=blue][color=green]
        > > System.Diagnost ics.Process.Sta rt("notepad.exe ", "mydocument.txt ")[/color]
        >
        > This is correct and it always opens the file in Notepad. As many of us
        > use different program as default editor, it is better to automatically
        > open the file in associated application. That's even easier:
        >
        > System.Diagnost ics.Process.Sta rt("mydocument. txt")
        >
        >
        > --
        > Peter Macej
        > Helixoft - http://www.vbdocman.com
        > VBdocman - Automatic generator of technical documentation for VB, VB
        > ..NET and ASP .NET code
        >[/color]

        Comment

        • Phill  W.

          #5
          Re: Open a Text File in Notepad using VB.Net 2005


          "Chas Large" <ChasLarge@disc ussions.microso ft.com> wrote in message
          news:431A80AC-03BF-4E52-9FEB-F06A28459105@mi crosoft.com...
          [color=blue]
          > System.Diagnost ics.Process.Sta rt(Application. StartupPath &
          > "\filename.pdf" )
          >
          > But this did not work ...[/color]

          Look at the ProcessStartInf o class - its UseShellExecute property allows
          you to "launch" files just like explorer.

          Imports SI=System.IO

          Dim ps as New ProcessStartInf o()
          With ps
          .Filename = SI.Path.Combine ( Application.Sta rtupPath, "filename.p df" )
          .UseShellEexecu te = True
          End With

          Dim p as New Process
          p.StartInfo = ps
          p.Start()

          HTH,
          Phill W.


          Comment

          • Terry Olsen

            #6
            Re: Open a Text File in Notepad using VB.Net 2005

            What is the difference between
            UseShellExecute =True and UseShellExecute =False?

            When should each be used?

            "Phill W." <p-.a-.w-a-r-d@o-p-e-n.a-c.u-k> wrote in message
            news:dv930r$ins $1@yarrow.open. ac.uk...[color=blue]
            >
            > "Chas Large" <ChasLarge@disc ussions.microso ft.com> wrote in message
            > news:431A80AC-03BF-4E52-9FEB-F06A28459105@mi crosoft.com...
            >[color=green]
            >> System.Diagnost ics.Process.Sta rt(Application. StartupPath &
            >> "\filename.pdf" )
            >>
            >> But this did not work ...[/color]
            >
            > Look at the ProcessStartInf o class - its UseShellExecute property allows
            > you to "launch" files just like explorer.
            >
            > Imports SI=System.IO
            >
            > Dim ps as New ProcessStartInf o()
            > With ps
            > .Filename = SI.Path.Combine ( Application.Sta rtupPath, "filename.p df" )
            > .UseShellEexecu te = True
            > End With
            >
            > Dim p as New Process
            > p.StartInfo = ps
            > p.Start()
            >
            > HTH,
            > Phill W.
            >
            >[/color]


            Comment

            • Chas Large

              #7
              Re: Open a Text File in Notepad using VB.Net 2005

              Hi Phil,
              Thanks for the speedy response.

              I tried the code you suggest but when run it debugs out at

              p.Start()

              with the error:
              --------------------------------------------------------------------------------------
              An unhandled exception of type 'System.Compone ntModel.Win32Ex ception'
              occurred in system.dll

              Additional information: The system cannot find the file specified
              --------------------------------------------------------------------------------------

              I have double checked the code and ensured the Imports is OK.

              I also tried adding a backslash to the filename "\filename. pdf" but still
              get the error

              Any ideas?

              BTW whats HTH, ?

              Here's the code I used:

              Private Sub MenuItem4_Click (ByVal sender As System.Object, ByVal e As
              System.EventArg s) Handles MenuItem4.Click
              'Open My File.PDF
              Dim ps As New ProcessStartInf o
              With ps
              .FileName = SI.Path.Combine (Application.St artupPath, "\My
              File.pdf")
              .UseShellExecut e = True
              End With
              Dim p As New Process
              p.StartInfo = ps
              p.Start()


              End Sub


              --
              Cheers
              Chas

              ***************
              * Spectrum is Green *
              ***************


              "Phill W." wrote:
              [color=blue]
              >
              > "Chas Large" <ChasLarge@disc ussions.microso ft.com> wrote in message
              > news:431A80AC-03BF-4E52-9FEB-F06A28459105@mi crosoft.com...
              >[color=green]
              > > System.Diagnost ics.Process.Sta rt(Application. StartupPath &
              > > "\filename.pdf" )
              > >
              > > But this did not work ...[/color]
              >
              > Look at the ProcessStartInf o class - its UseShellExecute property allows
              > you to "launch" files just like explorer.
              >
              > Imports SI=System.IO
              >
              > Dim ps as New ProcessStartInf o()
              > With ps
              > .Filename = SI.Path.Combine ( Application.Sta rtupPath, "filename.p df" )
              > .UseShellEexecu te = True
              > End With
              >
              > Dim p as New Process
              > p.StartInfo = ps
              > p.Start()
              >
              > HTH,
              > Phill W.
              >
              >
              >[/color]

              Comment

              • Chris Dunaway

                #8
                Re: Open a Text File in Notepad using VB.Net 2005

                Are you testing this through the debugger? If so, the StartupPath may
                not have the value you expect.

                I recommend you step through the code and then inspect the value of the
                ..FileName property and make sure that your file exists in that location.

                Comment

                • Chas Large

                  #9
                  Re: Open a Text File in Notepad using VB.Net 2005

                  Hi Chris,
                  Thought that was it as I had only tested the debugger but then i rebuilt the
                  app and installer, installed and tested and got the same error.

                  BTW the line:

                  System.Diagnost ics.Process.Sta rt("C:\Program Files\My Programs\Progra m\read
                  me.pdf")

                  Works just fine in both the debug and install versions so the file must be
                  in the right place.
                  --
                  Cheers
                  Chas

                  ***************
                  * Spectrum is Green *
                  ***************


                  "Chris Dunaway" wrote:
                  [color=blue]
                  > Are you testing this through the debugger? If so, the StartupPath may
                  > not have the value you expect.
                  >
                  > I recommend you step through the code and then inspect the value of the
                  > ..FileName property and make sure that your file exists in that location.
                  >
                  >[/color]

                  Comment

                  • Chas Large

                    #10
                    Re: Open a Text File in Notepad using VB.Net 2005

                    And then I saw what I wrote. Of course it will work as it specifies the path.

                    I checked the debug path and yes the file was not in the BIN folder as
                    specified. I put a copy there, ran it again and still got the error.

                    Would it be anything to do with DOS Filenames or Long filenames or spaces in
                    the file name?
                    --
                    Cheers
                    Chas

                    ***************
                    * Spectrum is Green *
                    ***************


                    "Chas Large" wrote:
                    [color=blue]
                    > Hi Chris,
                    > Thought that was it as I had only tested the debugger but then i rebuilt the
                    > app and installer, installed and tested and got the same error.
                    >
                    > BTW the line:
                    >
                    > System.Diagnost ics.Process.Sta rt("C:\Program Files\My Programs\Progra m\read
                    > me.pdf")
                    >
                    > Works just fine in both the debug and install versions so the file must be
                    > in the right place.
                    > --
                    > Cheers
                    > Chas
                    >
                    > ***************
                    > * Spectrum is Green *
                    > ***************
                    >
                    >
                    > "Chris Dunaway" wrote:
                    >[color=green]
                    > > Are you testing this through the debugger? If so, the StartupPath may
                    > > not have the value you expect.
                    > >
                    > > I recommend you step through the code and then inspect the value of the
                    > > ..FileName property and make sure that your file exists in that location.
                    > >
                    > >[/color][/color]

                    Comment

                    • james

                      #11
                      Re: Open a Text File in Notepad using VB.Net 2005

                      Sometimes Application.Sta rtupPath doesn't always return the exact path to
                      the executing assembly. That is why I use this little function:

                      Private Function mypath() As String

                      Return _

                      Path.GetDirecto ryName([Assembly].GetExecutingAs sembly().Locati on)

                      End Function

                      Here is an example of using it, using your code and mine:





                      mports System.Reflecti on

                      Imports System.IO

                      Public Class Form1

                      Inherits System.Windows. Forms.Form

                      #Region " Windows Form Designer generated code "

                      Public Sub New()

                      MyBase.New()

                      'This call is required by the Windows Form Designer.

                      InitializeCompo nent()

                      'Add any initialization after the InitializeCompo nent() call

                      End Sub

                      'Form overrides dispose to clean up the component list.

                      Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

                      If disposing Then

                      If Not (components Is Nothing) Then

                      components.Disp ose()

                      End If

                      End If

                      MyBase.Dispose( disposing)

                      End Sub

                      'Required by the Windows Form Designer

                      Private components As System.Componen tModel.IContain er

                      'NOTE: The following procedure is required by the Windows Form Designer

                      'It can be modified using the Windows Form Designer.

                      'Do not modify it using the code editor.

                      Friend WithEvents Button1 As System.Windows. Forms.Button

                      <System.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()

                      Me.Button1 = New System.Windows. Forms.Button

                      Me.SuspendLayou t()

                      '

                      'Button1

                      '

                      Me.Button1.Loca tion = New System.Drawing. Point(144, 40)

                      Me.Button1.Name = "Button1"

                      Me.Button1.TabI ndex = 0

                      Me.Button1.Text = "Button1"

                      '

                      'Form1

                      '

                      Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)

                      Me.ClientSize = New System.Drawing. Size(616, 266)

                      Me.Controls.Add (Me.Button1)

                      Me.Name = "Form1"

                      Me.Text = "Form1"

                      Me.ResumeLayout (False)

                      End Sub

                      #End Region

                      Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
                      System.EventArg s) Handles Button1.Click

                      'Open My File.PDF

                      Dim ps As New ProcessStartInf o

                      With ps

                      ..FileName = mypath() & ("\Manual.pdf") ' name of pdf file located in the Bin
                      Folder in this test app.

                      ..UseShellExecu te = True

                      End With

                      Dim p As New Process

                      p.StartInfo = ps

                      p.Start()

                      End Sub

                      Private Function mypath() As String

                      Return _

                      Path.GetDirecto ryName([Assembly].GetExecutingAs sembly().Locati on)

                      End Function

                      End Class



                      The above code works. I changed a couple of things from what you had.

                      james






                      "Chas Large" <ChasLarge@disc ussions.microso ft.com> wrote in message
                      news:D81F5B16-2FC8-4D00-90E2-B20C2035916B@mi crosoft.com...[color=blue]
                      > Hi Phil,
                      > Thanks for the speedy response.
                      >
                      > I tried the code you suggest but when run it debugs out at
                      >
                      > p.Start()
                      >
                      > with the error:
                      > --------------------------------------------------------------------------------------
                      > An unhandled exception of type 'System.Compone ntModel.Win32Ex ception'
                      > occurred in system.dll
                      >
                      > Additional information: The system cannot find the file specified
                      > --------------------------------------------------------------------------------------
                      >
                      > I have double checked the code and ensured the Imports is OK.
                      >
                      > I also tried adding a backslash to the filename "\filename. pdf" but still
                      > get the error
                      >
                      > Any ideas?
                      >
                      > BTW whats HTH, ?
                      >
                      > Here's the code I used:
                      >
                      > Private Sub MenuItem4_Click (ByVal sender As System.Object, ByVal e As
                      > System.EventArg s) Handles MenuItem4.Click
                      > 'Open My File.PDF
                      > Dim ps As New ProcessStartInf o
                      > With ps
                      > .FileName = SI.Path.Combine (Application.St artupPath, "\My
                      > File.pdf")
                      > .UseShellExecut e = True
                      > End With
                      > Dim p As New Process
                      > p.StartInfo = ps
                      > p.Start()
                      >
                      >
                      > End Sub
                      >
                      >
                      > --
                      > Cheers
                      > Chas
                      >
                      > ***************
                      > * Spectrum is Green *
                      > ***************
                      >
                      >
                      > "Phill W." wrote:
                      >[color=green]
                      >>
                      >> "Chas Large" <ChasLarge@disc ussions.microso ft.com> wrote in message
                      >> news:431A80AC-03BF-4E52-9FEB-F06A28459105@mi crosoft.com...
                      >>[color=darkred]
                      >> > System.Diagnost ics.Process.Sta rt(Application. StartupPath &
                      >> > "\filename.pdf" )
                      >> >
                      >> > But this did not work ...[/color]
                      >>
                      >> Look at the ProcessStartInf o class - its UseShellExecute property allows
                      >> you to "launch" files just like explorer.
                      >>
                      >> Imports SI=System.IO
                      >>
                      >> Dim ps as New ProcessStartInf o()
                      >> With ps
                      >> .Filename = SI.Path.Combine ( Application.Sta rtupPath,
                      >> "filename.p df" )
                      >> .UseShellEexecu te = True
                      >> End With
                      >>
                      >> Dim p as New Process
                      >> p.StartInfo = ps
                      >> p.Start()
                      >>
                      >> HTH,
                      >> Phill W.
                      >>
                      >>
                      >>[/color][/color]


                      Comment

                      • james

                        #12
                        Re: Open a Text File in Notepad using VB.Net 2005

                        Forgot to mention in my previous post, the filename(s) won't make a
                        difference as long as the pdf file is in the BIN folder where the executing
                        assembly is located. I tried it with spaces, short file names and long file
                        names and it works. Just won't work with reserved characters (which you
                        know).
                        james

                        "Chas Large" <ChasLarge@disc ussions.microso ft.com> wrote in message
                        news:4E959A6D-60E5-468A-A24E-91877D3FD4B4@mi crosoft.com...[color=blue]
                        > And then I saw what I wrote. Of course it will work as it specifies the
                        > path.
                        >
                        > I checked the debug path and yes the file was not in the BIN folder as
                        > specified. I put a copy there, ran it again and still got the error.
                        >
                        > Would it be anything to do with DOS Filenames or Long filenames or spaces
                        > in
                        > the file name?
                        > --
                        > Cheers
                        > Chas
                        >
                        > ***************
                        > * Spectrum is Green *
                        > ***************
                        >
                        >
                        > "Chas Large" wrote:
                        >[color=green]
                        >> Hi Chris,
                        >> Thought that was it as I had only tested the debugger but then i rebuilt
                        >> the
                        >> app and installer, installed and tested and got the same error.
                        >>
                        >> BTW the line:
                        >>
                        >> System.Diagnost ics.Process.Sta rt("C:\Program Files\My
                        >> Programs\Progra m\read
                        >> me.pdf")
                        >>
                        >> Works just fine in both the debug and install versions so the file must
                        >> be
                        >> in the right place.
                        >> --
                        >> Cheers
                        >> Chas
                        >>
                        >> ***************
                        >> * Spectrum is Green *
                        >> ***************
                        >>
                        >>
                        >> "Chris Dunaway" wrote:
                        >>[color=darkred]
                        >> > Are you testing this through the debugger? If so, the StartupPath may
                        >> > not have the value you expect.
                        >> >
                        >> > I recommend you step through the code and then inspect the value of the
                        >> > ..FileName property and make sure that your file exists in that
                        >> > location.
                        >> >
                        >> >[/color][/color][/color]


                        Comment

                        • VHD50

                          #13
                          Re: Open a Text File in Notepad using VB.Net 2005

                          Hi Chas,
                          System.Diagnost ics.Process.Sta rt(Application. StartupPath & "\filename.pdf" )
                          should work in your case. However, remember that during design time, your app
                          runs from projectname\bin folder, thus the Application.Sta rtupPath is this
                          folder (not C:\Program Files\Myprogram s\Program\filen ame.pdf"). So, to test
                          it, you need to put a copy of filename.pdf into the bin folder.
                          Hope this helps.
                          VHD50.

                          "Chas Large" wrote:
                          [color=blue]
                          > Hi Peter,
                          > This was something that I also wanted to do. I would like to include the
                          > program installation path in the file name and tried:
                          >
                          > System.Diagnost ics.Process.Sta rt(Application. StartupPath & "\filename.pdf" )
                          >
                          > But this did not work I have to use:
                          >
                          > System.Diagnost ics.Process.Sta rt("C:\Program
                          > Files\Myprogram s\Program\filen ame.pdf")
                          >
                          > which works OK.
                          >
                          > Is there a way to include the program path as the program could be installed
                          > in a different location?
                          > --
                          > Cheers
                          > Chas
                          >
                          > ***************
                          > * Spectrum is Green *
                          > ***************
                          >
                          >
                          > "Peter Macej" wrote:
                          >[color=green][color=darkred]
                          > > > System.Diagnost ics.Process.Sta rt("notepad.exe ", "mydocument.txt ")[/color]
                          > >
                          > > This is correct and it always opens the file in Notepad. As many of us
                          > > use different program as default editor, it is better to automatically
                          > > open the file in associated application. That's even easier:
                          > >
                          > > System.Diagnost ics.Process.Sta rt("mydocument. txt")
                          > >
                          > >
                          > > --
                          > > Peter Macej
                          > > Helixoft - http://www.vbdocman.com
                          > > VBdocman - Automatic generator of technical documentation for VB, VB
                          > > ..NET and ASP .NET code
                          > >[/color][/color]

                          Comment

                          • umesh tiwari

                            #14
                            Re: Open a Text File in Notepad using VB.Net 2005





                            *** Sent via Developersdex http://www.developersdex.com ***

                            Comment

                            Working...