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"
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
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.
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"
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.
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.
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.
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.
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"))
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