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.
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 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
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);
}
hi i am working on c# project and i try to lock a file from being opened , copied or even deleted by using that code :
FileInfo fi = new FileInfo(textBox1.Text);
FileSecurity ds = fi.GetAccessControl();
ds.AddAccessRule(new FileSystemAccessRule("Authenticated Users", FileSystemRights.FullControl, AccessControlType.Deny));
fi.SetAccessControl(ds);
but when i open the file , it is opened and can be deleted , is there anything wrong on my code ?
by the way , that code works perfectly on anywhere but flash drive , i can block editing or copying files from computer , but on flash drive the application is useless .
What filesystem does your flash drive have? I'm guessing FAT32, rather than NTFS.
FAT32 has no concept of per-file ACLs (or as far as I know, no concept of ACLs whatsoever).
See this article:
http://technet.microsoft.com/en-us/library/cc783530(WS.10).aspx
On a FAT or FAT32 volume, you can set permissions for shared folders but not for files and folders within a shared folder. Moreover, share permissions on a FAT or FAT32 volume restrict network access only, not access by users working directly on the computer.
The only option will be to open the file in exclusive access mode to prevent others from changing it while you are reading it.
See this question (stolen from Vitaliy's comment):
How to lock file
The code from the accepted answer:
using (FileStream fs =
File.Open("MyFile.txt", FileMode.Open, FileAccess.Read, FileShare.None))
{
// use fs
}
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.