尝试将内容文件复制到lonelystorage时出现NullReferenceException [英] NullReferenceException when trying to copy content file to isolatedstorage
问题描述
我正在尝试将文件复制到IsolatedStorage的资源,我正在使用以下代码。但我得到一个错误NullRefernceException。请帮助找到错误。
I am trying to copy an file to the resource to the IsolatedStorage, for which i am using the following code. But i get an error an NullRefernceException. Please help in finding the error.
Private Sub Page1_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
Try
Me.CopyFromContentToStorage("DataFiles/Customers.xml")
Catch ex As Exception
MessageBox.Show("Could not gather the Customers List", "Order Entry System", MessageBoxButton.OK)
End Try
End Sub
Private Sub CopyFromContentToStorage(fileName As [String])
Try
Using Store As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication
Using Src As Stream = Application.GetResourceStream(New Uri("/OrderEntrySystem;component/" + fileName, UriKind.Relative)).Stream
Using IsoStream As New IsolatedStorageFileStream("/OrderEntrySystem;component/" + fileName, FileMode.OpenOrCreate, FileAccess.Write, Store)
Src.Position = 0
CopyStream(Src, IsoStream)
IsoStream.Flush()
End Using
End Using
End Using
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Shared Sub CopyStream(input As Stream, output As IsolatedStorageFileStream)
Dim buffer As Byte() = New Byte(32767) {}
Dim TempPos As Long = input.Position
Dim readCount As Integer
Do
readCount = input.Read(buffer, 0, buffer.Length)
If readCount > 0 Then
output.Write(buffer, 0, readCount)
End If
Loop While readCount > 0
input.Position = TempPos
End Sub
我认为路径存在一些问题。
I think there is some problem with the path.
我的项目名称是"OrderEntrySystem_WP"但我已将程序集名称处理为"OrderEntrySystem"
My Project Name Is "OrderEntrySystem_WP" but i have chagned the assembly name to "OrderEntrySystem"
推荐答案
您有"构建行动"作为内容。因此,您不必编写"/ OrderEntrySystem; component /"
You have "Build Action" as Content. So you don't have to write "/OrderEntrySystem;component/"
请参阅以下链接了解详细信息
Refer following link for detail
http://msdn.microsoft.com/en-us/library/windowsphone/develop /ms596994(v=vs.105).aspx
您在IsolatedStorageFileStream中的文件路径不正确。尝试以下代码
Your file path in IsolatedStorageFileStream is not correct. Try following code
Using Src As Stream = Application.GetResourceStream(fileName, UriKind.Relative)).Stream
Using IsoStream As New IsolatedStorageFileStream(fileName,FileMode.OpenOrCreate, FileAccess.Write, Store)
Src.Position = 0
CopyStream(Src, IsoStream)
IsoStream.Flush()
End Using
End Using
这篇关于尝试将内容文件复制到lonelystorage时出现NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!