|
-
May 27th, 2003, 10:22 AM
#1
Thread Starter
Addicted Member
Inserting text at the beginning of a text file
Ok,
Todays questions of the day is two fold.
1) I need to insert a line of text into the begining of an already existing document but Append of course puts it at the end, how do you get it at the beginning.
2) My code is below for the problem I am having with #1. What is the .NET way of doing what I am trying to do?
VB Code:
Private Sub Modify_Logon_Script()
Dim logonscript As String
Dim logonscript_template As String
' Defines the logonscript and template.
logonscript_template = "logon.template"
logonscript = "data\logon.vbs"
' Grabs the share path from the text box and sets up the variable
strSharePath = ("strShareName = " & """" & txtSharePath.Text & """")
' Copies the template for the logon script to the data directory
FileCopy(strPath & "xrxlogon.template", strDataPath & "xrxlogon.vbs")
' Opens the logon script for editing
FileOpen(5, logonscript, OpenMode.Append)
' Writes the share path to the logon script
Print(5, strSharePath)
' Closes the logon script
FileClose(5)
End Sub ' Modifies the logon script with the correct path name
-
May 27th, 2003, 01:36 PM
#2
Thread Starter
Addicted Member
-
May 27th, 2003, 01:53 PM
#3
Where are strPath and strSharePath coming from? I see them in the method but not declared in it?
-
May 27th, 2003, 01:57 PM
#4
Lively Member
The only way I know of to add data to the beginning of a file is to open a _new_ file, write the new data, then read each line of the old file and write it to the new file. When you are done, you can erase the old file and rename the new file with the appropriate name.
-
May 27th, 2003, 02:00 PM
#5
This will insert text in the front of a file:
VB Code:
Dim sr As New IO.StreamReader(New IO.FileStream("c:\end.txt", IO.FileMode.Open))
Dim text As String = sr.ReadToEnd
sr.Close()
Dim sw As New IO.StreamWriter(New IO.FileStream("c:\end.txt", IO.FileMode.Open))
sw.WriteLine("Inserted in the front!")
sw.Write(text)
sw.Close()
-
May 27th, 2003, 02:18 PM
#6
Lively Member
That will work, but if you have an error while writing you won't be able to repeat the attempt later becuase you will have overwritten the original file I would suggest writing to a new file and killing the old only after the write is complete.
-
May 27th, 2003, 02:27 PM
#7
If it fails then the contents of the file are in the text variable. Also depending on what the error is the contents may not be lost.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|