How would i over write a file?
Printable View
How would i over write a file?
Delete it then write to same file name?
When you open it for writing you either use a method that defaults to overwriting or else specify an argument that forces overwriting. For instance, creating a StreamWriter will default to overwriting an existing file while creating a FileStream requires you to specify whether to overwrite or append.
are you trying to copy a file or write to a file? what are you trying to do?
Oh, Sorry... Im trying do a little project which is a file shredder. Im trying to over write a file with "nop", but i have no clue where to start.
So try something like this(put in the path and name of the file for the Path variable like C:\MyFolder\MyFile):
vb Code:
Imports System.IO Private Sub MyWriter() Try Dim PathOfFile As String = "Whatever you want" Dim Writer As New StreamWriter(PathOfFile, False) ' Writing to the file Writer.Write("nop") 'Close StreamWriter Writer.Close() Catch a As Exception MsgBox("error") End Try End Sub
Ask if you have any questions!
I suggest that, in future, you spend a bit more time on your first post and provide a full and clear description from the start. It will save everyone some time.
In theory, you can just open a FileStream and write the same number of Bytes as the file already contains. They could be all 0, all 1 or random values. In practice, I'm not 100% sure that that will actually write to the exact same location on disc as the existing data. You'd have to confirm that. If not then you'd probably have to resort to a low-level API.