I am writing a program that protects selected folders of your choice with a password. Going great so far, I was almost done, then I ran in to a problem.
The last thing that I need to complete is this:
I want the end user to be able to delete a directory. I am using a simple text file (that I change the extension to .dll for security purposes) to store the information of which directories to protect. The program reads from that file line by line to deterime which directories to protect.
There is a lot of code so I won't post it all, but I will post the section that I am working on.
Note, in the code that I have figured out how to match and find the line that I want to delete, I just don't know how to delete it.
VB Code:
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim fcontent() As String = Split(My.Computer.FileSystem.ReadAllText("C:\Program Files\Folder Protection\fld.dll"), vbCrLf) 'file from which to read text
For a As Integer = 0 To UBound(fcontent) 'reads the array of text lines
If String.Compare(fcontent(a), TextBox1.Text) = 0 Then 'compares the lines of text to the text in textbox1
I would recomend insead of using the text file withany extention which can be opened with texteditor use My.Settings to save the folders name's.
Back to your original question you can ues File.ReadAllLines to get a string array of the lines close the file and delete it and create a new file write all lines from the string array excluding the ones you need to delete and close the file.
Last edited by VBDT; Feb 5th, 2007 at 02:38 AM.
Rating is a way of saying thank you. Don't forget to rate always!
Thanks for the quick reply.
I understand how to read the lines of the file. But how do I rewrite the new file without the line that I don't want? I understand the concept of what you are describing, but what other commands or code do I use to do such a task.
Thanks, in advance for your help.
Read the file into an array (File.ReadAllLines). Load the array into a collection (New List(Of String)). Remove the lines you don't want (List.RemoveAt). Extract an array from the collection (List.ToArray). Write the array to the file (File.WriteAllLines).
Thanks for the quick reply.
I understand how to read the lines of the file. But how do I rewrite the new file without the line that I don't want? I understand the concept of what you are describing, but what other commands or code do I use to do such a task.
Thanks, in advance for your help.
Is there any particular reason you don’t want to use the application settings? It is more suitable I think! If there is then this is how you can do with the file. Just use this sub.
VB Code:
Private Sub RemoveFolders(ByVal fileName As String, ByVal folders() As String)
Dim lines() As String = IO.File.ReadAllLines(fileName)
Dim sw As New IO.StreamWriter(fileName)
For Each line As String In lines
If Array.IndexOf(folders, line) = -1 Then
sw.WriteLine(line)
End If
Next
sw.Close()
sw.Dispose()
End Sub
Rating is a way of saying thank you. Don't forget to rate always!
Again thanks for you help. I have another question, I've used your code and when I call on the sub, I'm not quite sure how to use it. Of course I know what the "fileName" is, I think(the path to the file). But I tried putting in the textbox1.text from where the folder path will be and it tells me:
"Value of type 'String' cannot be converted to '1-Dimensional array of String'.
I don't know what to do.
'Call the sub like this. Note the folders parameter is an array of strings so you
'can pass more than one folders to be removed. This call only removes one.
Me.RemoveFolders("file path", New String() {Me.TextBox1.Text})
To use the app Settings you should add a System.Collections.Specialized.StringCollection property with user scope like it is shown in the image. And use these lines of code to add and remove folder paths. Next time your app starts the folders will be accessible.
VB Code:
'Add folder
My.Settings.Folders.Add("folder path to add")
'Remove folder
My.Settings.Folders.Remove("folder path to remove")
Last edited by VBDT; Feb 5th, 2007 at 02:36 AM.
Rating is a way of saying thank you. Don't forget to rate always!
Ok that works(Me.RemoveFolders("file path", New String() {Me.TextBox1.Text})), except one thing. I need it to also remove the "vbCrLf" at the end. I tried a couple of things, like adding it to the end of the code above. It let me put it in there without errors, but it didn't work.
If this is in there it reads it as a blank string and it affects the operation of the whole program. Please help.
Thank you very much for your help so far, it is very appreciated.
I am not sure why you have it in there in a first place! It is a vb6 function and it is not a new line by its self; don’t use vb6 functions in .Net use "Environment.NewLine" or "ControlChars.NewLine" if you want new line to insert in to a string. Now, you don’t need to add new lines when you write in to file use “WriteLine” method of the stream writer like this:
VB Code:
'Write folder paths in a file.
Dim folders() As String = {"folder1", "folder2", "..."}
Using sw As New IO.StreamWriter(fileName)
For Each folder As String In folders
'This line writes a line in to
'the file with null at the end.
sw.WriteLine(folder)
Next
End Using
'Or if you want to append a folder to a file use this.
Using sw As New IO.StreamWriter(fileName, True)
'This line writes a line in to
'the file with null at the end.
sw.WriteLine(folder)
End Using
You shoulden't wory about newlins any more.
Last edited by VBDT; Feb 5th, 2007 at 02:45 PM.
Rating is a way of saying thank you. Don't forget to rate always!