Results 1 to 5 of 5

Thread: using variable as the name of text file saved

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2009
    Posts
    16

    using variable as the name of text file saved

    I wonder if there is any way to make the file saved with a variable as its name. Any ideas? Please help!
    Chris Yiu
    VB 2008
    Core 2 Duo 3.0 Ghz
    4GB DDR2
    Geforce 9600 GT 1GB
    My CG blog: http://c12hriscg.blogspot.com/
    Email: [email protected]

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: using variable as the name of text file saved

    The name of a file is just a string. If you variable contains a string then its as good a choice as any other string. There's no functional difference between this:
    vb.net Code:
    1. IO.File.WriteAllText("MyFile.txt", "Hello World")
    and this:
    vb.net Code:
    1. Dim fileName As String = "MyFile.txt"
    2.  
    3. IO.File.WriteAllText(fileName, "Hello World")
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: using variable as the name of text file saved

    There is definitely a way - simply replace the text of the file name with your variable name by using the & operator to combine the strings together (as I assume you already have the directory path where this would be saved in another string).
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4

    Thread Starter
    Junior Member
    Join Date
    Apr 2009
    Posts
    16

    Re: using variable as the name of text file saved

    If I have a RichTextBox1, and I want to save the text inside into txt file with a variable file name. How to do it? I mean how to assign the rtbox1 for it to save? Thanks!
    Chris Yiu
    VB 2008
    Core 2 Duo 3.0 Ghz
    4GB DDR2
    Geforce 9600 GT 1GB
    My CG blog: http://c12hriscg.blogspot.com/
    Email: [email protected]

  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: using variable as the name of text file saved

    I mean how to assign the rtbox1 for it to save?
    Thats completely different to the original question but its the same kind of answer. Assign the text to a variable and then use the variable where you would have to otherwise type a literal string.
    Having said that, the RTB control has a SaveFile method which you can use in this specific case. Example:

    vb Code:
    1. RichTextBox1.SaveFile("C:\example.txt")
    So if you wanted to use a variable as the file name, you could do this (obviously this example just saves it in the root of the C drive but you get the idea) :
    vb Code:
    1. RichTextBox1.SaveFile("C:\" & MyVariable)
    Where obviously MyVariable is your variable that contains the file name.

    To do it more 'properly' you can use IO.Path.Combine as well, like so:
    vb Code:
    1. RichTextBox1.SaveFile(IO.Path.Combine("C:\", MyVariable))
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


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