Cannot write a text message in Text File using WP7 - vb.net

I cannot write a file in a textfile in my wp7 application, what should I do?
here is my code
Dim isolatedStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication
Dim isoStream As IsolatedStorageFileStream = New IsolatedStorageFileStream("TextFile1.txt", FileMode.Append, isolatedStorage)
Dim streamwriter As StreamWriter = New StreamWriter(isoStream)
streamwriter.WriteLine("sample")
streamwriter.Close()
I also try to put it inside the Using statement, but it is still now writing in the text file
Dim isolatedStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication
Using isoStream As IsolatedStorageFileStream = New IsolatedStorageFileStream("/TextFile1.txt", FileMode.Append, isolatedStorage)
Using streamwriter As StreamWriter = New StreamWriter(isoStream)
streamwriter.WriteLine("PreMenu.xaml")
streamwriter.Close()
End Using
End Using
there is no error, but it is not writing a text in textfile, what should I do?

Related

if else for run file and stream file generate vb.net

How to First Run File seconds stream file in text box via if and else
If Process.Start("test.bat") Then
Dim address As String = "id.txt"
Dim client As WebClient = New WebClient()
Dim reader As StreamReader = New StreamReader(client.OpenRead(address))
TextBox1.Text = reader.ReadToEnd
End If
Note: text file generated via batch file.
Try
Dim proc = Process.Start("test.bat")
proc.WaitForExit()
Using client As New WebClient()
TextBox1.Text = client.DownloadString(File.ReadAllText("id.txt"))
End Using
Catch
End Try

How do I reload the formatted text into RTB?

I have a RTB in VB.NET, and put an event handler to save the formatted text into a file after encrypting it. However, I can't figure out how to reload the formatting - when I open it, it shows the symbols of formatting instead of formatted text. Here's my code:
Dim FileName As String = TextBox1.Text
File.Delete(FileName)
Dim EncryptElement As New TripleDESCryptoServiceProvider
EncryptElement.Key = {AscW("B"c), AscW("A"c), AscW("1"c), AscW("R"c), AscW("3"c), AscW("9"c), AscW("G"c), AscW("V"c), AscW("5"c), AscW("S"c), AscW("P"c), AscW("0"c), AscW("L"c), AscW("Z"c), AscW("4"c), AscW("M"c)} '128 bit Key
EncryptElement.IV = {AscW("N"c), AscW("B"c), AscW("5"c), AscW("3"c), AscW("G"c), AscW("L"c), AscW("2"c), AscW("Q"c)} ' 64 bit Initialization Vector
Dim fStream As FileStream = File.Open(FileName, FileMode.OpenOrCreate)
Dim cStream As New CryptoStream(fStream, New TripleDESCryptoServiceProvider().CreateEncryptor(EncryptElement.Key, EncryptElement.IV), CryptoStreamMode.Write)
Dim sWriter As New StreamWriter(cStream)
sWriter.WriteLine(RichTextBox1.Rtf)
sWriter.Close()
cStream.Close()
fStream.Close()
The above code is for saving, and the below code is for opening.
Dim FileName As String = TextBox1.Text
Dim DecryptElement As New TripleDESCryptoServiceProvider
DecryptElement.Key = {AscW("B"c), AscW("A"c), AscW("1"c), AscW("R"c), AscW("3"c), AscW("9"c), AscW("G"c), AscW("V"c), AscW("5"c), AscW("S"c), AscW("P"c), AscW("0"c), AscW("L"c), AscW("Z"c), AscW("4"c), AscW("M"c)}
DecryptElement.IV = {AscW("N"c), AscW("B"c), AscW("5"c), AscW("3"c), AscW("G"c), AscW("L"c), AscW("2"c), AscW("Q"c)}
Dim fStream As FileStream = File.Open(FileName, FileMode.OpenOrCreate)
Dim cStream As New CryptoStream(fStream, New TripleDESCryptoServiceProvider().CreateDecryptor(DecryptElement.Key, DecryptElement.IV), CryptoStreamMode.Read)
Dim sReader As New StreamReader(cStream)
Dim DecryptedData As String = ""
DecryptedData = sReader.ReadToEnd
RichTextBox1.AppendText(DecryptedData)
RichTextBox1.Enabled = True
Button1.Text = "OK"
sReader.Close()
cStream.Close()
fStream.Close()
Where is the problem?
You need RichTextBox1.SaveFile(SomeStream, RichTextBoxStreamType) I think StreamWriter is stuffing it up.
Oh and seeing as you just gave the world the keys for your encryption you might want to come up with a new one...
Added after comment not proven though.
Replace these two lines in your save routine
Dim sWriter As New StreamWriter(cStream)
sWriter.WriteLine(RichTextBox1.Rtf)
with
RichTextBox1.SaveFile(cStream,RichTextBoxStreamType.RichText);
and get rid of swriter.Close()
I think.

Search for word in text file and continue reading from there vb.net

As the title say i would like to search a text file for a specific word and then start reading the text from that point forth.
A code snippet is the following
Dim path As String = "c:\temp\test.txt"
Dim readall As String
Dim i As Integer
If File.Exists(path) Then
File.Delete(path)
End If
Dim sw As StreamWriter = New StreamWriter(path)
sw.WriteLine("Hello")
sw.WriteLine("i am here")
sw.WriteLine("to test")
sw.WriteLine("the class")
sw.Close()
Dim sr As StreamReader = New StreamReader(path)
readall = sr.ReadToEnd
sr.Close()
So right now i have read the file and stored it to string readall.
How can i write back to the file but now starting from the word "to"
For instance the outpout text file would be
to test
the class
I hope i made myself clear
Search readall for the first occurrence of "to" and store the rest of readall, starting with "to", to the variable stringToWrite:
Dim stringToWrite As String
stringToWrite = readall.Substring(IndexOf("to"))
Then write stringToWrite to file:
Dim sw As StreamWriter = New StreamWriter(path)
sw.write(stringToWrite)
sw.Close()

about text file in vb.net

I create text file and would like to write some content to that file, but I am getting an error in the stream writer statement.
-------
Dim fileLoc As String = "d:\sample1.txt"
Dim fs As FileStream = Nothing
If (Not File.Exists(fileLoc)) Then
fs = File.Create(fileLoc)
Else
File.Delete(fileLoc)
fs = File.Create(fileLoc)
End If
Using sw As StreamWriter = New StreamWriter(fileLoc)
--------
--------
some thing
----------
----------
sw.writeline(phone)
---------
end using
The error I get is:
The process cannot access the file 'd:\sample1.txt' because it is
being used by another process.
It happens since you are deleting the file and soon creating it. The delete process is going on so it would allow you create the file.
Dim fs As FileStream = New FileStream(Application.StartupPath & "\Log\log.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite)
Dim s As StreamWriter = New StreamWriter(fs)
s.Close()
fs.Close()$
Hope this code helps you.

The process cannot access the file because it is being used by another process.-error while writing to a file in vb.net

in my vb.net code im using streamwriter to write to a file that has been given as input to the form.
Dim strContents As String
Dim objReader As StreamReader
Try
objReader = New StreamReader("C:\test.txt")
strContents = objReader.ReadToEnd()
objReader.Close()
Catch Ex As Exception
End Try
Dim Contents As String
Dim bAns As Boolean = False
Dim objWriter As StreamWriter
Dim FileStream As System.IO.FileStream
Try
FileStream = New FileStream("C:\test.txt", FileMode.Open, FileAccess.ReadWrite)
objWriter = New StreamWriter("C:\test.txt")
objWriter.Write("fdgdfgdjkljljklg")
objWriter.Close()
bAns = True
Catch Ex As Exception
End Try
in some of the systems its working fine but in some other workstations its showing error that mentioned in the subject.
What restricts the access to the file here?
can anyone help me on this?
What restricts the access to the file here?
The fact that you haven't disposed the stream and thus the process is locking the file. A StreamWriter is holding an unmanaged handle to the file. If you don't dispose this handle other threads/process cannot open the file.
I would recommend you to always wrap IDisposable resources in Using statement to ensure proper disposal (even if an exception is thrown inside the block):
Using filestream As New StreamWriter(Inifile, True, System.Text.Encoding.UTF8)
' ... use the filestream here to write to the file
End Using