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
}
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.
In my case, I want to build my own "drop box" like application which I am going to use as a part of my another project.
Discription:
When a word file is opened in the "drop box" folder(inside the folder where changes to the files, file creations deletions ect.. are identified). pictures, txts, txt updates are uploaded to the server without any issue.
But when it comes to office documents. office document creation is uploaded.
Problem:
when the word file is opened, and do some update and save it. the file can not be uploaded due to permission error. even the opened file can not be copied to another place and then uploaded.
Any one faced this kind of issue, and any sugessions.
But we can manually copy and save a opened and saved(but not closed) to another location
But in the program it is not allowed.
You can create another copy of file, this is important because uploading may be slower and reading shared file may lead to conflicts for Word, so what you can do is, you can create a copy quickly on temp file and upload the temp file.
string tmp = Path.GetTempFileName();
using(Stream s = new FileStream(filePath,
FileMode.Open, FileAccess.Read,
// following option will let you open
// opened file by other process
FileShare.ReadWrite)){
using(FileStream fs = File.OpenWrite(tmp)){
// this will copy file to tmp
s.CopyTo(fs);
}
}
// upload tmp file...
your problem is similar to what we faced. In our case we are all connected to a domain directory and the problem was the antivirus installed on our server gives read/write permissions to users (executing exe, installing apps). so you specifically need to give a user the right to execute an app that wants to use another app, in this case office docs.
The problem extended to asp apps using Crystal Reports. hope it helps.
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.
how do I use a file that is currently being used by another process?
If the file is opened with sharing permissions then you should just be able to use it. On the other hand if the process opened the file with non-sharing permissions you cannot access it until the process in question releases the file.
If you own the processes in question, you can enable read sharing by opening the file in the following way.
using (var file = new FileStream(
#"C:\path\to\file.txt",
FileMode.Open,
FileAcces.Read,
FileShare.Read) {
// ...
}
The important parameter there is the FileShare.Read