Why can't I delete this file? Does Assembly.LoadFile leave it lock

G

Guest

In my Windows Form, I have a io.File.Delete(filePath) command. Everyone has Full Control to the folder and file. I watch the file with FileMon and nothing opens the file in my code. (There is no code to open the file before the delete command.) I get an 'UnauthorizedAccessException'.

How can I find out what's causing this exception. What could keep me from deleting the file. Please help. This, should be, simple thing is holding up production.

I cannot delete the file while my app is running. Why?

I have this in my code before the File.Delete command:
Dim myAssem As System.Reflection.Assembly = System.Reflection.Assembly.LoadFile(thisPath & "\" & drCurrent("FileName"))
currentVersion = myAssem.GetName.Version
...'Save the Version to a dataTable
myAssem = Nothing

Could this be locking my file? If so, how do I unlock it?
 
L

Lloyd Sheen

The load assembly is locking the code. I have found (or not found a way
around it) that the only way to do this is to load the file into a byte
array and then load assembly from the byte array.

Lloyd Sheen

Code sample:
Private Function GetTextConverterAssembly(ByVal rsType As String) As
[Assembly]
Dim poBytes() As Byte = GetTextConverterByteCode(rsType)
Return [Assembly].Load(poBytes)
End Function

Private Function GetTextConverterByteCode(ByVal rsType As String) As Byte()
Dim psLangFile As String
psLangFile = Path.GetDirectoryName(Application.ExecutablePath)
psLangFile += "\TextConverters\TextConverter." + rsType + ".dll"
Dim fs As FileStream = New FileStream(psLangFile, FileMode.Open,
FileAccess.Read)

Dim TS As New BinaryReader(fs)
Dim poBytes() As Byte
poBytes = TS.ReadBytes(CInt(fs.Length))
TS.Close()
Return poBytes
End Function


M K said:
In my Windows Form, I have a io.File.Delete(filePath) command. Everyone
has Full Control to the folder and file. I watch the file with FileMon and
nothing opens the file in my code. (There is no code to open the file before
the delete command.) I get an 'UnauthorizedAccessException'.
How can I find out what's causing this exception. What could keep me from
deleting the file. Please help. This, should be, simple thing is holding up
production.
I cannot delete the file while my app is running. Why?

I have this in my code before the File.Delete command:
Dim myAssem As System.Reflection.Assembly =
System.Reflection.Assembly.LoadFile(thisPath & "\" & drCurrent("FileName"))
 
S

Stoitcho Goutsev \(100\) [C# MVP]

M K,

Since when load an assembly it cannot be unload it make sense that CLR keeps
the file open so nobody can delete it.

--

Stoitcho Goutsev (100) [C# MVP]


M K said:
In my Windows Form, I have a io.File.Delete(filePath) command. Everyone
has Full Control to the folder and file. I watch the file with FileMon and
nothing opens the file in my code. (There is no code to open the file before
the delete command.) I get an 'UnauthorizedAccessException'.
How can I find out what's causing this exception. What could keep me from
deleting the file. Please help. This, should be, simple thing is holding up
production.
I cannot delete the file while my app is running. Why?

I have this in my code before the File.Delete command:
Dim myAssem As System.Reflection.Assembly =
System.Reflection.Assembly.LoadFile(thisPath & "\" & drCurrent("FileName"))
 
G

Guest

So, I guess that's not a good way to look at an assembly's Version. Is there another way to look at an assembly's version?

Stoitcho Goutsev (100) said:
M K,

Since when load an assembly it cannot be unload it make sense that CLR keeps
the file open so nobody can delete it.

--

Stoitcho Goutsev (100) [C# MVP]


M K said:
In my Windows Form, I have a io.File.Delete(filePath) command. Everyone
has Full Control to the folder and file. I watch the file with FileMon and
nothing opens the file in my code. (There is no code to open the file before
the delete command.) I get an 'UnauthorizedAccessException'.
How can I find out what's causing this exception. What could keep me from
deleting the file. Please help. This, should be, simple thing is holding up
production.
I cannot delete the file while my app is running. Why?

I have this in my code before the File.Delete command:
Dim myAssem As System.Reflection.Assembly =
System.Reflection.Assembly.LoadFile(thisPath & "\" & drCurrent("FileName"))
currentVersion = myAssem.GetName.Version
...'Save the Version to a dataTable
myAssem = Nothing

Could this be locking my file? If so, how do I unlock it?
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi M K,

You can read all the assembly info without loading it via AssemblyName class

AssemblyName an = AssemblyName.GetAssemblyName(<your assembly file name>);
Console.WriteLine(an.Version.ToString());

--
HTH
Stoitcho Goutsev (100) [C# MVP]


M K said:
So, I guess that's not a good way to look at an assembly's Version. Is
there another way to look at an assembly's version?
Stoitcho Goutsev (100) said:
M K,

Since when load an assembly it cannot be unload it make sense that CLR keeps
the file open so nobody can delete it.

--

Stoitcho Goutsev (100) [C# MVP]


M K said:
In my Windows Form, I have a io.File.Delete(filePath) command.
Everyone
has Full Control to the folder and file. I watch the file with FileMon and
nothing opens the file in my code. (There is no code to open the file before
the delete command.) I get an 'UnauthorizedAccessException'.
How can I find out what's causing this exception. What could keep me
from
deleting the file. Please help. This, should be, simple thing is holding up
production.
I cannot delete the file while my app is running. Why?

I have this in my code before the File.Delete command:
Dim myAssem As System.Reflection.Assembly =
System.Reflection.Assembly.LoadFile(thisPath & "\" & drCurrent("FileName"))
currentVersion = myAssem.GetName.Version
...'Save the Version to a dataTable
myAssem = Nothing

Could this be locking my file? If so, how do I unlock it?
 

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