Results 1 to 11 of 11

Thread: Create A File Through VB.NET

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2002
    Posts
    41

    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.

  2. #2

    Thread Starter
    Member
    Join Date
    Aug 2002
    Posts
    41
    *BUMP*

  3. #3
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    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.

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2002
    Posts
    41
    i did, but file.open doesnt work

    is there any other way?

  5. #5
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    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.

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2002
    Posts
    41
    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

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. 'if you are just going to rename it then you might as well just call it .doc in the first place
    2.         'renaming it doesn't format it any different
    3.         Dim fs As System.IO.FileStream = New System.IO.FileStream("c:\hello.doc", System.IO.FileMode.OpenOrCreate)
    4.         Dim sw As New System.IO.StreamWriter(fs)
    5.         sw.WriteLine("Write a line of text here.")
    6.         sw.Close()
    7.         fs.Close()
    8.  
    9.         'to start it in the associated program
    10.         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:
    1. 'imports at top
    2.         Imports System.IO
    3.         Imports System.Diagnostics
    4.  
    5.         'if you are just going to rename it then you might as well just call it .doc in the first place
    6.         'renaming it doesn't format it any different
    7.         Dim fs As FileStream = New FileStream("c:\hello.doc", FileMode.OpenOrCreate)
    8.         Dim sw As New StreamWriter(fs)
    9.         sw.WriteLine("Write a line of text here.")
    10.         sw.Close()
    11.         fs.Close()
    12.  
    13.         'to start it in the associated program
    14.         Process.Start("c:\hello.doc")
    Last edited by Edneeis; Sep 23rd, 2002 at 11:32 AM.

  8. #8

    Thread Starter
    Member
    Join Date
    Aug 2002
    Posts
    41
    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

  9. #9
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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:
    1. 'Dim fs As System.IO.FileStream = New System.IO.FileStream("c:\hello.doc", System.IO.FileMode.OpenOrCreate)
    2.         Dim fs As System.IO.FileStream = File.Open("c:\hello.doc", System.IO.FileMode.OpenOrCreate)
    3.         Dim sw As New System.IO.StreamWriter(fs)
    4.         sw.WriteLine("Write a line of text here.")
    5.         sw.Close()
    6.         fs.Close()
    7.  
    8.         'to start it in the associated program
    9.         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.

  10. #10

    Thread Starter
    Member
    Join Date
    Aug 2002
    Posts
    41
    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

  11. #11

    Thread Starter
    Member
    Join Date
    Aug 2002
    Posts
    41
    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
  •  



Click Here to Expand Forum to Full Width