My code saves 2 files with same path and filename, but different extensions.

When the installed program runs on another PC, where I cannot install VS, a strange bug occurs:

Both files should be saved on the same folder, but the first file gets saved on the system temp folder. The second file gets saved on the right folder.

I cannot reproduce this bug on the PC were I make and debug the program. On my PC both files are saved on the same folder.

On summary, the user gives this file path: "C:\somepath\file.rst"

Both files should be saved as


but they are saved here:



Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
´This block ask the user a full path file to be created, with .rst extension. I removed extra code for clarity

                With SaveFileDialog1
                    MyResult = .ShowDialog()
                    If Len(.FileName) = 0 OrElse MyResult = DialogResult.Cancel Then
                        Throw New NoNameException
                        PathRST = .FileName
                    End If
                End With 'SaveFileDialog1

            Catch e As NoNameException
                Throw New NoNameException
            End Try
        Loop While sFile = EmptyString

Dim TextoRDC As New StringBuilder

|      ´this block creates a text to be saved on a text file with same name as PathRST without extension
|        With TextoRDC
|            .AppendLine("file format : IDRISI Raster A.1")
|            'Extracts the filename without extension
|            .AppendLine("file title  : Indice " & Regex.Replace(My.Computer.FileSystem.GetName(PathRST), _
|                                                                "\.rst$", _
|                                                                "", _
|                                                                RegexOptions.CultureInvariant Or _
|                                                                RegexOptions.Multiline Or _
|                                                                RegexOptions.Singleline Or _
|                                                                RegexOptions.IgnoreCase))
|        End With 'TextoRDC
|      ´here the rst extension is replaced with .RDC extensión, and sent to a function to save the text
|        SaveTextOnFile(TextoRDC.ToString, Regex.Replace(PathRST, "\.rst$", ".RDC", _
|                                                               RegexOptions.CultureInvariant Or _
|                                                               RegexOptions.Multiline Or _
|                                                               RegexOptions.Singleline Or _
|                                                               RegexOptions.IgnoreCase))

SaveOtherFile(binaryData, PathRST)

This is the function who saves the first text on file

   Public Sub SaveTextOnFile(ByRef TextToSave As String, _
                             ByVal PathOfFile As String, _
                             Optional ByVal FileEncoding As Encoding = Nothing)

        If FileEncoding Is Nothing Then FileEncoding = UTF8

        Dim fs As FileStream = File.Open(PathOfFile , FileMode.Create)
        Dim sw As New StreamWriter(fs, FileEncoding )

        sw.Write(TextToSave )

    End Sub

The problem is that SaveTextOnFile, but the folder where the file is saved is not the one selected by the user (saved in TextoRDC).

This is the function who saves the second file:

            Friend Shared Sub SaveOtherFile(ByRef Data(,) As Double, _
                                             ByRef PathRSTFile As String)
                Dim FS As New FileStream(PathArchivoRST, FileMode.Create, FileAccess.Write)
                Dim BW As New BinaryWriter(FS)

                Dim UboundRows As Integer = UBound(Data, 1)
                Dim UboundColumns As Integer = UBound(Data, 2)

                For Row As Integer = 0 To UboundRows
                    For Column As Integer = 0 To UboundColumns
                        BW.Write(CSng(Data(Row, Column)))
                    Next Column
                Next Row
            End Sub 'SaveOtherFile