I want to read a binary file using BinaryReader, but I keep getting an exception:
using (var stream = File.Open("file.bin", FileMode.Open, FileAccess.Read))
{
using (BinaryReader r = new BinaryReader(stream)) //EXCEPTION
{
}
}
the "file.bin" has been set as a Content in the build action, but I keep getting this exception:
System.MethodAccessException was unhandled
Attempt to access the method failed: System.IO.File.Open(System.String, System.IO.FileMode, System.IO.FileAccess)
You don't use File.Open on Windows Phone 7 - you have to use isolated storage.
See the System.IO.IsolatedStorage namespace for more details.
For example:
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var stream = store.OpenFile("file.bin", FileMode.Open))
{
using (var reader = new BinaryReader(stream))
{
}
}
}
EDIT: As noted in comments, for content built into the XAP, you should use Application.GetResourceStream.
Related
I'm trying to work with text files in the apps folder.
Here's my GoogleApiClient constructor:
googleApiClient = new GoogleApiClient.Builder(this)
.AddApi(DriveClass.API)
.AddScope(DriveClass.ScopeFile)
.AddScope(DriveClass.ScopeAppfolder)
.UseDefaultAccount()
.AddConnectionCallbacks(this)
.EnableAutoManage(this, this)
.Build();
I'm connecting with:
googleApiClient.Connect()
And after:
OnConnected()
I need to list all files inside the app folder. Here's what I got so far:
IDriveFolder appFolder = DriveClass.DriveApi.GetAppFolder(googleApiClient);
IDriveApiMetadataBufferResult result = await appFolder.ListChildrenAsync(googleApiClient);
Which is giving me the files metadata.
But after that, I don't know how to read them, edit them or save new files. They are text files created with my app's previous version (native).
I'm following the google docs for drive but the Xamarin API is a lot different and has no docs or examples. Here's the API I'm using: https://components.xamarin.com/view/googleplayservices-drive
Edit:
Here is an example to read file contents from the guide:
DriveFile file = ...
file.open(mGoogleApiClient, DriveFile.MODE_READ_ONLY, null)
.setResultCallback(contentsOpenedCallback);
First I can't find anywhere in the guide what "DriveFile file = ..." means. How do I get this instance? DriveFile seems to be a static class in this API.
I tried:
IDriveFile file = DriveClass.DriveApi.GetFile(googleApiClient, metadata.DriveId);
This has two problems, first it complains that GetFile is deprecated but doesn't say how to do it properly. Second, the file doesn't have an "open" method.
Any help is appreciated.
The Xamarin binding library wraps the Java Drive library (https://developers.google.com/drive/), so all the guides/examples for the Android-based Drive API work if you keep in mind the Binding's Java to C# transformations:
get/set methods -> properties
fields -> properties
listeners -> events
static nested class -> nested class
inner class -> nested class with an instance constructor
So you can list the AppFolder's directory and files by recursively using the Metadata when the drive item is a folder.
Get Directory/File Tree Example:
await Task.Run(() =>
{
async void GetFolderMetaData(IDriveFolder folder, int depth)
{
var folderMetaData = await folder.ListChildrenAsync(_googleApiClient);
foreach (var driveItem in folderMetaData.MetadataBuffer)
{
Log.Debug(TAG, $"{(driveItem.IsFolder ? "(D)" : "(F)")}:{"".PadLeft(depth, '.')}{driveItem.Title}");
if (driveItem.IsFolder)
GetFolderMetaData(driveItem.DriveId.AsDriveFolder(), depth + 1);
}
}
GetFolderMetaData(DriveClass.DriveApi.GetAppFolder(_googleApiClient), 0);
});
Output:
[SushiHangover.FlightAvionics] (D):AppDataFolder
[SushiHangover.FlightAvionics] (F):.FlightInstrumentationData1.json
[SushiHangover.FlightAvionics] (F):.FlightInstrumentationData2.json
[SushiHangover.FlightAvionics] (F):.FlightInstrumentationData3.json
[SushiHangover.FlightAvionics] (F):AppConfiguration.json
Write a (Text) File Example:
using (var contentResults = await DriveClass.DriveApi.NewDriveContentsAsync(_googleApiClient))
using (var writer = new OutputStreamWriter(contentResults.DriveContents.OutputStream))
using (var changeSet = new MetadataChangeSet.Builder()
.SetTitle("AppConfiguration.txt")
.SetMimeType("text/plain")
.Build())
{
writer.Write("StackOverflow Rocks\n");
writer.Write("StackOverflow Rocks\n");
writer.Close();
await DriveClass.DriveApi.GetAppFolder(_googleApiClient).CreateFileAsync(_googleApiClient, changeSet, contentResults.DriveContents);
}
Note: Substitute a IDriveFolder for DriveClass.DriveApi.GetAppFolder to save a file in a subfolder of the AppFolder.
Read a (text) File Example:
Note: driveItem in the following example is an existing text/plain-based MetaData object that is found by recursing through the Drive contents (see Get Directory/File list above) or via creating a query (Query.Builder) and executing it via DriveClass.DriveApi.QueryAsync.
var fileContexts = new StringBuilder();
using (var results = await driveItem.DriveId.AsDriveFile().OpenAsync(_googleApiClient, DriveFile.ModeReadOnly, null))
using (var inputStream = results.DriveContents.InputStream)
using (var streamReader = new StreamReader(inputStream))
{
while (streamReader.Peek() >= 0)
fileContexts.Append(await streamReader.ReadLineAsync());
}
Log.Debug(TAG, fileContexts.ToString());
I want to use the file in isolated storage using its full path(like URI), I know there is sandboxed api to access isolated storage. But i have to load the images using their paths.
So is it possible ?
I got it like this:
public string GetAbsolutePathOfFile(string filePath)
{
using (var storage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var isfs = storage.OpenFile(filePath, FileMode.Open, FileAccess.ReadWrite,
FileShare.ReadWrite))
{
return isfs.Name; // this return the absolute path.
}
}
}
FileStream FS = new FileStream("MyFolder\\MyFile.txt", FileMode.Open);
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("MyFolder\\MyFile.txt", FileMode.Append, myIsolatedStorage));
using (writeFile)
{
FS.Seek(0, SeekOrigin.End);
writeFile.WriteLine(txtWrite.Text);
writeFile.Close();
System.Diagnostics.Debug.WriteLine("Now I am here");
}
When I am trying to run this code(trying to append data into an existing text file), getting exception
"Attempt to access the method failed:
System.IO.FileStream..ctor(System.String, System.IO.FileMode)"
What is the mistake I have done here?
Don't use the FileStream class directory. Get your streams via the methods on IsolatedStorageFile:
IsolatedStorageFile myIsolatedStorage =
IsolatedStorageFile.GetUserStoreForApplication();
using (var writeFile = myIsolatedStorage.OpenFile("MyFolder\\MyFile.txt", FileMode.Append))
using (var writeFileStream = new StreamWriter(writeFile))
{
writeFileStream.WriteLine(txtWrite.Text);
System.Diagnostics.Debug.WriteLine("Now I am here");
}
Could it be that you're attempting to open the same file twice?
A version of your question (with an answer) can be seen at How to append data into the same file in IsolatedStorage for Windows Phone
Finally I made it working after struggling for 4 hrs:
IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
StreamWriter writeFile = new StreamWriter(new IsolatedStorageFileStream("MyFolder\\MyFile.txt", FileMode.Append, myIsolatedStorage));
writeFile.Flush();
System.Diagnostics.Debug.WriteLine(txtWrite.Text);
writeFile.WriteLine(txtWrite.Text);
writeFile.Close();
System.Diagnostics.Debug.WriteLine("Now I am here");
I removed the file stream method and did some modifications. Its started to work.
Thanks to everybody who tried to help me with your suggestions
I jwant to get length of wave file. Currently I'm using following code
using (IsolatedStorageFile isofile = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isostream = new IsolatedStorageFileStream(FilePath, System.IO.FileMode.Open,System.IO.FileAccess.Read, isofile))
{
me = new MediaElement();
me.SetSource(isostream);
}
}
embedVoiceLength = me.NaturalDuration.TimeSpan.TotalSeconds;
However, it doesn't return the length from naturalduration.timespan.totalseconds , because me is not opened;
If you can't get the length until the file is opened then try opening it.
If all you want is the length and to not actually play it then handle the MediaOpened event and when it is triggered get the length and then stop the playback.
You can use something like this:
Microphone microphone = Microphone.Default;
using (IsolatedStorageFileStream stream = storage.OpenFile(filename, FileMode.Open, FileAccess.Read))
{
TimeSpan duration = microphone.GetSampleDuration((int)stream.Length);
}
I picked this sample code from an interesting article on working with audio on WP7. Here it is, code is also available for download. Hope this helps! :)
I am writing a program that will be executing on the cloud. The program will generate an output that should be written on to a file and the file should be saved on the blob container.
I don't have a idea of how to do that
Will this code
FileStream fs = new FileStream(file, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
generate a file named "file" on the cloud...
Oh.. then how to store the content to the blob..
Are you attempting to upload a Page blob or a Block blob? Usually block blobs are what are required, unless you are going to create a VM from the blob image, then a page blob is needed.
Something like this works however. This snippet taken from the most excellent Blob Transfer Utility Check it out for all your upload and download blob needs. (Just change the type from Block To Page if you need a VHD)
public void UploadBlobAsync(ICloudBlob blob, string LocalFile)
{
// The class currently stores state in class level variables so calling UploadBlobAsync or DownloadBlobAsync a second time will cause problems.
// A better long term solution would be to better encapsulate the state, but the current solution works for the needs of my primary client.
// Throw an exception if UploadBlobAsync or DownloadBlobAsync has already been called.
lock (WorkingLock)
{
if (!Working)
Working = true;
else
throw new Exception("BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer.");
}
// Attempt to open the file first so that we throw an exception before getting into the async work
using (FileStream fstemp = new FileStream(LocalFile, FileMode.Open, FileAccess.Read)) { }
// Create an async op in order to raise the events back to the client on the correct thread.
asyncOp = AsyncOperationManager.CreateOperation(blob);
TransferType = TransferTypeEnum.Upload;
m_Blob = blob;
m_FileName = LocalFile;
var file = new FileInfo(m_FileName);
long fileSize = file.Length;
FileStream fs = new FileStream(m_FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
ProgressStream pstream = new ProgressStream(fs);
pstream.ProgressChanged += pstream_ProgressChanged;
pstream.SetLength(fileSize);
m_Blob.ServiceClient.ParallelOperationThreadCount = 10;
asyncresult = m_Blob.BeginUploadFromStream(pstream, BlobTransferCompletedCallback, new BlobTransferAsyncState(m_Blob, pstream));
}