I am trying to create an empty PDF file in Xamarin.IOS using Filestream. When I try to create the Filestream using the following code:
FileStream fs = new FileStream("InvestmentAgreemen.pdf", FileMode.Create, FileAccess.ReadWrite);
I get the following error: "Access to the path "/private/var/containers/Bundle/Application/05AA1616-B66B-483D-8BA1-80A2B1AEC973/NewEPA.app/InvestmentAgreemen.pdf" is denied."
It worked fine when running on the iPad simulator, but as soon as I moved it to a real device I got this permissions error. Additionally, I have to use a filestream to create the empty PDF because I am also using Syncfusion's PDF tools for xamarin.IOS which require the use of Filestream for saving.
I also tried creating a FileIOPermission object granting AllAccess to AllFiles but I got a system not allowed error.
How can I resolve this permissions error?
You are trying to create a file in the root of your application's bundle, which is read-only. I'm surprised that this didn't fail on the simulator also.
To create a file at runtime, you need to specify a user writable path
var documents =
Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
var filename = Path.Combine (documents, "InvestmentAgreemen.pdf");
Create your pdf within a writable directory of your application instead of the root directory of the bundle:
using (var docPath = NSFileManager.DefaultManager.GetUrl(NSSearchPathDirectory.CachesDirectory, NSSearchPathDomain.All, null, true, out var nsError))
using (var fileStream = new FileStream(Path.Combine(docPath.Path, "InvestmentAgreemen.pdf"), FileMode.Create, FileAccess.ReadWrite))
{
// do something with the file stream
}
I have created a sample which creates and saves the PDF document in Xamarin.IOS platform. Which can be downloaded from the below location:
http://www.syncfusion.com/downloads/support/directtrac/184238/ze/PDFSave1731207096
Please try the sample and let me know whether the issue is fixed.
Regards,
Surya Kumar
Related
I need to create a process that creates/modifies some text files in a folder. I am using below code to do that:
file = new System.IO.FileInfo(filePath);
file.Directory.Create();
System.IO.File.WriteAllText(file.FullName, "Some text...");
I have a Biztalk queue that looks into the text files in the folder every 2 minutes and picks up the files to process them. I want to lock the files when I am creating/modifying so that Biztalk wont try to process those files. How can I achieve this?
I read about Transactional NTFS in windows which will let me create Transaction context but windows documentation says this feature will deprecated and recommends not to use it.
If the file is on a local NTFS volume of CIFS share, the File Adapter will not attempt to read an open file. However,
A better pattern would be to do your file work in a temporary folder, then copy the completed files to the BizTalk folder only when they are done. That way, you don't have to worry about locking at all.
To acquire an exclusive lock you can use the file stream to do so
using (FileStream fs = new FileStream("Test.txt", FileMode.Append, FileAccess.Write, FileShare.None))
{
using (StreamWriter sw = new StreamWriter(fs))
{
sw.WriteLine("test");
}
}
This way you are locking the file exclusively for the current file stream. Any other application or even a new instance of file stream from another thread within the same application attempts to read or write to the file will be denied by the operating system.
In most cases, write file with different extension then rename the file works fine.
I try to copy a file that is included in the app bundle as a resource to the temp folder on the iPhone. While this works on the Simulator, on the device I get an exception:
System.UnauthorizedAccessException: Access to the path
"/private/var/mobile/Applications/B763C127-9882-4F76-8860-204AFEA8DD68/Client_iOS.app/testbundle.zip"
is denied.
The code I use is below It cannot open the source file.
using(var sourceStream = File.Open("./demobundle.zip", FileMode.Open))
{
sourceStream.CopyTo(targetStream);
}
What is the correct way of copying a file into a destination stream?
Why is it that I always find the answers to my questions practically immediately after I asked here? :-)
One has to specify the file access mode. If it is set to Read, it'll work. The default seems to be some write mode and that is obviously not possible.
using(var sourceStream = File.Open("./demobundle.zip", FileMode.Open, FileAccess.Read))
{
sourceStream.CopyTo(targetStream);
}
I currently have the code below in my WPF application which does exactly what I want it to do, however, upon publishing this it won't necessarily be able to access these folder locations as they won't be pointing to the correct directory nor will the folders exist.
I was hoping somebody might be able to tell me what is the best way to save something into a local folder?
Whether it's inside the application folder itself or not is of no issue either.
The code I'm currently using for the writing of the file:
using (Stream stream = File.Open(#"..\..\Templates\data.bin", FileMode.Create))
{
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(stream, templateList);
}
The code I'm currently using for the loading of the file:
using (Stream stream = File.Open(#"..\..\Templates\data.bin", FileMode.Open))
{
BinaryFormatter bin = new BinaryFormatter();
templateList = (List<Template>)bin.Deserialize(stream);
}
You could use System.Environment.SpecialFolder.LocalApplicationData to store application specific data:
using System;
class Sample
{
public static void Main()
{
Console.WriteLine("GetFolderPath: {0}",
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
}
}
Ref: http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx
You can use Environment.SpecialFolder to find an appropriate place to put files (for example, ApplicationData would be a good place to start). If you only need a temp file, you can use Path.GetTempFileName to create one.
Edit: One last note. Storing stuff in the application folder itself can be a giant pain. Usually the application folder is created with the admin account during the installation so your app won't be able to write to it while running on a user account.
I'm using a FileStream to download information of an FTP server to a directory on my C:\ drive. For some reason, even though I've even tried setting the directory permissions to even 'Everyone' access, it's given me this exception:
System.UnauthorizedAccessException: Access to the path 'C:\tmpfolder' is denied'
Why is this? Here is an extract of my code.
byte[] fileData = request.DownloadData(dataMap.GetString("ftpPath") + "/" + content);
file = new FileStream(#"C:\tmpfolder", FileMode.Create, FileAccess.Write);
downloadedlocation = file.ToString();
file.Write(fileData, 0, fileData.Length);
Also, my program is not in ASP.NET and is just a C# console app.
If it doesn't matter where to store the file, try
using System.IO;
.
.
.
string tempFile = Path.GetTempFileName();
This will create a temporary file in your account temp folder. No concerns about permissions ;-)
I would guess that you do not have write privilege to c:\ where you try to create a file named tmpfolder.
If a folder named tmpfolder exists in your c:\ change your code to
file = new FileStream(#"C:\tmpfolder\myfile.tmp", FileMode.Create, FileAccess.Write);
hth
Mario
EDIT: On a further note: check out this link How to create a temporary file (for writing to) in C#? , you may need it if you have multiple file operations going on at the same time. Do no forget to delete the files afterwards.
I have a doubt from a silverlight application we can access MyDocuments. I am creating an Application which will download a set of files from a remote server . Is it possible to save these file in MyDocuments instead of Isolated Storage. I am using Silverlight 4.0 . Can any one give me Sample codes for it.
In order to acheive that you need to use Silverlight 4 and specify that is should get elevated privileges when install as an Out-of-browser application. When running as an OOB the app will have access to the users Documents folder.
In all other cases you will need to use the SaveFileDialog where the user can explictly specify where to save the file.
Edit code example:-
if (Application.Current.HasElevatedPermissions)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
path = Combine.Path(path, "MySaveFile.dat");
using (var filestream = File.OpenWrite(path))
{
// pump your input stream in to the filestream using standard Stream methods
}
}
No Isolated storage is currently the only option.