Reply

The process cannot access the file %1 because it is being used by another process.

[ Edited ]

Hi,

I'm using the following function to get the MD5Hash of a Solid Edge file:

Protected Function GetMD5HashFromFile(ByVal fileName As String) As String Dim file As New System.IO.FileStream(fileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read) Dim md5 As MD5 = New MD5CryptoServiceProvider() Dim retVal As Byte() = md5.ComputeHash(file) file.Close() Dim sb As New System.Text.StringBuilder() For i As Integer = 0 To retVal.Length - 1 sb.Append(retVal(i).ToString("x2")) Next Return sb.ToString() End FunctionThis works fine for most of the files but some are open by users while the program is running. Even though I use IO.FileAccess.Read I'm getting "The process cannot access the file [filename] because it is being used by another process.".

My understanding was that I was opening the file read only, but its obviously not that simple. When I open the particular file I get the standard SE "Read-Only Assistant" dialog:

 

Just to be clear, I'm fine with the files being open while the program runs, I just need the MD5Hash for the file in its last saved state.

Any help greatly appreciated.

Thanks.

Alex

 

Posted by: Alex Lush
Post date: 2/8/2012 12:37:04 AM

3 REPLIES

RE: The process cannot access the file %1 because it is being used by another process.

[ Edited ]

If the file is opened and you can't open in read mode ... there is one thing you could try.

 

Copy the file using a temp file name, open it, do your stuff, close it and delete it.

 

Tho I'm not sure if the copy will work if it's already opened .. same problem may happen.

 

 

 

Posted by: David Shooner
Post date: 2/8/2012 8:50:54 AM

RE: The process cannot access the file %1 because it is being used by another process.

[ Edited ]

Hi David, thanks for the reply.

 

I did think about trying the save a temp copy method but would like to avoid this if possible.

 

I'm thinking of contacting GTAC with this, before I do tho, can somebody confirm that my assumption that the "IO.FileAccess.Read" *should* open the file as read only regardless of whether anyone else has it open or not??

 

Thanks.

 

Alex

 

 

Posted by: Alex Lush
Post date: 2/8/2012 9:02:00 PM

Re: The process cannot access the file %1 because it is being used by another process.

[ Edited ]

This example is in C# but should be easy to translate to VB. I've had to read a locked file in the past so I knew how to do it with FileStream class. Hope this helps.

 

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace ReadLockedFile
{
    class Program
    {
        static void Main(string[] args)
        {
            string file = @"C:\Part1.par";  /* File is currently open in Solid Edge (locked */
            using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                string hash = GetMD5HashFromStream(fileStream);
            }
        }

        static string GetMD5HashFromStream(Stream stream)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            StringBuilder sb = new StringBuilder();

            byte[] retVal = md5.ComputeHash(stream);

            for (int i = 0; i < retVal.Length; i++)
            {
                /* Translate to HEX */
                sb.Append(retVal[i].ToString("x2"));
            }

            return sb.ToString();
        }
    }
}

 

Posted by: Jason Newell
Post date: 2/14/2012 11:09:34 PM