"Process cannot access file" problem

P

Paul

Hi,
VB.NET is saying the file I am creating is in use by another process and
won't complete its task of moving the file to the specified destination
folder.
Here is my code (the main bit anyway)....


Private Sub LogChange(ByVal source As Object, ByVal e As
System.IO.FileSystemEventArgs)
If e.ChangeType = WatcherChangeTypes.Created Then
System.IO.File.Move(e.FullPath, textBoxDest.Text & "\" & e.Name)
End If


e.FullPath returns the full path and filename of the file created e.g
c:\filename.txt
textboxDest is where the file is to be moved to e.g D:\
e.Name is the filename without the path e.g. filename.txt

This is the error text...

System.IO.IOException: The process cannot access the file
"D:\Unsorted\2005\New.mp3" because it is being used by another process.
What happens is files get downloaded to the D:\Unsorted\ folder which is
being monitored by the FileSystemWatcher. The app then tries to move the
downloaded/created file to the D:\Unsorted\2005\ folder then the error
occurs.

Any help much appreciated,
Thanks
Paul
 
P

petterl

I had simular problems with my filewatch program. It happend when I had a
window open on that directory in thumbnail or filmstrip mode. I also got a
tip about check if the file is finished before move it. try opend the file.
I have not tested the tip yet.

Petter L
 
P

petterl

Hi again

Right after my first nessage I got the problem again in my program. When I
halted right before the copy action and steped though it, it worked most of
the times but if i let the program run it created a "Process cannot access
file" and it pointed to the destination path for the file. What i did was to
put

System.Threading.Thread.Sleep(5000)

before the copy routine and now it works fine.

Maybe this can help on your problem too ?

Petter L.
 
P

Paul

Thanks for the tip Petter. It works on my problem too but I would prefer to
use a better way if one is known.

Is it possible to lock the file being moved so that no other process can
access the file? E.g. Windows trying to extract thumbnail, ID3 tag etc.

Thanks,
Paul
 
T

Tom Krueger [MSFT]

Hello,

I'm not sure if you are running into the same issue I had a while back or
not, but just incase here are my thoughts.

I remember I would get the same error and the only way I could get around it
was by calling dispose on the FileSystemWatcher. Here are my notes from the
code I wrote a while back
/// Release handles to directory. A directory cannot be fully
/// deleted until it is released. Need to use Dispose because
/// can't wait for garbage collector.

It worked for me. So I my architectured was a class called
FileSystemMoniter (which I created) and in this class I setup the
FileSystemWatcher and exposed the events of FileSystemWatcher. Mine might
be a bit different in that for each FileSystemWatcher event I had them raise
a my own Changed event so that my subscribers only had to listen to one
event when any changes occured.

Then when I needed to start and stop watching I released the references to
the changed event and called dispose on the FileSystemMonitor. Here is some
of the code.


/// <summary>

/// Monitors the file system for any changes in the specified path.

/// </summary>

public class FileSystemMonitor : IDisposable {


# region ***** FIELDS *****

private int _ignoreChangesPeriod;

private int _lastChangeTime;

private FileSystemWatcher _watcher;

private FileSystemWatcher _watcherChanged;

private string _watchPath;

public delegate void FileSystemChangedHandler(object sender,
FileSystemEventArgs e);

public event FileSystemChangedHandler Changed;

# endregion

# region ***** CONSTRUCTORS *****

public FileSystemMonitor(string path) {

_watchPath = path;

_watcher = new FileSystemWatcher();

_watcher.Created += new FileSystemEventHandler(OnFileCreated);

_watcher.Deleted += new FileSystemEventHandler(OnFileDeleted);

_watcher.Renamed += new RenamedEventHandler(OnFileRenamed); // NOTE: THIS
MAY NOT BE WORKING.

SetWatcherProperties(_watcher);

// Setup second watcher to watch changed because

// Create, Delete, and rename don't seem to fire when NotifyFilter is set.

// NOTE: THIS MAY NOT BE WORKING.

_watcherChanged = new FileSystemWatcher();

_watcherChanged.Changed += new FileSystemEventHandler(OnFileChanged);

_watcherChanged.NotifyFilter = NotifyFilters.FileName |
NotifyFilters.DirectoryName;

SetWatcherProperties(_watcherChanged);

}

# endregion

# region ***** PUBLIC METHODS *****

/// <summary>

/// Release handles to directory. A directory cannot be fully

/// deleted until it is released. Need to use Dispose because

/// can't wait for garbage collector.

/// </summary>

public void Dispose() {

_watcher.Dispose();

_watcherChanged.Dispose();

}

# endregion

# region ***** PRIVATE METHODS *****

private void SetWatcherProperties(FileSystemWatcher watcher) {

watcher.Path = _watchPath;

watcher.Filter = "*.*";

watcher.IncludeSubdirectories = true;

watcher.EnableRaisingEvents = true;

}

private void OnFileCreated(object sender, FileSystemEventArgs e) {

Debug.WriteLine("FileSystmeMonitor.OnFileCreated: " + e.FullPath);

CallChanged(sender, e);

}

private void OnFileDeleted(object sender, FileSystemEventArgs e) {

Debug.WriteLine("FileSystmeMonitor.OnFileDeleted: " + e.FullPath);

CallChanged(sender, e);

}

private void OnFileRenamed(object sender, RenamedEventArgs e) {

Debug.WriteLine("FileSystmeMonitor.OnFileRenamed: " + e.FullPath);

CallChanged(sender, e);

}

private void OnFileChanged(object sender, FileSystemEventArgs e) {

Debug.WriteLine("FileSystmeMonitor.OnFileChanged: " + e.FullPath);

CallChanged(sender, e);

}

private void CallChanged(object sender, FileSystemEventArgs e) {


// Check if should ignore change.

int currentTime = System.Environment.TickCount;

if (currentTime - _lastChangeTime > _ignoreChangesPeriod) {

if (this.Changed != null) {

this.Changed(sender, e);

}

} else {

Debug.WriteLine("CallChanged Ignored");

}

_lastChangeTime = currentTime;

}

# endregion

#region ***** PROPERTIES *****

public int IgnoreChangesMiliseconds {

get { return _ignoreChangesPeriod; }

set { _ignoreChangesPeriod = value; }

}

#endregion

}



-----

These methods were in another class that was using The FileSystemMonitor.

public void StartFileMonitor() {


Debug.WriteLine("Start File Monitor: " + this.Directory);

// Setup file system monitor. This object will alert

// this image to reload when changes occur to the file system.

if (_fileSystemMonitor == null) {

_fileSystemMonitor = new FileSystemMonitor(_dirPath);

_fileSystemMonitor.IgnoreChangesMiliseconds =
AppManager.FILE_SYSTEM_IGNORE_CHANGES_TIME;

}

if (_fileSystemChangedHandler == null) {

_fileSystemChangedHandler = new
FileSystemMonitor.FileSystemChangedHandler(FileSystemMonitor_OnFileSystemChanged);

Debug.WriteLine("FileSystemHandler Created: " + this.Directory);

}


_fileSystemMonitor.Changed += _fileSystemChangedHandler;

Debug.WriteLine("FileSystemHandler Added: " + this.Directory);

}

public void StopFileMonitor() {

Debug.WriteLine("Stoping File Monitor: " + this.Directory);

// Remove event handler;

if (_fileSystemChangedHandler != null) {

_fileSystemMonitor.Changed -= _fileSystemChangedHandler;

Debug.WriteLine("FileSystemChangedHandler Removed: " + this.Directory);


}

if (_fileSystemMonitor != null) {

_fileSystemMonitor.Dispose();

Debug.WriteLine("FileSystemMonitor Disposed");

//_fileSystemMonitor = null;

}

}



Hope that helps.




--
Tom Krueger

Smart Client DevCenter - http://msdn.microsoft.com/smartclient/
Mobile DevCenter - http://msdn.microsoft.com/mobility

This posting is provided "as is" with no warranties and confers no rights.
 
P

Petter L

I found out it whas not a perfect solution but it worked on small files but
big files it came back. So I looking into the other tip here also

Petter L.
 
B

Bjarke Lindberg

Petter said:
I found out it whas not a perfect solution but it worked on small files but
big files it came back. So I looking into the other tip here also

Hi Petter.

I've had the same problem. My solution was something like this:

/// <summary>
/// This method returns as soon as the specified file is writeable.
/// </summary>
/// <param name="fileName">The full path to the file.</param>
protected void waitForFile(string fileName) {
new FileWaiter(fileName).DoWait();
}

[...]
internal class FileWaiter {
public string fileName;

public FileWaiter(string fileName) {
this.fileName = fileName;
}

public void DoWait(int timeout) {
DoWait(new TimeSpan(0, 0, 0, 0, timeout));
}

public void DoWait() {
DoWait(TimeSpan.MinValue);
}

public void DoWait(TimeSpan timeout) {
System.Diagnostics.Debug.Assert(filename != null);
Thread t = new Thread(new ThreadStart(ThreadWait));
t.Start();
if (timeout > TimeSpan.MinValue)
t.Join(timeout);
else
t.Join();
}

void ThreadWait() {
do {
//Start waiting..
Thread.Sleep(500);
}
while (System.IO.File.Exists(fileName) && isFileOpen(fileName));
}
}

protected static bool isFileOpen(string fileName) {
System.IO.FileStream s = null;
try {
s = System.IO.File.Open(fileName, System.IO.FileMode.Open,
System.IO.FileAccess.ReadWrite,
System.IO.FileShare.None);
return false;
} catch (System.IO.IOException) {
return true;
} finally {
if (s != null)
s.Close();
}
}
[...]
 
P

Paul

I appreciate the code guys and the time spent replying but I'm afraid I
don't have a clue on how introduce any of it into my project as the two
examples posted have been in C as opposed to VB. I just realised I posted
this in the the dotnet.general NG aswell as the donet.languages.vb NG which
I suppose is why I got replies in C!
Sorry for the mistake. I guess I'll keep on looking!

Cheers,
Paul
 
P

petterl

I have tried to make something out of the C code into VB.Net code but now
even when the isopen routine says that the file is ready to be copied, i
thet the same message that the file is in use by other process.

here is copy of the routine w/ IsFileOpen

Private Sub ProcessFile(ByVal file_name As String, ByVal shName As String)

Dim wfile As File



fslog.lstFiles.Items.Add(Now.ToString() & " Processed " & file_name)

If shName = "Thumbs.db" Then ' = LCase(file_name) Then

Exit Sub

End If

fslog.lstFiles.Items.Add("file exist " & wfile.Exists(file_name))

fslog.lstFiles.Items.Add("Is file in use " & isFileOpen(file_name))



Do

System.Threading.Thread.Sleep(500)

Loop While wfile.Exists(file_name) And isFileOpen(file_name)



wfile.Copy(file_name, ToPath & shName)



MessageBox.Show(shName & " har blitt kopiert inn på " & LookPath,
"Watcher message", MessageBoxButtons.OK)

fslog.lstFiles.Items.Add(Now.ToString() & " " & shName & " har blitt
kopiert inn på " & LookPath)



wfile = Nothing

End Sub





Private Function isFileOpen(ByVal filename As String) As Boolean

Dim sf As System.IO.FileStream

Try

sf = System.IO.File.Open(filename, FileMode.Open)

Return False

Catch ex As System.IO.IOException

Return True

ex = Nothing

Finally

If Not IsNothing(sf) Then

sf = Nothing

End If



End Try

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top