Not sure how to use IsolatedStorage - vb.net

I want to create a notes app for Windows Phone 7 using Visual Basic. I have read a few tutorials but they are all suited for C# not VB. Basically, I have a main page and a page to add notes. Once the user types out a note on the add notes page, the title of that note appears on the main page. I also want the user to be able to select that title and it will display the note. I have done a bit of research and I know I will need to use isolated storage (not sure how to implement it in VB) to save the notes. I think I will also need a list box that will store the title of the notes. I am not asking for someone to just give me code, I am asking for some tutorials regarding this in VB or any pointers or general help on acheiving this. Thanks

All the code samples on MSDN are available in both C# and VB. See http://msdn.microsoft.com/en-us/library/ff431744(v=vs.92).aspx
The Model-View-ViewModel Sample (under Common Application Development Tasks) is probably a good place for you to start.
The link to download the VB code is http://go.microsoft.com/fwlink/?LinkId=229339

You are correct that you will need to use IsolatedStorage if you're wanting to write the notes to the phone (and not the cloud somewhere). Here is a link to a blog entry that has a class (in Visual Basic) that has some helper methods that will give you some similiar methods to VB.Net traditional methods (like ReadAllText, WriteAllText). It may be what you want for the file system reading/writing (but at a minimum will get you started with Isolated Storage).
http://www.blakepell.com/2012-03-07-wp7-file-helpers

Isolated Storages
Isolated storage is used to store local files such as text files, images, videos etc on the Windows Phone. Each app is assigned a specific isolated storage and is exclusive ONLY to that app. No other app is able to access your apps isolated storage.
A lot can be read from here All about Windows Phone Isolated Storage
Before you begin you will need to import IsolatedStorage to your project.
Imports System.IO
Imports System.IO.IsolatedStorage
Creating Folders
This creates a directory in your apps isolated storage. It will be called "NewFolder"
Dim myIsolatedStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
myIsolatedStorage.CreateDirectory("NewFolder")
You can create folders inside folders inside folders like so:
myIsolatedStorage.CreateDirectory("NewFolder/NewFolder1/NewFolder2/NewFolder3")
To delete a folder:
myIsolatedStorage.DeleteDirectory("NewFolder")
A good practice when creating and deleting folders is to add a Try Catch statement around the folder creation method so if there is an exception you or the user will be notified as to why it occurs e.g. the folder not existing therefore cannot be deleted or the folder existing therefore needing to be replaced etc. The example below shows a basic function in creating a folder with a Try Catch statement.
Public Sub CreateDirectory(directoryName As String)
Try
Dim myIsolatedStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
If Not String.IsNullOrEmpty(directoryName) AndAlso Not myIsolatedStorage.DirectoryExists(directoryName) Then
myIsolatedStorage.CreateDirectory(directoryName)
End If
' handle the exception
Catch ex As Exception
End Try
End Sub
To use it you can do:
Me.CreateDirectory("NewFolder")
For the deletion method:
Public Sub DeleteDirectory(directoryName As String)
Try
Dim myIsolatedStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
If Not String.IsNullOrEmpty(directoryName) AndAlso myIsolatedStorage.DirectoryExists(directoryName) Then
myIsolatedStorage.DeleteDirectory(directoryName)
End If
' handle the exception
Catch ex As Exception
End Try
End Sub
And to use it:
Me.DeleteDirectory("NewFolder")
Creating Files
You can create an empty file like this:
Dim myIsolatedStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
Dim writeFile As New StreamWriter(New IsolatedStorageFileStream("NewFolder\SomeFile.txt", FileMode.CreateNew, myIsolatedStorage))
To delete the file:
myIsolatedStorage.DeleteFile("NewFolder/SomeFile.txt")
Like before, a good practise when it comes to creating files is to always check if the directory you are writing to or deleting exists.
You can do something like:
Try
Dim myIsolatedStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
Dim writeFile As StreamWriter
If Not myIsolatedStorage.DirectoryExists("NewFolder") Then
myIsolatedStorage.CreateDirectory("NewFolder")
writeFile = New StreamWriter(New IsolatedStorageFileStream("NewFolder\SomeFile.txt", FileMode.CreateNew, myIsolatedStorage))
Else
writeFile = New StreamWriter(New IsolatedStorageFileStream("NewFolder\SomeFile.txt", FileMode.CreateNew, myIsolatedStorage))
End If
' do something with exception
Catch ex As Exception
End Try
Saving and Reading Text Files
To save a text file with your content you can do something like:
Dim myIsolatedStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
Using writeFile As New StreamWriter(New IsolatedStorageFileStream("myNote.txt", FileMode.Create, FileAccess.Write, myIsolatedStorage))
Dim someTextData As String = "This is some text data to be saved in a new text file in the IsolatedStorage!"
writeFile.WriteLine("note data")
writeFile.Close()
End Using
To read the contents of the Text File:
Dim myIsolatedStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
Dim fileStream As IsolatedStorageFileStream = myIsolatedStorage.OpenFile("myFile.txt", FileMode.Open, FileAccess.Read)
Using reader As New StreamReader(fileStream)
TextBlock.Text = reader.ReadLine()
End Using
I've provided you with some of the basics of the Windows Phone Isolated Storage and how to use it but I highly suggest that you read more about it at Here

Related

Access to .txt [VB.NET]

I've developed a multi-user application. One user saves a string to a text file, other users have to read this string in a background process.
My problem is: when some users open the file to read the string, another user that wants to save a new string can't write to the file. I would like to implement a system that checks if the file is opened or not, and in case it is, waits a few seconds and retries.
What is the function or command to look if the file is already opened?
I solved my problem using tips:
i've replaced this code:
ip_acquisito = My.Computer.FileSystem.ReadAllText(path_elenco & "\ip.txt")
with this code:
Using fs As FileStream = File.Open(path_elenco & "\ip.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim b(1024) As Byte
Dim temp As UTF8Encoding = New UTF8Encoding(True)
Do While fs.Read(b, 0, b.Length) > 0
ip_acquisito = temp.GetString(b)
Loop
End Using
There are quite a few patterns you can deal with. Two I'd recommend would be:
Dealing with FileAccess, FileMode and FileShare
Sync the file access with EventWaitHandle

Program not able to write to a newly made text file

After some great arguments made by other users in this question: How to write to a text file inside of the application, I decided to not use a resource file, instead create a file in a folder & then read/write from there.
Although, for some reason, I can't seem to write to the file in question, it keeps throwing an exception telling me the file is already in use by another process.
Here's the code which I use for writing to this file.
If System.IO.File.Exists(credentials) Then
Dim objWriter As New System.IO.StreamWriter(credentials, False)
objWriter.WriteLine(remember)
objWriter.Close()
Else
System.IO.Directory.CreateDirectory(Mid(My.Application.Info.DirectoryPath, 1, 1) & ":\ProgramData\DayZAdminPanel")
System.IO.File.Create(credentials)
Dim objWriter As New System.IO.StreamWriter(credentials, False)
objWriter.WriteLine(remember)
objWriter.Close()
End If
Any ideas on how I can write to the text file in question?
There's a good chance that a previous iteration of your application failed to properly close access to the file in the StreamWriter. Since your constructor is set to overwrite (and not append) to the file, this could be the source.
Try setting up your application with a "Using" statement to properly open/close the file:
If System.IO.File.Exists(credentials) Then
Using objWriter As StreamWriter = New StreamWriter(credentials, False)
objWriter.WriteLine(remember)
objWriter.Close()
End Using
Else
System.IO.Directory.CreateDirectory(Mid(My.Application.Info.DirectoryPath, 1, 1) & ":\ProgramData\DayZAdminPanel")
System.IO.File.Create(credentials)
Using objWriter As StreamWriter = New StreamWriter(credentials, False)
objWriter.WriteLine(remember)
objWriter.Close()
End Using
End If
It does look rather redundant to have a Using block and a close statement, but this ensures access to your file even if an exception occurs.
You are trying to create a directory in the common application data directory. This directory should be found using the Environment class methods and enums because is different between operating systems. However you use the value credentials for the filename. I suppose that you want to store your datafile in the common application directory and not in a place where there is no permission to write data files (Like C:\program files (x86)).
Then, to avoid problems with file stream not correctly closed try to use the Using statement that assures a correct closure and disposal of your file resource (No need to call close inside a Using).
Also, notice that StreamWriter is perfectly capable to create the file if it doesn't exists or if you wish to overwrite the previous contents (passing false for the Append flag).
So your code could be reduced to these lines.
' Get the common application data directory (could be different from Win7 and XP)
Dim workDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
' Combine with your own working data directory
workDir = Path.Combine(workdir, "DayZAdminPanel")
' Create if not exists
If Not Directory.Exists(workDir) Then
Directory.CreateDirectory(workDir)
End If
' Create/Overwrite your data file in a subfolder of the common application data folder
Dim saveFile = Path.Combine(workDir, Path.GetFileName(credentials))
Using objWriter = New System.IO.StreamWriter(saveFile, False)
objWriter.WriteLine(remember)
End Using
File.Create returns an opened FileStream that you should be passing into the StreamWriter constructor on the subsequent, rather than passing the filename again, or you can just omit the File.Create call altogether.
You might want to look into a Using block for the StreamWriter so it gets predictably closed.

Let VB Form prepare working environment in chosen directory

I am working on a GUI for a simulation program. The simulation program is a single .exe which is driven by an input file (File.inp placed in the same directory).
The Original.inp file functions as a template from which the form reads all the values into an array. Then it changes these values reflecting the changes done by the user in the form. After that it writes all the new values to File.inp.
By pushing the "Run" button the Simulation.exe file is executed.
The folder structure looks like this:
root
|
|---input
| |
| |--Original.inp
|
|---GUI.exe
|---Simulation.exe
|---File.inp
Ideally I would supply only the GUI, the user would select the working directory and then the GUI.exe would create an input folder and extract the Original.inp and Simulation.exe in the appropriate locations. So far I have only managed to include Original.inp and Simulation.exe as "EmbeddedResources" in my VB project and I have let my code create an input folder in the working directory chosen by the user.
Can someone please explain to me how I can extract the .inp and .exe file into the correct directories? I've searched on google, tried File.WriteAllBytes and Filestream.WriteByte but did not get the desired results.
The problem with File.WriteAllBytes was that I could not point to the embedded resource ("Simulation.exe is not a member of Resources" and with Filestream.WriteByte I got a 0 kb file.
The question commenters are correct, this is probably a task best left for a setup program. However, that having been stated, in the interest of answering the question as asked I offer the following approach.
Contrary to your supposition in your question comment, you do need to "read" the embedded resource from the GUI's executable file, since it's an embedded resource and not an external resource. It won't magically extract itselt from the executable file. You need to do the manual read from the assembly and write to your specified locations. To do this, you need to read the resource using .Net Reflection, via the currently executing assembly's GetManifestResourceStream method.
The Simulation.exe file is a binary file so it must be handled as such. I assumed that the Orginal.inp file was a text file since it afforded the opportunity to demonstrate different types of file reads and writes. Any error handling (and there should be plenty) is omitted for brevity.
The code could look something like this:
Imports System.IO
Imports System.Reflection
Module Module1
Sub Main()
'Determine where the GUI executable is located and save for later use
Dim thisAssembly As Assembly = Assembly.GetExecutingAssembly()
Dim appFolder As String = Path.GetDirectoryName(thisAssembly.Location)
Dim fileContents As String = String.Empty
'Read the contents of the template file. It was assumed this is in text format so a
'StreamReader, adept at reading text files, was used to read the entire file into a string
'N.B. The namespace that prefixes the file name in the next line is CRITICAL. An embedded resource
'is placed in the executable with the namespace noted in the project file, so it must be
'dereferenced in the same manner.
Using fileStream As Stream = thisAssembly.GetManifestResourceStream("SOQuestion10613051.Original.inp")
If fileStream IsNot Nothing Then
Using textStreamReader As New StreamReader(fileStream)
fileContents = textStreamReader.ReadToEnd()
textStreamReader.Close()
End Using
fileStream.Close()
End If
End Using
'Create the "input" subfolder if it doesn't already exist
Dim inputFolder As String = Path.Combine(appFolder, "input")
If Not Directory.Exists(inputFolder) Then
Directory.CreateDirectory(inputFolder)
End If
'Write the contents of the resource read above to the input sub-folder
Using writer As New StreamWriter(Path.Combine(inputFolder, "Original.inp"))
writer.Write(fileContents)
writer.Close()
End Using
'Now read the simulation executable. The same namespace issues noted above still apply.
'Since this is a binary file we use a file stream to read into a byte buffer
Dim buffer() As Byte = Nothing
Using fileStream As Stream = thisAssembly.GetManifestResourceStream("SOQuestion10613051.Simulation.exe")
If fileStream IsNot Nothing Then
ReDim buffer(fileStream.Length)
fileStream.Read(buffer, 0, fileStream.Length)
fileStream.Close()
End If
End Using
'Now write the byte buffer with the contents of the executable file to the root folder
If buffer IsNot Nothing Then
Using exeStream As New FileStream(Path.Combine(appFolder, "Simulation.exe"), FileMode.Create, FileAccess.Write, FileShare.None)
exeStream.Write(buffer, 0, buffer.Length)
exeStream.Close()
End Using
End If
End Sub
End Module
You will also have to add logic to determine if the files have been extracted so it doesn't happen every time the GUI is invoked. That's a big reason why an installation program might be the correct answer.

Download file in VB.NET 2010

I have looked almost everywhere on the internet and I cannot find a way to download a file from the internet into a specific folder that works with VB.NET 2010. I would like to download a file called, for instance, example.txt, and download it into, for example, %HOMEDRIVE%%HOMEPATH%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup so that it will run automatically at system startup. All help is appreciated
Guessing something based on...
Using webClient = New WebClient()
Dim bytes = webClient.DownloadData("http://www.google.com")
File.WriteAllBytes(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "MyFileName.ext"), bytes)
End Using
As for the startup, VB.NET has a pretty ease way to add Registry keys...
My.Computer.Registry.SetValue
To set something like HKEY_CURRENT_USER\Software\Microsoft\CurrentVersion\Run
UPDATE
How to: Create a Registry Key and Set Its Values in Visual Basic
http://msdn.microsoft.com/en-us/library/cy6azwf7(v=VS.100).aspx
I would suggest using WebClient.DownloadFile. Use Environment.SpecialFolder.Startup to get the path to save the file.
Sub Main()
Using wc As New WebClient()
Dim startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup)
wc.DownloadFile("http://MyDomain.com/MyFile.txt", Path.Combine(startupPath, "test.txt"))
End Using
End Sub

Using a .txt file Resource in VB

Right now i have a line of code, in vb, that calls a text file, like this:
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("data5.txt")
data5.txt is a resource in my application, however the application doesn't run because it can't find data5.txt. I'm pretty sure there is another code for finding a .txt file in the resource that i'm overlooking, but i can't seem to figure it out. So does anyone know of a simple fix for this? or maybe another whole new line of code? Thanks in advance!
If you added the file as a resource in the Project + Properties, Resources tab, you'll get its content by using My.Resources:
Dim content As String = My.Resources.data5
Click the arrow on the Add Resource button and select Add Existing File, select your data5.txt file.
I'm assuming that the file is being compiled as an Embedded Resource.
Embedded Resources aren't files in the filesystem; that code will not work.
You need to call Assembly.GetManifestResourceStream, like this:
Dim fileText As String
Dim a As Assembly = GetType(SomeClass).Assembly
Using reader As New StreamReader(a.GetManifestResourceStream("MyNamespace.data5.txt"))
fileText = reader.ReadToEnd()
End Using
First, go to project resources (My Project --> Resources), and drag-and-drop your file, say "myfile.txt", from the file system to the resourses page.
Then:
Imports System.IO
...
Dim stream As New MemoryStream(My.Resources.myfile)
Dim reader As New StreamReader(stream)
Dim s As String = reader.ReadToEnd()