File Save Bug

marraco

New member
Joined
Aug 13, 2013
Messages
2
Programming Experience
Beginner
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

C:\somepath\file.rdc
C:\somepath\file.rst

but they are saved here:

C:\Users\UserName\AppData\Local\Temp\file.rdc
C:\somepath\file.rst

VB.NET:
[COLOR="#696969"]
Friend WithEvents SaveFileDialog1 As System.Windows.Forms.SaveFileDialog
      
[COLOR="#808080"]?This block ask the user a full path file to be created, with .rst extension. I removed extra code for clarity[/COLOR]

         Do
            Try[/COLOR]
                With SaveFileDialog1
                    MyResult = .ShowDialog()
                    If Len(.FileName) = 0 OrElse MyResult = DialogResult.Cancel Then
                        Throw New NoNameException
                    Else
                        [COLOR="#FF0000"]PathRST [/COLOR]= .FileName
                    End If
                End With 'SaveFileDialog1

            [COLOR="#696969"]Catch e As NoNameException
                Throw New NoNameException
            End Try
        Loop While sFile = EmptyString
[/COLOR]
Dim [COLOR="#008000"]TextoRDC [/COLOR]As New StringBuilder

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


[COLOR="#DAA520"]SaveOtherFile[/COLOR](binaryData, [COLOR="#FF0000"]PathRST[/COLOR])


This is the function who saves the first text on file

VB.NET:
   Public Sub [COLOR="#0000FF"]SaveTextOnFile[/COLOR](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 )

        sw.Flush()
        fs.Flush()
        sw.Close()
        fs.Close()
    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:

VB.NET:
            Friend Shared Sub [COLOR="#DAA520"]SaveOtherFile[/COLOR](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
                BW.Close()
                FS.Close()
            End Sub 'SaveOtherFile
 
It's possible that the application doesn't have permissions to write the *.rdc file in the specified location. In the SaveOtherFile method you are creating the FileStream with write access. Try modifying the SaveTextOnFile method to open the file with write access.

Good luck!
 
I found the problem. Old code.

That PC had an old, buggy, version installed.

I discovered it when I saw the program behaving like an old version. I didn't even knew that it had this bug.

I installed a new version using remote control, and it worked fine...
 
Back
Top