Why is "My" namespace unavailable?
Our Winform app consists of a Main.exe and a Launcher.exe. The purpose of the Launcher app is to check for updated versions of the Main.exe.
If it finds no updates it launches Main.exe using System.Diagnostics.Process.Start
If it finds an update, it copies the new Main.exe into position and launches Main.exe the same way (in this case it will be a fresh copy of Main.exe).
Here's the Process.Start code:
Dim p As New ProcessStartInfo
p.FileName = "Main.exe"
p.WindowStyle = ProcessWindowStyle.Normal
Process.Start(p)
This code executes whether or not there's been an update and always successfully fires up Main.exe.
However our Main.exe, if it's being run for the first time (ie. after an update), will error on any line which references the My namespace, such as My.Settings or My.Computer.FileSystem
For example this line will cause an error:
Msgbox(My.Computer.FileSystem.SpecialDirectories.Desktop)
Here's the error:
System.IO.DirectoryNotFoundException: Could not find special directory 'Desktop'. at Microsoft.VisualBasic.FileIO.SpecialDirectories.GetDirectoryPath(String Directory, String DirectoryNameResID) at Microsoft.VisualBasic.FileIO.SpecialDirectories.get_Desktop() at Microsoft.VisualBasic.MyServices.SpecialDirectoriesProxy.get_Desktop()
But while this example refers to Desktop remember开发者_Go百科 it can't find any of the SpecialDirectories. It's not restricted to Desktop.
But only the first time it's run (ie. immediately after an update). Thereafter it will run fine.
If the process failed more spectacularly, to do with file system issues, locks or threads, it would be more understandable. But why just this "minor" problem with the My namespace?
I realise now it was nothing to do with the copied file and everything to do with a badly implemented System.Security.Principal.WindowsImpersonationContext.
Amazing how these things become clear 5 minutes after posting.
精彩评论