Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

"Open with" question

17 views
Skip to first unread message

kimiraikkonen

unread,
Dec 18, 2007, 1:38:10 PM12/18/07
to
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.

za...@construction-imaging.com

unread,
Dec 18, 2007, 2:15:53 PM12/18/07
to

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.

Mattias Sjögren

unread,
Dec 18, 2007, 2:12:35 PM12/18/07
to
>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

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

kimiraikkonen

unread,
Dec 18, 2007, 2:37:18 PM12/18/07
to
On Dec 18, 9:12 pm, Mattias Sjögren <mattias.dont.want.s...@mvps.org>
wrote:

> >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
>
> --
> Mattias Sjögren [C# MVP] mattias @ mvps.orghttp://www.msjogren.net/dotnet/|http://www.dotnetinterop.com

> Please reply only to the newsgroup.

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

za...@construction-imaging.com

unread,
Dec 18, 2007, 2:54:23 PM12/18/07
to

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.

Mattias Sjögren

unread,
Dec 18, 2007, 2:55:55 PM12/18/07
to
>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.

kimiraikkonen

unread,
Dec 18, 2007, 4:15:24 PM12/18/07
to
On Dec 18, 9:55 pm, Mattias Sjögren <mattias.dont.want.s...@mvps.org>
wrote:

> >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
>
> --
> Mattias Sjögren [C# MVP] mattias @ mvps.orghttp://www.msjogren.net/dotnet/|http://www.dotnetinterop.com

> Please reply only to the newsgroup.

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.

Family Tree Mike

unread,
Dec 18, 2007, 5:46:04 PM12/18/07
to

"kimiraikkonen" wrote:

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

kimiraikkonen

unread,
Dec 18, 2007, 6:01:37 PM12/18/07
to
On Dec 19, 12:46 am, Family Tree Mike

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

Herfried K. Wagner [MVP]

unread,
Dec 18, 2007, 6:28:57 PM12/18/07
to
"kimiraikkonen" <kimirai...@gmail.com> schrieb:

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

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

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Family Tree Mike

unread,
Dec 18, 2007, 8:42:00 PM12/18/07
to

"Herfried K. Wagner [MVP]" wrote:

> "kimiraikkonen" <kimirai...@gmail.com> schrieb:
> >> Dim arguments As string() = Environment.GetCommandLineArgs()
> >> Dim fn as string = arguments(0)
>

> .... 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.
>
> --
> M S Herfried K. Wagner
> M V P <URL:http://dotnet.mvps.org/>
> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
>

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


>

kimiraikkonen

unread,
Dec 19, 2007, 5:11:09 AM12/19/07
to
On Dec 19, 3:42 am, Family Tree Mike

<FamilyTreeM...@discussions.microsoft.com> wrote:
> "Herfried K. Wagner [MVP]" wrote:
>
>
>
> > "kimiraikkonen" <kimiraikkone...@gmail.com> schrieb:

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

kimiraikkonen

unread,
Dec 25, 2007, 1:36:57 PM12/25/07
to
On Dec 19, 1:28 am, "Herfried K. Wagner [MVP]" <hirf-spam-me-
h...@gmx.at> wrote:
> "kimiraikkonen" <kimiraikkone...@gmail.com> schrieb:

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.

0 new messages