Opening a file in my program
Opening a file in my program
Opening a file in my program
Opening a file in my program
Opening a file in my program
Opening a file in my program Opening a file in my program Opening a file in my program Opening a file in my program Opening a file in my program Opening a file in my program Opening a file in my program Opening a file in my program
Opening a file in my program Opening a file in my program
Opening a file in my program
Go Back  Xtreme Visual Basic Talk > > > Opening a file in my program


Reply
 
Thread Tools Display Modes
  #1  
Old 08-31-2005, 04:26 PM
trinkus79's Avatar
trinkus79 trinkus79 is offline
Centurion
 
Join Date: Jan 2004
Location: Portland, OR
Posts: 161
Default Opening a file in my program


I made a program that uses a custom file extension ".prs" I used the deployment wizard thing, and made the install files and everything, and I even set up the file types in the setup (with some help from msdn). It sort of works they way it's supposed to. If I search my computer for .prs files, they have the same icon as my program, and if I double click them my program opens. Only problem is, my program doesn't load any of the data from the file. They are just simple text files with a .prs extension. Do I have to add some code to my form load event? or anything like that...

thanks in advance...
Reply With Quote
  #2  
Old 08-31-2005, 04:43 PM
wayneph's Avatar
waynephOpening a file in my program wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

Quote:
Originally Posted by trinkus79
Do I have to add some code to my form load event? or anything like that...
Yep. The Environment.CommandLine property will have the text passed into the program. (In this case your filename.)

You need to look at this value and determine what to do with it to load the file.
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #3  
Old 09-07-2005, 05:50 PM
trinkus79's Avatar
trinkus79 trinkus79 is offline
Centurion
 
Join Date: Jan 2004
Location: Portland, OR
Posts: 161
Default

I've never used this before, and I'm still kinda new to .net.....so basically that link just confused the poo outta me

any pointers to put me in the right direction?
Reply With Quote
  #4  
Old 09-07-2005, 06:30 PM
sgt_pinky's Avatar
sgt_pinky sgt_pinky is offline
Contributor
 
Join Date: Feb 2004
Location: Melbourne, Australia
Posts: 633
Default

So, for example, you can do this in your Form_Load:

Code:
Dim StartUpParams As String() = Environment.GetCommandLineArgs

Now set a breakpoint and have a look at that string array, and you will see the startup parameters for your app (which will be the *.prs file in your case).

The only problem with this is that its difficult to debug. It's a simple method though, so you should be able to work it out. You just have to install your app, test it, change code, install, test again, etc. Yeah, annoying.
Reply With Quote
  #5  
Old 09-08-2005, 11:33 AM
trinkus79's Avatar
trinkus79 trinkus79 is offline
Centurion
 
Join Date: Jan 2004
Location: Portland, OR
Posts: 161
Default

thanks pinky, I'll give that a whirl.

I also have another prob that kinda falls in line with this one I think. On my form I have a rich text box that I use as a "special instructions" field for my form. I have a bunch of other fields on this form, and when the user saves, it just saves to a textfile, but with the .prs extension. Everything works fine as long as the user doesn't put a 'return' in the rich textbox. Everything is saved on it's own line in the text file, and I just loop through the file and put everything back when it 'opens' the file. It works great, but if you can imagine when it gets to the returns in the rich textbox, there are blank lines in the file, so it kinda jumbles stuff around. Any ideas on how to fix this? I could post my code if it would help....
Reply With Quote
  #6  
Old 09-08-2005, 02:02 PM
trinkus79's Avatar
trinkus79 trinkus79 is offline
Centurion
 
Join Date: Jan 2004
Location: Portland, OR
Posts: 161
Default

What am I looking for exactly? This is what I did
Code:
Dim icnt As Integer Dim StartUpParams As String() = Environment.GetCommandLineArgs For icnt = 0 To UBound(StartUpParams) MessageBox.Show(StartUpParams(icnt), "start up", MessageBoxButtons.OK) Next
when I double click a program associated with my program I get a this:
one msg box that says:
"C:\blahblahblah\productionrequestform.exe
then another that says:
"C:\blahblahbla\filename.prs
...what do I do next? I'm self taught in VB and I don't think any of the books I've read really cover this nifty stuff either that or I missed it
Reply With Quote
  #7  
Old 09-08-2005, 02:07 PM
the-copy the-copy is offline
Regular
 
Join Date: Mar 2004
Posts: 50
Default

Quote:
Originally Posted by trinkus79
What am I looking for exactly? This is what I did
Code:
Dim icnt As Integer Dim StartUpParams As String() = Environment.GetCommandLineArgs For icnt = 0 To UBound(StartUpParams) MessageBox.Show(StartUpParams(icnt), "start up", MessageBoxButtons.OK) Next
when I double click a program associated with my program I get a this:
one msg box that says:
"C:\blahblahblah\productionrequestform.exe
then another that says:
"C:\blahblahbla\filename.prs
...what do I do next? I'm self taught in VB and I don't think any of the books I've read really cover this nifty stuff either that or I missed it
Now you got the PATH to the prs-file, load it in to your program
Reply With Quote
  #8  
Old 09-08-2005, 02:26 PM
trinkus79's Avatar
trinkus79 trinkus79 is offline
Centurion
 
Join Date: Jan 2004
Location: Portland, OR
Posts: 161
Default

It's prolly easy, I prolly know how, but I just can't think of it....how do I do that?
I have an open sub in my program that I can use to open the .prs files. How to do tell my program to open that file when you double click it?

I know I'm prolly going to feel dumb once you say it....that's usually the way it goes
Reply With Quote
  #9  
Old 09-08-2005, 05:03 PM
wayneph's Avatar
waynephOpening a file in my program wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

Well, since the second value in StartUpParams is your file name, I'd just call your OpenFile sub with the StartUpParams(1) as the parameter.

(If your current OpenFile sub shows the File Dialog to select the file, I would separate the code that does the acutal "work" of opening the file out to another sub. Then you can call the same code when a file is double clicked, and when the user selects it from the Open Dialog.)
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #10  
Old 09-08-2005, 05:16 PM
trinkus79's Avatar
trinkus79 trinkus79 is offline
Centurion
 
Join Date: Jan 2004
Location: Portland, OR
Posts: 161
Default

lol, I was just trying that as you replyed.....only problem is I get:
"An unhandled exception of type 'System.IndexOutOfRangeException' occurred in Production Request Form.exe

Additional information: Index was outside the bounds of the array."

from my code of:
Code:
Dim StartUpParams() As String = Environment.GetCommandLineArgs If StartUpParams(1) = "" Then ...blah blah blah Else Open_FromFile(StartUpParams(1)) End If

not sure what's wrong...
Reply With Quote
  #11  
Old 09-08-2005, 05:25 PM
sgt_pinky's Avatar
sgt_pinky sgt_pinky is offline
Contributor
 
Join Date: Feb 2004
Location: Melbourne, Australia
Posts: 633
Default

Yeah, because there is no StartUpParams(1) if you just double click on your program (ie, not double click on a *.prs file). You have to check the length of StartUpParams:

Code:
If Not StartUpParams Is Nothing Then If StartUpParams.GetUpperBound(0) > 0 Then Open_FromFile(StartUpParams(1)) End If End If

And with your RichTextBox, consider using the inbuilt functions to save and load files:

Code:
RichTextBox1.LoadFile() RichTextBox1.SaveFile()

Last edited by sgt_pinky; 09-08-2005 at 05:34 PM.
Reply With Quote
  #12  
Old 09-08-2005, 05:53 PM
trinkus79's Avatar
trinkus79 trinkus79 is offline
Centurion
 
Join Date: Jan 2004
Location: Portland, OR
Posts: 161
Default

Got the open part....
in my form load I have:
Code:
Dim StartUpParams() As String = Environment.GetCommandLineArgs If UBound(StartUpParams) <= 0 Then 'initialize stuff here Else Open_FromFile(StartUpParams(1)) End If

seems to be working great, thanks for the tips guys.

pinky: I thought of doing that, but the way I save and open my forms is by saving each textbox, checkbox value, or radiobutton value from the form to its own line in a textbox. I only want to have one file when they save, so they can email it to me or save it to a disk. Is there a way I can use the rtf.savefile/openfile to add it to that one file with out it overwriting everything? or is there a way I can replace the 'vbCrLf' with some random string like "HHHGGG" then just put the vbCrLF back in when I open the file?
Reply With Quote
  #13  
Old 09-08-2005, 06:42 PM
sgt_pinky's Avatar
sgt_pinky sgt_pinky is offline
Contributor
 
Join Date: Feb 2004
Location: Melbourne, Australia
Posts: 633
Default

Yeah, I'm not too sure what you mean. Post a bit of code. Also, in .NET you should really use StartUpParams.GetUpperBound(0) instead of UBound(StartUpParams). And you should use ControlChars.CrLf instead of the old vb6 vbCrLf.
Reply With Quote
  #14  
Old 09-09-2005, 10:27 AM
trinkus79's Avatar
trinkus79 trinkus79 is offline
Centurion
 
Join Date: Jan 2004
Location: Portland, OR
Posts: 161
Post

Thanks for the tips!
and now...
Here is my Save code
Code:
Dim strSpecial As String strSpecial = Me.rtfInstructions.Text With SFD1 .DefaultExt = ".prs" .Filter = "Production Request Sheet|*.prs|All Files|*.*" .InitialDirectory = "C:\BWFsheets\Production_Sheets" .AddExtension = True .Title = "Production Request Sheet - Save" End With If txtReporter.Text = "" Then MessageBox.Show("Please enter your name", MsgBoxStyle.OKOnly, "Reporter Name") txtReporter.SelectAll() Exit Sub Else If System.IO.Directory.Exists("C:\BWFSheets\Production_Sheets") = False Then System.IO.Directory.CreateDirectory("C:\BWFSheets\Production_Sheets") End If If SFD1.ShowDialog = DialogResult.OK Then Dim fsStream As New FileStream(SFD1.FileName, FileMode.Create, FileAccess.ReadWrite) Dim swWriter As New StreamWriter(fsStream) Try swWriter.WriteLine(dteDateNeeded.Text) swWriter.WriteLine(lstTimeBy.SelectedItem) swWriter.WriteLine(optam.Checked) swWriter.WriteLine(optpm.Checked) swWriter.WriteLine(txtReporter.Text) swWriter.WriteLine(chkOriginalTR.Checked) swWriter.WriteLine(txtTranscriptCopies.Text) swWriter.WriteLine(chkOriginalWL.Checked) swWriter.WriteLine(txtWLCopies.Text) swWriter.WriteLine(chkOriginalMT.Checked) swWriter.WriteLine(txtMTCopies.Text) swWriter.WriteLine(optSingle.Checked) swWriter.WriteLine(optDouble.Checked) swWriter.WriteLine(txtASCIICopies.Text) swWriter.WriteLine(txtCDCopies.Text) swWriter.WriteLine(strSpecial) '<----- swWriter.WriteLine(txtFileName1.Text) '*** more of the same here *** swWriter.WriteLine(chkAscii1.Checked) swWriter.WriteLine(chkAscii2.Checked) swWriter.WriteLine(chkAscii3.Checked) swWriter.WriteLine(chkAscii4.Checked) swWriter.WriteLine(chkEtran1.Checked) swWriter.WriteLine(chkEtran2.Checked) swWriter.WriteLine(chkEtran3.Checked) swWriter.WriteLine(chkEtran4.Checked) swWriter.Close() MessageBox.Show("File Saved in: " & SFD1.FileName, MsgBoxStyle.OKOnly, "SAVED TEXT") Catch ex As IOException MessageBox.Show(ex.Message) End Try End If End If
and here is my code for opening.This is the 'open' from the menu, I have separate code for opening when the user double clicks the file.I just haven't gotten around to condensing it yet
Code:
Dim arrInput(60) As String Dim intInCnt With OFD1 .DefaultExt = ".prs" .Filter = "Production Request Sheet|*.prs|All Files|*.*" .InitialDirectory = "A:\" .AddExtension = True .Title = "Production Request Sheet Open File" End With If OFD1.ShowDialog = DialogResult.OK Then Dim fsStream As New FileStream(OFD1.FileName, FileMode.Open, FileAccess.Read) Dim srReader As New StreamReader(fsStream) Try While srReader.Peek > -1 For intInCnt = 0 To 60 arrInput(intInCnt) = srReader.ReadLine() Next Me.dteDateNeeded.Value = arrInput(0) Me.lstTimeBy.SelectedItem = arrInput(1) Me.optam.Checked = arrInput(2) Me.optpm.Checked = arrInput(3) Me.txtReporter.Text = arrInput(4) Me.chkOriginalTR.Checked = arrInput(5) Me.txtTranscriptCopies.Text = arrInput(6) Me.chkOriginalWL.Checked = arrInput(7) Me.txtWLCopies.Text = arrInput(8) Me.chkOriginalMT.Checked = arrInput(9) Me.txtMTCopies.Text = arrInput(10) Me.optSingle.Checked = arrInput(11) Me.optDouble.Checked = arrInput(12) Me.txtASCIICopies.Text = arrInput(13) Me.txtCDCopies.Text = arrInput(14) Me.rtfInstructions.Text = arrInput(15) '<---- Me.txtFileName1.Text = arrInput(16) '*** more of the same here *** Me.chkEtran1.Checked = arrInput(57) Me.chkEtran2.Checked = arrInput(58) Me.chkEtran3.Checked = arrInput(59) Me.chkEtran4.Checked = arrInput(60) End While srReader.Close() Me.Refresh() MessageBox.Show("File Open Successful", MsgBoxStyle.OKOnly, "Production Request Sheet Open") Catch ex As Exception MessageBox.Show(ex.Message) End Try End If
the rftInstructions.text is what I'm having trouble with. If the user types something in, then hits the enter key and types more stuff,it throws off the rest of the variables.
Oh, and if there are any other suggestions you would like to make, feel free, I'm always up for learning 'the right way' or 'another way' to do something.

thanks!
Reply With Quote
  #15  
Old 09-09-2005, 06:42 PM
sgt_pinky's Avatar
sgt_pinky sgt_pinky is offline
Contributor
 
Join Date: Feb 2004
Location: Melbourne, Australia
Posts: 633
Default

This bit intrigues me:

Code:
If txtReporter.Text = "" Then MessageBox.Show("Please enter your name", MsgBoxStyle.OKOnly, "Reporter Name") txtReporter.SelectAll() Exit Sub Else 'blah blah End If

You have said that if the textbox is empty, show a message box, and then select all the text in that textbox. Why would you select all the text in that textbox, when you know already that it's empty!!! :P

Also, wayneph wrote an excellent little starter to using serialisation in .NET. This would probably be an interesting next step for you, providing you with a slightly more secure way to save your data (in binary form). wayneph's tutorial on serialisation

Serialisation is spelt with an 's' in Australia. I don't know why, but everyone thinks we drive in miles/h here *NO! km/h!*, and that we spell things with a 'z' *NO!*. And we measure things in centimeters and meters, not inches and feet! That's my short history lesson on Australia.

When I am coding though, I always use the american spelling. So I would write 'Dim BackgroundColor As Color'. Not, 'Dim BackgroundColour As Color', because it gets really confusing otherwise.
Reply With Quote
  #16  
Old 09-09-2005, 06:45 PM
sgt_pinky's Avatar
sgt_pinky sgt_pinky is offline
Contributor
 
Join Date: Feb 2004
Location: Melbourne, Australia
Posts: 633
Default

Also, you can shorten this bit:

Code:
Dim fsStream As New FileStream(OFD1.FileName, FileMode.Open, FileAccess.Read) Dim srReader As New StreamReader(fsStream)

To:

Code:
Dim srReader As New StreamReader(OFD1.FileName)

You really only use two lines like you have there if you want to perform a very specific type of file I/O - which in your case, you don't.
Reply With Quote
  #17  
Old 09-12-2005, 11:11 AM
trinkus79's Avatar
trinkus79 trinkus79 is offline
Centurion
 
Join Date: Jan 2004
Location: Portland, OR
Posts: 161
Default

yeah, that selectall thing is a leftover....I had some error checking code in there, but then I decided that I didn't care if they spelled thier name wrong guess I just missed that line....

and I'll check out the serialisation thing too.. Do you think that could fix my problem with haveing a "CrLf" in the textbox?

I don't really care if they can open it in notepad and edit it...I have that file type linked to my program, and sad to say, most of the people that work here wouldn't be able to figure out how to open the file in something else
Reply With Quote
  #18  
Old 09-12-2005, 11:35 AM
wayneph's Avatar
waynephOpening a file in my program wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

Yes, Serialization would take care of your CrLf problem. Although I definately like the option (slightly biased author ), you could always just use a replace when you're writing to the file.

Anything that has a CrLf (Environment.NewLine) character replace it with something like "---" that will keep it on one line in your text file. Then when you display it back to the Text box, replace "---" with the original characters. Not quite as elegant, but it works pretty well with what you already have. If you go this route, just make sure that you choose something that doesn't end up in the string. (not many people use 3 hypens next to each other.)
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
  #19  
Old 09-12-2005, 04:57 PM
trinkus79's Avatar
trinkus79 trinkus79 is offline
Centurion
 
Join Date: Jan 2004
Location: Portland, OR
Posts: 161
Default

ok,I tried to do....
Code:
Dim strSpecial As String strSpecial = Me.rtfInstructions.Text strSpecial = strSpecial.Replace(Environment.NewLine.ToString, "-**-")
but its still saving the newline to the textfile...
Kinda strange, but I guess it's not the save part thats messing me up, cuse even though there are 'newline' chars in there, it's all on one line, it must be the open that is messin me up....
Reply With Quote
  #20  
Old 09-12-2005, 06:52 PM
wayneph's Avatar
waynephOpening a file in my program wayneph is offline
Web Junkie

Retired Moderator
* Expert *
 
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393
Default

Quote:
Originally Posted by trinkus79
cuse even though there are 'newline' chars in there, it's all on one line
HUH? what do you mean by there are newline chars in there? Either it is split on multiple lines (with newline chars) or it is on one line with something else.

Can you attach a copy of your file (as a .txt) so that we can see it?
__________________
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Opening a file in my program
Opening a file in my program
Opening a file in my program Opening a file in my program
Opening a file in my program
Opening a file in my program
Opening a file in my program Opening a file in my program Opening a file in my program Opening a file in my program Opening a file in my program Opening a file in my program Opening a file in my program
Opening a file in my program
Opening a file in my program
 
Opening a file in my program
Opening a file in my program
 
-->