473,324 Members | 2,166 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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 ***
Mar 15 '06 #1
13 39786
System.Diagnostics.Process.Start("notepad.exe", "mydocument.txt")

"Chris Johnson" <cj******@cskauto.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...

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 ***

Mar 15 '06 #2
> System.Diagnostics.Process.Start("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.Diagnostics.Process.Start("mydocument.txt")
--
Peter Macej
Helixoft - http://www.vbdocman.com
VBdocman - Automatic generator of technical documentation for VB, VB
..NET and ASP .NET code
Mar 15 '06 #3
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.Diagnostics.Process.Start(Application.Start upPath & "\filename.pdf")

But this did not work I have to use:

System.Diagnostics.Process.Start("C:\Program
Files\Myprograms\Program\filename.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:
System.Diagnostics.Process.Start("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.Diagnostics.Process.Start("mydocument.txt")
--
Peter Macej
Helixoft - http://www.vbdocman.com
VBdocman - Automatic generator of technical documentation for VB, VB
..NET and ASP .NET code

Mar 15 '06 #4

"Chas Large" <Ch*******@discussions.microsoft.com> wrote in message
news:43**********************************@microsof t.com...
System.Diagnostics.Process.Start(Application.Start upPath &
"\filename.pdf")

But this did not work ...


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

Imports SI=System.IO

Dim ps as New ProcessStartInfo()
With ps
.Filename = SI.Path.Combine( Application.StartupPath, "filename.pdf" )
.UseShellEexecute = True
End With

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

HTH,
Phill W.
Mar 15 '06 #5
What is the difference between
UseShellExecute=True and UseShellExecute=False?

When should each be used?

"Phill W." <p-***********@o-p-e-n.a-c.u-k> wrote in message
news:dv**********@yarrow.open.ac.uk...

"Chas Large" <Ch*******@discussions.microsoft.com> wrote in message
news:43**********************************@microsof t.com...
System.Diagnostics.Process.Start(Application.Start upPath &
"\filename.pdf")

But this did not work ...


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

Imports SI=System.IO

Dim ps as New ProcessStartInfo()
With ps
.Filename = SI.Path.Combine( Application.StartupPath, "filename.pdf" )
.UseShellEexecute = True
End With

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

HTH,
Phill W.

Mar 15 '06 #6
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.ComponentModel.Win32Exception'
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.EventArgs) Handles MenuItem4.Click
'Open My File.PDF
Dim ps As New ProcessStartInfo
With ps
.FileName = SI.Path.Combine(Application.StartupPath, "\My
File.pdf")
.UseShellExecute = True
End With
Dim p As New Process
p.StartInfo = ps
p.Start()
End Sub
--
Cheers
Chas

***************
* Spectrum is Green *
***************
"Phill W." wrote:

"Chas Large" <Ch*******@discussions.microsoft.com> wrote in message
news:43**********************************@microsof t.com...
System.Diagnostics.Process.Start(Application.Start upPath &
"\filename.pdf")

But this did not work ...


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

Imports SI=System.IO

Dim ps as New ProcessStartInfo()
With ps
.Filename = SI.Path.Combine( Application.StartupPath, "filename.pdf" )
.UseShellEexecute = True
End With

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

HTH,
Phill W.

Mar 15 '06 #7
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.

Mar 15 '06 #8
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.Diagnostics.Process.Start("C:\Program Files\My Programs\Program\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:
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.

Mar 15 '06 #9
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:
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.Diagnostics.Process.Start("C:\Program Files\My Programs\Program\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:
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.

Mar 15 '06 #10
Sometimes Application.StartupPath 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.GetDirectoryName([Assembly].GetExecutingAssembly().Location)

End Function

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

mports System.Reflection

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.

InitializeComponent()

'Add any initialization after the InitializeComponent() 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.Dispose()

End If

End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'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.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

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

Me.SuspendLayout()

'

'Button1

'

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

Me.Button1.Name = "Button1"

Me.Button1.TabIndex = 0

Me.Button1.Text = "Button1"

'

'Form1

'

Me.AutoScaleBaseSize = 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(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

'Open My File.PDF

Dim ps As New ProcessStartInfo

With ps

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

..UseShellExecute = True

End With

Dim p As New Process

p.StartInfo = ps

p.Start()

End Sub

Private Function mypath() As String

Return _

Path.GetDirectoryName([Assembly].GetExecutingAssembly().Location)

End Function

End Class

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

james


"Chas Large" <Ch*******@discussions.microsoft.com> wrote in message
news:D8**********************************@microsof t.com...
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.ComponentModel.Win32Exception'
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.EventArgs) Handles MenuItem4.Click
'Open My File.PDF
Dim ps As New ProcessStartInfo
With ps
.FileName = SI.Path.Combine(Application.StartupPath, "\My
File.pdf")
.UseShellExecute = True
End With
Dim p As New Process
p.StartInfo = ps
p.Start()
End Sub
--
Cheers
Chas

***************
* Spectrum is Green *
***************
"Phill W." wrote:

"Chas Large" <Ch*******@discussions.microsoft.com> wrote in message
news:43**********************************@microsof t.com...
> System.Diagnostics.Process.Start(Application.Start upPath &
> "\filename.pdf")
>
> But this did not work ...


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

Imports SI=System.IO

Dim ps as New ProcessStartInfo()
With ps
.Filename = SI.Path.Combine( Application.StartupPath,
"filename.pdf" )
.UseShellEexecute = True
End With

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

HTH,
Phill W.

Mar 15 '06 #11
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" <Ch*******@discussions.microsoft.com> wrote in message
news:4E**********************************@microsof t.com...
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:
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.Diagnostics.Process.Start("C:\Program Files\My
Programs\Program\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:
> 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.
>
>

Mar 15 '06 #12
Hi Chas,
System.Diagnostics.Process.Start(Application.Start upPath & "\filename.pdf")
should work in your case. However, remember that during design time, your app
runs from projectname\bin folder, thus the Application.StartupPath is this
folder (not C:\Program Files\Myprograms\Program\filename.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:
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.Diagnostics.Process.Start(Application.Start upPath & "\filename.pdf")

But this did not work I have to use:

System.Diagnostics.Process.Start("C:\Program
Files\Myprograms\Program\filename.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:
System.Diagnostics.Process.Start("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.Diagnostics.Process.Start("mydocument.txt")
--
Peter Macej
Helixoft - http://www.vbdocman.com
VBdocman - Automatic generator of technical documentation for VB, VB
..NET and ASP .NET code

Mar 17 '06 #13


*** Sent via Developersdex http://www.developersdex.com ***
Apr 4 '06 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jesper | last post by:
How can I open a textfile from C# using notepad (or the user assigned application for this).
3
by: RN Das | last post by:
Hi expert, I have a small web application in C# and ASP.Net and I want to open a file in notepad when clicked on a linked button. Here is hander code when clicked on the link button. It can open...
3
by: sohan | last post by:
hi I have hyperlink column in a datagrid. The column contains the name of a text file. I am able to appendthe full path of the file. The file is on D drive on the server. But on clicking on...
0
by: SimplySuzy | last post by:
Help I am trying to open a text file that I am naming dynamically through global variables in a dts package. When I print the file name to the screen, it is correct. When I use the dts package...
1
by: sachin10 | last post by:
hi i m sachin, i m new to VB programming.i face some problem regarding the sorting of text file contents using arrays.text file have data in the format as below seq...
4
by: Stupid48 | last post by:
I'm trying to do a simple task but can't seem to find a solution. How do I read lines from a text file backwards. i.e. I want to select the last 20 lines of a text file and display them in...
3
by: superc0red | last post by:
Hey, How i can open a text file using shellscripting, and new lines? for example i have the following text file: -------------------- Five Moroccans and a Syrian are charged with 191...
3
by: shil | last post by:
Hi, I'm trying to find a right way the data from a TAB delimited UTF-8 text file into SQL database. My text file has Unicode characters like ö, é. I'm trying to code this in vb.net. Can any one...
1
by: sydmil | last post by:
Hello, I am relatively new to VB6 and I was wondering if anyone could show me an example of a VB6 code to do the following: Open text file (which is a column of numbers) in excel - comma...
2
by: Raman Pahwa | last post by:
i have a text file. I want to import it in table in sql 2005.that table has 8 fields.whn importing text file first 4 fields are in first line and next 2 in 2nd and rest 2 in last line. I want all 8...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.