Search code examples
vb.netwinformscrystal-reports

How to stop AppDomain.CurrentDomain.BaseDirectory change during execution.?


I've code in VB.NET windows forms.
I am using AppDomain.CurrentDomain.BaseDirectory for path location but it changes to changes to C:\Windows\System32\spool\Drivers\x64\3\ from other drives (D:\NB) automatically during exe execution.
Issue is I cannot find from where this changes?

NOTE :
I am using crystal reports SP35 version which is latest for now.
I've read somewhere that log4net changes this. if so I have a doubt on it, how do I stop it from changing?

Thanks.

UPDATE
Here is my little code for explanation.

Public Function ConReadData(FName As String, Ar As Integer, ObjectName As String)
    ConReadData = ""
    Try
        msgbox(FName)
        Dim json As String = File.ReadAllText(FName)
        Dim dist As Newtonsoft.Json.Linq.JArray = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Object)(json)
        ConReadData = dist.Item(Ar)(ObjectName)
    Catch ex As Exception
        Throw
    End Try
    Return ConReadData
End Function

Now the issue is the msgbox shows my current directory but after that it shows error regarding this this path. C:\Windows\System32\spool\Drivers\x64\3\

I am sure I haven't used this path anywhere in my project but still this is stuck. Main issue it that it don't show up everytime. I don't know what triggers this to generate the error.

I am also sending photo of the error,

Error

As you can see in the image, it's due to unhandled exception but in the function, I have handled it. Connection.json is my file.
This is not only with this file, after that if I try to export any file, the issue persists.


Solution

  • Consider doing it one of these 2 ways instead. It's almost as if one of the assemblies in your app is interfering and overwriting the expected value.

    METHOD 1 Use the code from METHOD 2 below to grab your path as soon as your app starts and store it to Settings. Reference the path stored in Settings in all other code.

    METHOD 2

    Dim appDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)
    Dim reportPath = Path.Combine(appDir, "Reports", "MyReport.rpt")
    ' Use reportPath for Crystal Reports processing
    

    You could also try (in WinForms) Application.ExecutablePath

    or

    System.Reflection.Assembly.GetEntryAssembly().Location