|
-
Sep 22nd, 2002, 12:43 AM
#1
Thread Starter
Member
Create A File Through VB.NET
i have a button, and when clicked, i want it to make a file "c:\hello.txt"
then rename it to "hello.doc"
and finally, run it
thanks
Last edited by h3adbang3r; Sep 23rd, 2002 at 06:27 AM.
-
Sep 23rd, 2002, 06:27 AM
#2
Thread Starter
Member
-
Sep 23rd, 2002, 06:30 AM
#3
Fanatic Member
You probably want to look into the File object and its associated methods:
ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemiofileclasstopic.htm
Copious examples are off that page.
-
Sep 23rd, 2002, 06:36 AM
#4
Thread Starter
Member
i did, but file.open doesnt work
is there any other way?
-
Sep 23rd, 2002, 08:26 AM
#5
Fanatic Member
Not sure what you should do if the cut-and-paste examples in the help don't work. They do for me Take a closer look at the examples.
Edit: Woops, I didn't point you to any cut/paste demos did I. Here ya go.
ms-help://MS.VSCC/MS.MSDNVS/vblr7/html/vastmopen.htm
ms-help://MS.VSCC/MS.MSDNVS/vblr7/html/vafctclose.htm
ms-help://MS.VSCC/MS.MSDNVS/vbcn7/html/vbconProcessingFiles.htm
Last edited by Slow_Learner; Sep 23rd, 2002 at 08:30 AM.
-
Sep 23rd, 2002, 09:12 AM
#6
Thread Starter
Member
ill try it as soon as i get home
but.. does anyone know why "file.open" doesnt work? because i really need to use this
thanks
-
Sep 23rd, 2002, 11:19 AM
#7
I didn't check out the links but what do you want to put in the file?
You can use a filestream for most anything:
VB Code:
'if you are just going to rename it then you might as well just call it .doc in the first place
'renaming it doesn't format it any different
Dim fs As System.IO.FileStream = New System.IO.FileStream("c:\hello.doc", System.IO.FileMode.OpenOrCreate)
Dim sw As New System.IO.StreamWriter(fs)
sw.WriteLine("Write a line of text here.")
sw.Close()
fs.Close()
'to start it in the associated program
System.Diagnostics.Process.Start("c:\hello.doc")
Or if you want to save some typing you can add a couple of imports at the top above the form class and then write it like this:
VB Code:
'imports at top
Imports System.IO
Imports System.Diagnostics
'if you are just going to rename it then you might as well just call it .doc in the first place
'renaming it doesn't format it any different
Dim fs As FileStream = New FileStream("c:\hello.doc", FileMode.OpenOrCreate)
Dim sw As New StreamWriter(fs)
sw.WriteLine("Write a line of text here.")
sw.Close()
fs.Close()
'to start it in the associated program
Process.Start("c:\hello.doc")
Last edited by Edneeis; Sep 23rd, 2002 at 11:32 AM.
-
Sep 23rd, 2002, 05:01 PM
#8
Thread Starter
Member
ok, let me reexplain this
when button 1 is pressed, make a file "c:\hello.txt"
then print this text in the file:
" line 1
line 2
line3"
and thats it
i have no idea why i cant use "File.Open"
maybe someone can tell me why?
thanks again
-
Sep 23rd, 2002, 06:27 PM
#9
Ok if you like the File.Open function so much then you can use it but it does the samething as the constructor I used for the filestream. Notice the return value in the IDE intellisense.
VB Code:
'Dim fs As System.IO.FileStream = New System.IO.FileStream("c:\hello.doc", System.IO.FileMode.OpenOrCreate)
Dim fs As System.IO.FileStream = File.Open("c:\hello.doc", System.IO.FileMode.OpenOrCreate)
Dim sw As New System.IO.StreamWriter(fs)
sw.WriteLine("Write a line of text here.")
sw.Close()
fs.Close()
'to start it in the associated program
System.Diagnostics.Process.Start("c:\hello.doc")
Working with files got a bit of a work over in .NET but trust me its much better this way.
-
Sep 23rd, 2002, 06:44 PM
#10
Thread Starter
Member
its not that i like file.open so much, its that i dont see any other way of doing it
btw.. it still says file is not declared
-
Sep 23rd, 2002, 06:52 PM
#11
Thread Starter
Member
WOOHOO
nm the question
i figured out why file.open doesnt work 
thanks everyone
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|