Results 1 to 15 of 15

Thread: File

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    File

    Hi...im just starting out with programming and am getting a bit stuck.

    ive got my form working in such a way that what i enter in a textbox can be saved as a ".txt" file - which is perfect

    however, to make the naming of the file as straightforward for the user as possible, im trying to code it so they have don't have to manually input filepaths which can often turn out to be tricky.

    so at the moment, i have a folder browser which puts the selected path into a text box.

    next, i have a textbox where the user can enter the name they want for the file... eg "ITEM 1"

    then, i have coded the following:

    "tbMix.Text = tbFileName.Text & "\" & tbScriptName.Text & ".txt""

    tbMix is a textbox which collects from tbFileName (the selected path) and tbScriptName (where the user has entered "ITEM 1") and also puts in a "\" to ensure the path is accepted, plus ".txt" to save in the correct format.

    when i run this code, the information is all collected correctly in the right textbox, but when i click save, i get "illegal characters in path" as an error.

    worth mentioning -- when i manually type the file path, it saves fine..

    thanks for your help!

    M




    [[Code for the "save" button:

    Private Sub ConfirmSaveToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConfirmSaveToolStripMenuItem.Click
    Dim objwriter As New System.IO.StreamWriter(tbMix.Text)
    objwriter.Write(tbUpperBody.Text)
    objwriter.Close()
    End Sub

    ]]]

  2. #2
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: File

    If the selected path contains a \ at the end, and you also add a \ then you have two.

    Use the IO.Path.Combine for this job instead, it deals with it for you:

    Code:
    tbMix.Text = IO.Path.Combine(tbFileName.Text , tbScriptName.Text & ".txt")

    PS: I can't quite grasp what you are trying to do, but the My.Settings is a great place to store info through application restarts, and its in XML making it easy to manipulate.

    PPS: Ensure that the filename and path are valid. If you believe they are, post here exactly the contents of the textbox before you open it.

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: File

    Try the following which adds the backslash in for you and appends the extension to the file name for you.

    Code:
    tbMix.Text = IO.Path.Combine(tbFileName.Text, _
                 IO.Path.ChangeExtension(tbScriptName.Text, ".txt"))

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    Re: File

    Hi again

    thanks for your quick replies suggestions...I still seem to be getting the same error message "illegal characters in path" trying both ... :S

    The filename and path are valid - been trying to save the files to the desktop to make sure of that.

    I just tried something interesting with my original code...it was putting all the information into the right place, but the error was occurring on clicking save...I have just copy and pasted the filepath out of the textbox which determines the name of the file to be saved eg "C:\User\MyName\Desktop\trial.txt" and when i paste into word, there is a line break appearing..eg

    "C:\Users\MyName\Desktop\
    trial.txt"

    Could that be the cause of the error? if so...how do i get rid of that line break? This is the original
    code:
    tbMix.Text = tbFileName.Text & "\" & tbScriptName.Text & ".txt"

    Thanks again for your help...

  5. #5
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: File

    Yes, that would be the error, the reason I suggested you copy it, things are not always as they seem. I would guess you are taking the text from a textbox that allows multiline?

    Here is a simple way to remove non-allowed characters, I extracted it from an existing module, so might need to check it yourself.

    Code:
            Dim FileName As String = IO.Path.Combine(tbFileName.Text, _
                 IO.Path.ChangeExtension(tbScriptName.Text, ".txt"))
            For Each chrInvChar As Char In IO.Path.GetInvalidPathChars
                FileName = FileName.Replace(chrInvChar, "")
            Next
            tbMix.Text = FileName
    Last edited by Grimfort; Aug 10th, 2011 at 08:39 AM.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    Re: File

    the textbox multiline property is false at the moment. :S

  7. #7
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: File

    I don't supose you are loading the value of tbFileName from a text file which contains the line breaks? Either way, the code above should give you a decent filename.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    Re: File

    thanks again...still not working unfortunately...

    where about in my own code should i insert the stuff you gave me?

    tbFileName is being loaded from a folder browser dialogue

  9. #9
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: File

    Another idea is to put an OpenFileDialog on your form, set CheckFileExists to False and allow them to type the file name in the file name textbox of the dialog.

    Code:
    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        Console.WriteLine(OpenFileDialog1.FileName)
    End If
    If you want them to always start in a specific folder i.e. the desktop for example you could set the InitialDirectory property of the dialog.
    Code:
    OpenFileDialog1.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop)

  10. #10
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: File

    Quote Originally Posted by MacShand View Post
    thanks again...still not working unfortunately...

    where about in my own code should i insert the stuff you gave me?

    tbFileName is being loaded from a folder browser dialogue
    Sorry, I ment the tbScript. That code should go straight in where you had your first line. There is no reason it should fail with an illegal characters error. Are you sure thats what it still says and has not changed to say a security exception? Copy and paste out the tbMix.Text value and the exact exception you get.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    Re: File

    tbScript is a manual entry by the user when the application is running...

    here's the error window i get when i run it...
    Attached Images Attached Images  

  12. #12
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,780

    Re: File

    Ahh, K. I would not have thought that those methods would even check for those chars, but hey hum. It could be either the Combine, or the ChangeExtension.
    Somehow you are getting a CrLf into that tbScript name, this should fix it, but better off working out how it got there first.

    I am new to these predicates, so if anyone can write it better please do, but heres one!

    Code:
    tbMix.Text = IO.Path.Combine(tbFileName.Text, _
                 IO.Path.ChangeExtension(String.Join("", tbScriptName.Text.Where(Function(TheChar As Char, TakeVar As Boolean) _
                   Not IO.Path.GetInvalidPathChars.Contains(TheChar)).ToList), ".txt"))

  13. #13
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: File

    Could it be this simple?
    Code:
    Imports System.Text.RegularExpressions
    . . .
    Private Sub Test()
        Dim Value As String = String.Concat("1  2   One~Two", Environment.NewLine, "!!!More\ here /-Three")
        Dim tester = Regex.Replace(Value, "[^\w\.@-]", "")
        Console.WriteLine("[{0}]", tester)
    End Sub
    Code:
    [12OneTwoMorehere-Three]

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    Re: File

    Awesome! Got it!

    Thank you so much for all your help....brilliant!

  15. #15
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: File

    Quote Originally Posted by MacShand View Post
    Awesome! Got it!

    Thank you so much for all your help....brilliant!
    Good to hear you are satisfied. Please mark this thread resolved.

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