IsolatedStorageFileStream exception is throw when file is opened? - silverlight

when I attempt to open a file in a WP7 app:
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream nameXmlFile = new IsolatedStorageFileStream("Names.xml", System.IO.FileMode.Open, isf);
I recieve the following error:
Operation not permitted on IsolatedStorageFileStream.
I'm not sure why it isn't opening because I used the exact code somewhere else in my app and it works fine.
Any leads as to why this is happening?
EDIT
I used the following code to add the file to isolated storage in the App.xaml.cs Application_Launching Event:
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream feedXmlFile = new IsolatedStorageFileStream("Names.xml",System.IO.FileMode.Create, isf);

One of the problems with using the IsolatedStorageFileStream constructor is that the exception generated has limited info. The alternative OpenFile method has a richer set of exceptions.
As a general rule of thumb if an API allows you to do the same thing with a constructor or with a method go with the method. In this case try this code instead:-
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream nameXmlFile = isf.OpenFile("Names.xml", System.IO.FileMode.Open);
If this fails you will have at least narrowed down the potential cause.
This may seem obvious but in your creation code you have actually written to and closed the file you created?

IsolatedStorage Exception is a known issue whil fires Application_Launching. more details

When you run into an exception during file access, check for two things:
isolated storage is not thread safe, so use a 'lock' when accessing the file system
Isolated storage best practices
Fully read the stream into memory before closing the stream. So don't pass the filestream back to for example an image control.
Reading a bitmap stream from isolated storage

Related

Spring Batch FlatFileItemWriter does not write data to a file

I am new to Spring Batch application. I am trying to use FlatFileItemWriter to write the data into a file. Challenge is application is creating the file on a given path, but, now writing the actual content into it.
Following are details related to code:
List<String> dataFileList : This list contains the data that I want to write to a file
FlatFileItemWriter<String> writer = new FlatFileItemWriter<>();
writer.setResource(new FileSystemResource("C:\\Desktop\\test"));
writer.open(new ExecutionContext());
writer.setLineAggregator(new PassThroughLineAggregator<>());
writer.setAppendAllowed(true);
writer.write(dataFileList);
writer.close();
This is just generating the file at proper place but contents are not getting written into the file.
Am I missing something? Help is highly appreciated.
Thanks!
This is not a proper way to use Spring Batch Writer and writer data. You need to declare bean of Writer first.
Define Job Bean
Define Step Bean
Use your Writer bean in Step
Have a look at following examples:
https://github.com/pkainulainen/spring-batch-examples/blob/master/spring-boot/src/main/java/net/petrikainulainen/springbatch/csv/in/CsvFileToDatabaseJobConfig.java
https://spring.io/guides/gs/batch-processing/
You probably need to force a sync to disk. From the docs at https://docs.spring.io/spring-batch/trunk/apidocs/org/springframework/batch/item/file/FlatFileItemWriter.html,
setForceSync
public void setForceSync(boolean forceSync)
Flag to indicate that changes should be force-synced to disk on flush. Defaults to false, which means that even with a local disk changes could be lost if the OS crashes in between a write and a cache flush. Setting to true may result in slower performance for usage patterns involving many frequent writes.
Parameters:
forceSync - the flag value to set

How to access downloaded sound in IsolatedStorage via URI (WP7)?

I basically downloaded a file name custom.mp3 into my isolatedstorage and I can see it via isolatedstorage explorer....
The question here is... How can I access the particular custom.mp3 via URI?
So far I got this.. but I wonder why it is not working:
alarm.Sound = new Uri("isostore:/custom.mp3", UriKind.Absolute);
Your path is wrong. Nothing else is wrong with your code. Post the code you're using for saving the mp3 file in the first place, if you want further help.
For easier reading, the code to store the MP3 goes something like this..
string alarmfile = "custom.mp3";
isolatedStorageFileStream = new IsolatedStorageFileStream(alarmfile,FileMode.Create,isolatedStorageFile);
long songfilelength = (long) e.Result.Length;
byte[] songbyte = new byte[songfilelength];
e.Result.Read(songbyte, 0, songbyte.Length);
isolatedStorageFileStream.Write(songbyte, 0, songbyte.Length);
isolatedStorageFileStream.Flush();
Only files packaged in the XAML can be used as alarm sound:
Remarks
The Sound URI must point to a file packaged in the application’s .xap
file. Isolated storage is not supported. When the alarm is launched,
the sound is played quietly and then gradually increases in volume.
There is no way to modify this behavior.
From:
Alarm.Sound Property

Can't read files from BlobStore

I have been trying to write and read files directly from the BlobStore, but it just doesnt work.
The issue is I open the file like file = fileService.getBlobFile(blobKey); and it doesn't throw any exception but right in the next line I call readChannel = fileService.openReadChannel(file, false); and that one throws a FileNotFoundException.
I'm confused as to why the first line did not throw the exception.
Here is the same issue
Unfortunately no one answered that question.
I haven't had any problems with writes or deletes, but I too get a FileNotFoundException when using openReadChannel(...) with an AppEngineFile.
I've tried using an AppEngineFile created from its constructor taking a complete path. I've tried using an AppEngineFile obtained from getBlobFile(...) like you do above. Either way, when the AppEngineFile is passed to openReadChannel(...) a FileNotFoundException is thrown.
My workaround was to let BlobstoreService.serve(...) do all the work of reading and sending the file. I suspect that using the FileService to read from an AppEngineFile isn't supported yet (I'm using 1.6.0), so reads must be done via the BlobstoreService (serve(...), fetchData(...), BlobstoreInputStream).

Windows Phone 7 App Quits when I attempt to deserialize JSON

I'm developing my first windows phone 7 app, and I've hit a snag. basically it's just reading a json string of events and binding that to a list (using the list app starting point)
public void Load()
{
// form the URI
UriBuilder uri = new UriBuilder("http://mysite.com/events.json");
WebClient proxy = new WebClient();
proxy.OpenReadCompleted += new OpenReadCompletedEventHandler(OnReadCompleted);
proxy.OpenReadAsync(uri.Uri);
}
void OnReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null)
{
var serializer = new DataContractJsonSerializer(typeof(EventList));
var events = (EventList)serializer.ReadObject(e.Result);
foreach (var ev in events)
{
Items.Add(ev);
}
}
}
public ObservableCollection<EventDetails> Items { get; private set; }
EventDetails is my class that wraps the json string. this class has to be correct because it is an exact copy of the class used by that website internally from which the json is generated...
I get the json string correctly from the webclient call (I read the memorystream and the json is indeed there) but as soon as I attempt to deserialize the string, the application exits and the debugger stops.
I get no error message or any indication that anything happen, it just stops. This happens if I type the deserialize method into the watch window as well...
I have already tried using JSON.net in fact I thought maybe it was a problem with JSON.net so I converted it to use the native deserializer in the .net framework but the error is the same either way.
why would the application just quit? shouldn't it give me SOME kind of error message?
what could I be doing wrong?
many thanks!
Firstly, the fact that you have some string there that looks like JSON does not mean that you have a valid JSON. Try converting a simple one.
If your JSON is valid, it might be that your JSON implementation does not know how to convert a list to EventList. Give it a try with ArrayList instead and let me know.
The application closes because an unhandled exception happens. If check the App.xaml.cs file you will find the code that closes your app. What you need to do is try catch your deserialization process and handle it locally. So most likely you have some JSON the DataContractJsonSerializer does not like. I have been having issue with it deserializing WCF JSON and have had to go other routes.
You may want to check to ensure your JSON is valid, just because your website likes it does not mean it is actually valid, the code on your site may be helping to correct the issue. Drop a copy of your JSON object (the string) in http://jsonlint.com/ to see if it is valid or not. Crokford (the guy who created JSON) wrote this site to validate JSON, so I would rely on it more than your site ;) This little site has really helped me out of some issues over the past year.
I ran into this same kind of problem when trying to migrate some existing WM code to run on WP7. I believe that the WP7 app crashes whenever it loads an assembly (or class?) that references something that's not available in WP7. In my case, I think it was Assembly.Load or something in the System.IO namespace, related to file access via paths.
While your case might be something completely different, the symptoms were exactly the same.
The only thing I can recommend is to go through the JSON library and see if it's referencing base classes that are not allowed in WP7. Note that it doesn't even have to hit the line of code that's causing the issue - it'll crash as soon as it tries to hit the class that contains the bad reference.
If you can step into the JSON library, you can get a better idea of which class is causing the problem, because as soon as the code references it, the whole app will crash and the debugger will stop.

Attempt to access the method failed: System.IO.StreamWriter..ctor(System.String, Boolean)

I need to write string to an xml file in Silverlight. When I used the following code, the above mentioned exception "Attempt to access the method failed: System.IO.StreamWriter..ctor(System.String, Boolean)" has occurred.
using (StreamWriter sw = new StreamWriter("C:\Test.xml", false))
{
sw.Write(root.ToString());
}
Can anyone help me out in doing writing to xml file in Silverlight?
Silverlight is in a sandboxed mode where it has restricted access. You can write to "Isolated Storage", but you cannot create files on their hard drive.
http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile(VS.95).aspx
http://forums.silverlight.net/forums/p/22011/77243.aspx

Resources