Check if Excel file is being used

Continuing the discussion from Check if file is occupied:

Hello,
I’m getting an error message from the Read Range activity: The process cannot access the file XXX because it is being used by another process.

I would like to check whether the file is in use and have found the link to the TryCatch check file.Open(FileMode.Open, FileAccess.Read, FileShare.None), but I don’t know where to put that setting. So far I have the following:

EDIT: I don’t want to KillProcess as I don’t manage the file so somebody could genuinely be editing it. The file is also located on the shared drive.
Ideally I would like to be able to check if it’s open and then decide to act upon it.

Any help would be much appreciated.
Regards,
byuli

1 Like

@byuli

Before read range Activity, use Kill Process Activity and pass the process name as Excel to kill it.

hi, sorry should’ve explained it better. I don’t want to kill the process because the file is not managed by me so somebody could be genuinely trying to amend it. The file is also located on the shared drive.
Ideally I would just like to see if it’s being used and then I’ll retry later/once available/or when next scheduled.

@byuli Refer here

Hello, Watch this video. With the help of this, you will get to know How to Kill Current’s user’s process only. Please let me know if it works.

@byuli Check below workflow once.

Test.xaml (11.9 KB)

1 Like

@byuli - I would recommend using a try-catch in this situation like you mentioned. The try block should contain the workbook read range activity. The Catch should be for System.IO.IOException. So far so good based on your screenshot - no need to do anything with the file.Open(FileMode.Open, FileAccess.Read, FileShare.None) portion written in your post.

If the file is open, then it will throw the IOException and will move into your catch block and perform any activities you put there. If it is not open, it will simply move onto the next activity outside of your try-catch block.

2 Likes

I was literally just implementing this into my process, because in order for multi-thread processing, you must be able to update a file from multiple robots at the same time.

I was going to use Read Range to test if file is in use, but it was going to take half a second too long in my opinion, causing latency for the multi-thread approach. I found that using a Filestream was super fast. - but remember to Close and Dispose of the filestream.

Here is a snippet:

where outputFilestream is of type System.IO.Filestream, and here is the code:
System.IO.File.Open(outputLogFilepath, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite)

The Retry has a delay of 10 seconds with a retry number (right now) set to 6, so it will wait for 60seconds for the file to become available. I also in this approach set ContinueOnError to True, so it will write to a copy of the file even if the file never becomes available (it could happen).

You can replace the Retry for a Try/Catch as well as was suggested. Like I said, for multi-threading, I was wanting to wait a certain period of time to see if the file becomes available.

Appreciate any additional feedback from others as well.

Regards.

9 Likes

Thank you! I will use this as my solution, it seems simple enough to use while I’m learning.

Thank you very much for this, I’m not familiar with multi-threading just yet, but I will certainly study your post to see what it means and whether I can use it. Your support is much appreciated!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.