FileInfo CopyTo method throwing an IOException (.net) - c#

The CopyTo method of FileInfo class throws an IOException
The process cannot access the file 'C:\Data\Test.XML' because it is being used by another process.
Any ideas on why this should happen? I understand that copying a file just requires read access. So ideally even if the file is write protected or is opened by some other program the CopyTo should have no problem executing.
FileInfo copyFile = null;
//currentFile.FileInformation is of type FileInfo which is referring to the file for which a copy is being created. In this case it is C:\Data\Test.XML
System.IO.FileInfo file = new FileInfo(currentFile.FileInformation.FullName);
// Constructing name for the temporary copy of Test.XML
string newName = "Temp Copy of " + currentFile.FileInformation.Name;
//This is where I get the exception. The CopyTo fails...
copyFile = file.CopyTo(System.IO.Path.Combine(currentFile.FileInformation.DirectoryName, newName), true);
fs = System.IO.File.Open(copyFile.FullName, FileMode.Open);
Also some important points to note :
I have write access to the folder to which I am trying to copy. This is happening with only certain files.
The file for which I am trying to create a copy of is not Read-only.
Please let me know if I can provide you with any more details
Thanks in advance

Download sysinternal's process explorer
put a breakpoint on File.CopyTo
in process explorer, search for the file name, it will tell you which process got it open

Another process might have specified the FILE_SHARE_READ mode when it opened the file, which would prevent you from even reading it.
You can use Process Explorer to find that process.

If you're using Windows, try using Process Explorer to determine what process is using the files you are trying to copy.

Related

Read-only file in use right after read-only set to false

I'm trying to use the following code to delete a read-only file.
var fileInfo = new FileInfo(saveLocation);
fileInfo.IsReadOnly = false;
fileInfo.Delete();
When it gets to the third line, the following exception is thrown
Message: The process cannot access the file '\\filepath\filename.pdf' because it is being used by another process.
Note: \\filepath\filename.pdf is not the actual file path, I'm just using it to replace a longer path
I've checked the file, and before the code runs, it is set to read-only, and after the code runs, it is not anymore.
Am I incorrect in thinking that when a file is opened as read-only it is not considered to by in use? I'm pretty sure that is true for Microsoft office files suck as .xlsx files, but maybe not for PDFs?
Ultimately, my goal is to be able to push an updated version of this file to a shared location even if some user has the file open on their machine, which is why I initially set it to be read-only.
Message: The process cannot access the file '\filepath\filename.pdf' because it is being used by another process.
This is not the same as the file being read-only.
You can find out in code which process is locking the file
https://stackoverflow.com/a/20623311/141172
You can also find out from the command line
UPDATE
Based on your comments, it seems like you may want an exclusive lock on the file for the duration that you are processing it
open file in exclusive mode in C#
Command-line tool for finding out who is locking a file

Does WebClient.DownloadFileAsync overwrite the file if it already exists on disk?

I can't find any information on my question. Please excuse me if my search efforts have not been good enough to find the answer. I just want to avoid spinning my wheels.
Thanks!
Follow up: If it doesn't overwrite, how can I get it to (if possible)?
A 30 second test confirms that it does overwrite
Test:
using (WebClient client = new WebClient())
{
client.DownloadFileAsync(new Uri("http://download.microsoft.com/download/8/4/A/84A35BF1-DAFE-4AE8-82AF-AD2AE20B6B14/directx_Jun2010_redist.exe"), #"C:\Test.exe");
}
Test.exe is overwitten if downlaoded again
The WebClient class is obviously designed to suppress a lot of detail and control. You can write your own method to asynchronously download a file very easily and control how the downloaded data is written to disc.
I know for sure that this solution at codeproject contains a class which downloads a file using WebRequest and WebResponse which allows for much more control. See the class contained named webdata. The code you can need to pay attention too:
FileStream newFile = new FileStream(targetFolder + file, FileMode.Create);
newFile.Write(downloadedData, 0, downloadedData.Length);
newFile.Close();
The FileMode Enumeration contains a series of members that dictate the behaviour of saving a file FileMode.CreateNew will throw an IOException if a file already exists. As where FileMode.Create will overwrite files if possible.
If you insist on using WebClient.DownloadFileAsync then, as the other fellas have already mentioned: you can just inform the user that an existing file will be overwritten by means of an OpenFileDialog but some downloads can be time consuming and there's nothing to say that the user has not created another file during the download.
If the file exists, yes.
If you are renaming it, or if you have it hooked into an OpenFileDialogue(), that's your discretion.

Making a file backup program

Where I work, I test software, we put a new build of the software on the server each day, I need to be able to save a copy of the build and renaming it for the date it was saved, how would I do this?
(the start and destination locations will be the same each time)
The file is just an installer kinda, it is all self contained.
I need it to access the date the original was created on, so I do not have the problem with multiple files of the same name, and making it easy to find the build I need.
From msdn:
string path = #"c:\temp\MyTest.txt";
string path2 = path + "temp";
try
{
// Create the file and clean up handles.
using (FileStream fs = File.Create(path)) {}
// Ensure that the target does not exist.
File.Delete(path2);
// Copy the file.
File.Copy(path, path2);
Console.WriteLine("{0} copied to {1}", path, path2);
// Try to copy the same file again, which should succeed.
File.Copy(path, path2, true);
Console.WriteLine("The second Copy operation succeeded, which was expected.");
}
Are you talking about running perhaps a batch script or vbscript type file to do this automatically? That's about the easiest way to do it that I can think of. However, I may be oversimplifying your situation, so just let me know. I can provide you with some example code on how to do this via a .bat or vbscript file.
I would create a new folder containing the date in the name (like "Build_2012_06_07") for each build and move all the files (.exe, .dll, .pdb etc.) belonging to a build to this folder without renaming the files.

.NET: IOException for permissions when creating new file?

I am trying to create a new file and write XML to it:
FileStream output = File.Create(Path.Combine(PATH_TO_DATA_DIR, fileName));
The argument evaluates to:
C:\path\to\Data\test.xml
The exception is:
The process cannot access the file 'C:\path\to\Data\test.xml' because it is being used by another process.
What am I doing wrong here?
UPDATE: This code throws the same exception:
StreamWriter writer = new StreamWriter(Path.Combine(PATH_TO_DATA_DIR, fileName));
UPDATE 2: The file I am trying to create does not exist in the file system. So how can be it in use?
UPDATE 3: Changed to a new file name, and now it works. I'm not sure why. Perhaps I unknowing created it in the first call, and it failed on subsequent calls?
The message means that another process is using that file. If you have it open it could be using it, or when it was originally created if the stream was not closed properly that could do it also.
First check to make sure you do not have it open. I would try to change the fileName and see if you get the same error. If you do get the same error than some place in your code it is not closing a stream that when it is done with the file.
Your program keeps a handle on your file after it's being created to return to your FileStream object. Because you don't specify the access to it, perhaps it won't let you get a grab on it. Perhaps should you consider closing it, and then reopen it in a proper manner by specifying how you want it open (ReadOnly, ReadWrite, WriteOnly) ?
Not trying to sound insulting, but does the folder exist? Does the file already exist but is hidden by the system? And does the user account that is running the program have write permissions to the folder? Have you tried creating a file using a different method (like with File.WriteAllText(<path>, "Testing") just to see if it's your particular call to File.Create?

How can I tell when a file has been recently overwritten inside an FTP folder?

I'm writing a Windows Service that needs to take a file from an FTP folder on the local computer and parse the data in that file, and push it to an external source. The file will be uploaded to the FTP folder at some point from some other party, and I can't predict when the file will be uploaded. I need the service to be able to scan the FTP folder and recognize when the file has been recently uploaded to the server, and kick off the parsing process.
I need some know if there's some way in .NET to be able to detect when a file was put into a directory.
There is a similar question in SO about this here, but it wasn't in regards to writing a windows service. Also, the solution seemed to be to monitor the directory itself, but I tested that idea using the DirectoryInfo class and it doesn't work when looking at the LastWriteTime property. The time of the directory doesn't change when I copy and replace a file in the directory.
Note: I can't rely on the create/modify timestamps on the file, being as I don't know how the other party is generating these files.
Use a FileSystemWatcher. That will throw a "Changed" (Or even a "created" I believe) event which you can handle. You'll need to be careful to make sure the file is fully written before you start working with it, but that should get you pointed in the right direction.
EDIT: To Joseph's question about how, here's a static method I keep in my utilities class for just such occasions:
public static bool FileComplete(string filePath)
{
try
{
File.Open(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
return true;
}
catch { return false; }
}
Then I just a While(!FileComplete(foo)) {} before my actual file handling.
Take a look at FileSystemWatcher.

Categories

Resources