"Open with" question

K

kimiraikkonen

Hi,
I have small project which opens text files via inside the project but
i want to associate this app for every text file. To have a test, if i
right click on a text file -> then "open with" -> my application...
but my app's text area(textbox,where the text data is loaded into. )
is opened blank, nothing is displayed except a general view of my
project app

How can i make "open with" association to display text with my project
properly?

Thanks.
 
M

Mattias Sjögren

How can i make "open with" association to display text with my project
properly?

Your application would likely accept the path to the file on the
command line. You then parse the command line during application
startup, load the file and display the content in the text box.


Mattias
 
Z

zacks

Hi,
I have small project which opens text files via inside the project but
i want to associate this app for every text file. To have a test, if i
right click on a text file -> then "open with" -> my application...
but my app's text area(textbox,where the text data is loaded into. )
is opened blank, nothing is displayed except a general view of my
project app

How can i make "open with" association to display text with my project
properly?

All an "open with" association does is launch the associated
application with the complete path to the file clicked on in the
Command Line. It is up to your application to see that there is a file
name in the command line and process it appropriately.
 
K

kimiraikkonen

Your application would likely accept the path to the file on the
command line. You then parse the command line during application
startup, load the file and display the content in the text box.

Mattias

My app is not a console application however, where and what can i add
this to create a "open with" functionality?
 
Z

zacks

My app is not a console application however, where and what can i add
this to create a "open with" functionality?

If the form's Load event. Check the contents of the
Environment.CommandLine. If there is a filename present, open it, read
the data, and put the data in the textbox's Text property.
 
M

Mattias Sjögren

My app is not a console application however, where and what can i add
this to create a "open with" functionality?

The principle is the same in a Windows app. You can still have a
parameterized Main entry point, or you can use
System.Environment.GetCommandLineArgs() from anywhere in your code.


Mattias
 
K

kimiraikkonen

The principle is the same in a Windows app. You can still have a
parameterized Main entry point, or you can use
System.Environment.GetCommandLineArgs() from anywhere in your code.

Mattias

Hi again, could you sample some code and will i put them in form_load
event?

Assume, i have a textbox and openfiledialog control and i use
streamreader to display texts.

Thanks.
 
F

Family Tree Mike

kimiraikkonen said:
Hi again, could you sample some code and will i put them in form_load
event?

Assume, i have a textbox and openfiledialog control and i use
streamreader to display texts.

Thanks.

I believe all you need to do is this:

Dim arguments As string() = Environment.GetCommandLineArgs()
Dim fn as string = arguments(0)

if (System.IO.File.Exists(fn)) then
MyTextBox.Text = System.IO.File.ReadAllText(fn)
endif
 
K

kimiraikkonen

I believe all you need to do is this:

Dim arguments As string() = Environment.GetCommandLineArgs()
Dim fn as string = arguments(0)

if (System.IO.File.Exists(fn)) then
MyTextBox.Text = System.IO.File.ReadAllText(fn)
endif

Hi, that doesn't work :-( Only "MZ" is displayed textbox with no sense.
 
H

Herfried K. Wagner [MVP]

.... should read:

\\\
If Arguments.Length > 1 Then
Dim fn As String = Arguments(1)
if (System.IO.File.Exists(fn)) then
MyTextBox.Text = System.IO.File.ReadAllText(fn)
endif

End If
///
Hi, that doesn't work :-( Only "MZ" is displayed textbox with
no sense.

The first argument ('Arguments(0)') seems to contain the path to your
executable file. 'MZ' are typically the first characters in executable
files.
 
F

Family Tree Mike

Herfried K. Wagner said:
.... should read:

\\\
If Arguments.Length > 1 Then
Dim fn As String = Arguments(1)


End If
///


The first argument ('Arguments(0)') seems to contain the path to your
executable file. 'MZ' are typically the first characters in executable
files.

Thanks Herfried. I wasy typing without a compiler. I believe the command
args are starting at index of one as you said.
 
K

kimiraikkonen

:







Thanks Herfried. I wasy typing without a compiler. I believe the command
args are starting at index of one as you said.

Hi there,
After trying to figure out where and what the codes are used for i
managed to what i was trying to by adding to form_load event:

Dim cla As String() = Environment.GetCommandLineArgs()
If cla.Length > 1 Then
myStreamReader = System.IO.File.OpenText(cla(1))
TextBox1.Text = myStreamReader.ReadToEnd
myStreamReader.Dispose()

'After text is opened, all the texts come up as selected, thus i added
to make them unselected.
TextBox1.SelectionStart = 0
End If
 
K

kimiraikkonen

... should read:

\\\
If Arguments.Length > 1 Then
Dim fn As String = Arguments(1)


End If
///


The first argument ('Arguments(0)') seems to contain the path to your
executable file. 'MZ' are typically the first characters in executable
files.

Hi,
I want to ask this:

Why was i forced to use:

If Arguments.Length > 1 Then
Dim fn As String = Arguments(1)

'do stuff

end if

For example when i want to display the current app's path:

Dim cla As String() = Environment.GetCommandLineArgs()

Msgbox(cla(0))

shows "exact" path in messagebox, no other extra or unwanted strings
like "MZ", so it would be kind of you explaining this issue.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top