473,385 Members | 1,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

"Process cannot access file" problem

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
Nov 21 '05 #1
10 15841
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

"Paul" <pr*****@mail.com> wrote in message
news:41***********************@mercury.nildram.net ...
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

Nov 21 '05 #2
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.

"Paul" <pr*****@mail.com> wrote in message
news:41***********************@mercury.nildram.net ...
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

Nov 21 '05 #3
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

"petterl" <pe**********@hotmail.com> wrote in message
news:u2**************@TK2MSFTNGP12.phx.gbl...
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.

"Paul" <pr*****@mail.com> wrote in message
news:41***********************@mercury.nildram.net ...
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


Nov 21 '05 #4
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(FileSys temMonitor_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.

"Paul" <pr*****@mail.com> wrote in message
news:41***********************@mercury.nildram.net ...
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

Nov 21 '05 #5
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.
"Paul" <pr*****@mail.com> wrote in message
news:41*********************@mercury.nildram.net.. .
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

"petterl" <pe**********@hotmail.com> wrote in message
news:u2**************@TK2MSFTNGP12.phx.gbl...
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.

"Paul" <pr*****@mail.com> wrote in message
news:41***********************@mercury.nildram.net ...
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



Nov 21 '05 #6
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.
"Paul" <pr*****@mail.com> wrote in message
news:41*********************@mercury.nildram.net.. .
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

"petterl" <pe**********@hotmail.com> wrote in message
news:u2**************@TK2MSFTNGP12.phx.gbl...
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.

"Paul" <pr*****@mail.com> wrote in message
news:41***********************@mercury.nildram.net ...
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



Nov 21 '05 #7
Petter L wrote:
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();
}
}
[...]
Nov 21 '05 #8
Petter L wrote:
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();
}
}
[...]
Nov 21 '05 #9
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

"Tom Krueger [MSFT]" <a-******@online.microsoft.com> wrote in message
news:uV**************@TK2MSFTNGP15.phx.gbl...
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(FileSys temMonitor_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.

"Paul" <pr*****@mail.com> wrote in message
news:41***********************@mercury.nildram.net ...
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


Nov 21 '05 #10
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

"Paul" <pr*****@mail.com> wrote in message
news:41***********************@mercury.nildram.net ...
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

"Tom Krueger [MSFT]" <a-******@online.microsoft.com> wrote in message
news:uV**************@TK2MSFTNGP15.phx.gbl...
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(FileSys temMonitor_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.

"Paul" <pr*****@mail.com> wrote in message
news:41***********************@mercury.nildram.net ...
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



Nov 21 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Paul | last post by:
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...
2
by: Henrik | last post by:
Hi Im trying to make an app that uses threading, I want to write to a file in a loop. How can i ensure that only one thread writes to the file a the time??? Im accessing the fil with: ...
2
by: lucy | last post by:
Hi, I use ADOX to create a Access database programmatically from my asp.net application and save ".mdb" file to a temp folder under my project foler on the web server. After database is created...
7
by: RSH | last post by:
Hi, I have an ASP .Net web page that creates a temp directory on the server then it is using the File.Copy command to move a file to a temp download directory, Then I am doing a...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.