How to create text file with a name base on a text box.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • apptech
    New Member
    • Dec 2008
    • 6

    How to create text file with a name base on a text box.

    I am writing a small application for my company. There are two text boxes and a submit button. textBox1 is where the information is entered to be write to the text file. TextBox2 is where the the user is suppose to enter the name of the file. Problem is I can't get get Textbox 2 to work. The file is created with the static name in the code but I can't figure out how to make the name it self using TextBox2. Where is my code for this section. As you can see the value entered into TextBox2 is assigned to the file name variable. I just don't know how to add static information to:
    StreamWriter sw = new StreamWriter(fi lename);

    Code:
            string filename;     
    
            filename = TextBox2.Text;
    
            StreamWriter sw = new StreamWriter(filename);
            sw.WriteLine(textBox1.Text);
            sw.Flush();
            sw.Close();
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    What do you mean by "static information"?

    You should just be able to grab text from the textbox the way you just did.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Have you considered using an OpenFileDialog control to retrieve the correct path/file name that the user wants to work with?

      Are you getting any error messages?
      Or are you just having problems retrieving the file info?

      Comment

      • apptech
        New Member
        • Dec 2008
        • 6

        #4
        path and extension

        I want the path the file will be uploaded to an the file extension to be static and they just type in the name of the file.

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Have you looked at using a masked text box?

          Comment

          • apptech
            New Member
            • Dec 2008
            • 6

            #6
            Not really

            No, How does that work?

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              Check it out :D

              -Frinny

              Comment

              • Curtis Rutland
                Recognized Expert Specialist
                • Apr 2008
                • 3264

                #8
                Or define the path and extension in code, and request the filename.
                Code:
                string path = @"c:\test\";
                string extension = ".txt";
                string fullpath = path + TextBox2.Text + extension

                Comment

                • apptech
                  New Member
                  • Dec 2008
                  • 6

                  #9
                  Thanks

                  Thanks insertAlias, that will work.

                  Comment

                  • tlhintoq
                    Recognized Expert Specialist
                    • Mar 2008
                    • 3532

                    #10
                    Originally posted by insertAlias
                    Or define the path and extension in code, and request the filename.
                    Code:
                    string path = @"c:\test\";
                    string extension = ".txt";
                    string fullpath = path + TextBox2.Text + extension
                    May I throw in a couple suggestions to 'beef up' the function for the real world?

                    Don't assume you have a C: drive. Silly as it might sound, it is possible to set up a PC where the boot drive is J: or some other letter. It doesn't cost anything to ask the OS for its SystemDrive and build your string from there.

                    Don't assume the directory hierarchy exists at write-time. It does no harm to make the directory again, just in case someone decided to do some hard drive clean up and deleted what was an empty directory at that time.

                    Don't assume you have rights to the directory you're writing to. What happens if someone changes the path to read-only or takes away your permissions to write to it? You could start writing a file that never gets created.

                    Don't assume the user only enters "Filename" in the text box. What if the user enters "Filename.t xt" in the text box? You don't need a file named "Filename.txt.t xt" because you blindly added '.txt' to whatever they entered.
                    Personally I like this construct because it fixes a bad extension and adds one if needed
                    Code:
                    string FileNameVariable = Path.ChangeExtension(FileNameVariable, ".txt");
                    And you probably want to parse the file name the user enters to strip out any illegal filename characters like ? * / and so on.

                    In short don't assume anything except that every condition you're expecting will be wrong, and that anything the user can do to foul up your program will be done.

                    Comment

                    Working...