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?
Lloyd Sheen - 29 Jul 2004 19:22 GMT
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
> 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?
>
[quoted text clipped - 5 lines]
>
> Could this be locking my file? If so, how do I unlock it?
Stoitcho Goutsev \(100\) [C# MVP] - 29 Jul 2004 19:31 GMT
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.

Signature
Stoitcho Goutsev (100) [C# MVP]
> 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?
>
[quoted text clipped - 5 lines]
>
> Could this be locking my file? If so, how do I unlock it?
M K - 29 Jul 2004 20:23 GMT
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) [C# MVP]" wrote:
> M K,
>
[quoted text clipped - 20 lines]
> >
> > Could this be locking my file? If so, how do I unlock it?
Stoitcho Goutsev \(100\) [C# MVP] - 29 Jul 2004 23:25 GMT
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());

Signature
HTH
Stoitcho Goutsev (100) [C# MVP]
> 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?
>
[quoted text clipped - 24 lines]
> > >
> > > Could this be locking my file? If so, how do I unlock it?