CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    [RESOLVED] Unexplained FIle save bug

    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

    Code:
    
    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
    
             Do
                Try
                    With SaveFileDialog1
                        MyResult = .ShowDialog()
                        If Len(.FileName) = 0 OrElse MyResult = DialogResult.Cancel Then
                            Throw New NoNameException
                        Else
                            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

    Code:
       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 )
    
            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:

    Code:
                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
                    BW.Close()
                    FS.Close()
                End Sub 'SaveOtherFile
    [Vb.NET 2008 (ex Express)]

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Unexplained FIle save bug

    Seems pretty clear.

    You are not using the path/filename that was passed to the sub in the second save sub

    Code:
      Friend Shared Sub SaveOtherFile(ByRef Data(,) As Double, _
                                                 ByRef PathRSTFile As String)
                    Dim FS As New FileStream(PathArchivoRST, FileMode.Create, FileAccess.Write)
    The part about it working differently on a different computer may just be a red herring
    Last edited by DataMiser; August 13th, 2013 at 05:22 PM.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: Unexplained FIle save bug

    Quote Originally Posted by DataMiser View Post
    Seems pretty clear.

    You are not using the path/filename that was passed to the sub in the second save sub

    Code:
      Friend Shared Sub SaveOtherFile(ByRef Data(,) As Double, _
                                                 ByRef PathRSTFile As String)
                    Dim FS As New FileStream(PathArchivoRST, FileMode.Create, FileAccess.Write)
    The part about it working differently on a different computer may just be a red herring
    Thanks for your answer, but unfortunately, that's not the bug. I made a mistake on this post, because I translated variable names from Spanish to English, to the purpose of posting, and forgot to translate it. The original code is this:

    Code:
        Public Sub GuardarTextoEnArchivo(ByRef TextoAGuardar As String, _
                                         ByVal PathDeArchivo As String, _
                                         Optional ByVal Codificación As Encoding = Nothing)
    
            If Codificación Is Nothing Then Codificación = UTF8
    
            Dim fs As FileStream = File.Open(PathDeArchivo, FileMode.Create)
            Dim sw As New StreamWriter(fs, Codificación)
    
            sw.Write(TextoAGuardar)
    
            sw.Flush()
            fs.Flush()
            sw.Close()
            fs.Close()
        End Sub
    [Vb.NET 2008 (ex Express)]

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Unexplained FIle save bug

    In that case have you tried adding any logging to see what the value of your path variable is at different points in that first block of code? Namely before and after each call to the subs that save the data?
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: Unexplained FIle save bug

    Quote Originally Posted by DataMiser View Post
    In that case have you tried adding any logging to see what the value of your path variable is at different points in that first block of code? Namely before and after each call to the subs that save the data?
    Yes, but it work flawlessly on my PC. Is on another PC were it does not works.

    The only way I can log on that PC is sending a new compiled program. I know that an exception is not fired, because the program catches the exceptions, and if them are not managed, they emerge to a GUI warning, and the user would had seen it.

    The other PC is on another city, but I ran the program personally on that PC with Teamviewer, and I did not saw an exception.

    Anyways, I plan to send some patched program, to get a log, but I would prefer to have a clue before sending it.
    [Vb.NET 2008 (ex Express)]

  6. #6
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: Unexplained FIle save bug

    Quote Originally Posted by Marraco View Post
    Thanks for your answer, but unfortunately, that's not the bug. I made a mistake on this post, because I translated variable names from Spanish to English, to the purpose of posting, and forgot to translate it. The original code is this:

    Code:
        Public Sub GuardarTextoEnArchivo(ByRef TextoAGuardar As String, _
                                         ByVal PathDeArchivo As String, _
                                         Optional ByVal Codificación As Encoding = Nothing)
    
            If Codificación Is Nothing Then Codificación = UTF8
    
            Dim fs As FileStream = File.Open(PathDeArchivo, FileMode.Create)
            Dim sw As New StreamWriter(fs, Codificación)
    
            sw.Write(TextoAGuardar)
    
            sw.Flush()
            fs.Flush()
            sw.Close()
            fs.Close()
        End Sub
    I made another post error. I should had posted this

    Code:
                Friend Shared Sub SalvarAArchivo(ByRef Datos(,) As Double, _
                                                 ByRef PathArchivoRST As String)
    
                    Dim FS As New FileStream(PathArchivoRST, FileMode.Create, FileAccess.Write)
                    Dim BW As New BinaryWriter(FS)
    
                    Dim UboundFilas As Integer = UBound(Datos, 1)
                    Dim UboundColumnas As Integer = UBound(Datos, 2)
    
                    For Fila As Integer = 0 To UboundFilas
                        For Columna As Integer = 0 To UboundColumnas
                            BW.Write(CSng(Datos(Fila, Columna)))
                        Next Columna
                    Next Fila
    
                    BW.Close()
                    FS.Close()
                End Sub 'SalvarAArchivo
    [Vb.NET 2008 (ex Express)]

  7. #7
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Unexplained FIle save bug

    I doubt that having VS installed has anything to do with the issue you are running into. OS maybe, User settings perhaps, file security settings maybe, If it were me I would go with a log ASAP have a look at it and then go from there.

    Curious were you able to reproduce the issue when you were logged into the system via teamviewer?
    Always use [code][/code] tags when posting code.

  8. #8
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: Unexplained FIle save bug

    Quote Originally Posted by DataMiser View Post
    I doubt that having VS installed has anything to do with the issue you are running into. OS maybe, User settings perhaps, file security settings maybe, If it were me I would go with a log ASAP have a look at it and then go from there.

    Curious were you able to reproduce the issue when you were logged into the system via teamviewer?
    Yes, I was able to reproduce the issue on that PC.


    About having VS installed, the original code was made on XP, where I did some mistakes, like reading and writing some files on the C:\Program Files\Program folder

    It doesn't works on Vista, 7 and 8, unless it is run with administrator privileges.

    The point is that VS run flawlessly on my Win 7 PC, even when I do not run with administrator privileges, so that's makes hard to detect permissions problems.
    I ran VS 2010 on an account without administrator privileges, but the code works flawlessly (yet VS F8 key doesn't works for debugging)
    [Vb.NET 2008 (ex Express)]

  9. #9
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Unexplained FIle save bug

    You will run into that same issue wit or without VS installed if you are trying to write to program files folder unless you have the UAC disabled in which case you will not get the error but it still does not matter if VS is or is not installed.
    Always use [code][/code] tags when posting code.

  10. #10
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: Unexplained File save bug

    Quote Originally Posted by DataMiser View Post
    You will run into that same issue wit or without VS installed if you are trying to write to program files folder unless you have the UAC disabled in which case you will not get the error but it still does not matter if VS is or is not installed.
    My UAC is enabled, but my VS code writes on Program files even without admin rights.


    Anyways, the problem is solved.
    It was an old version which was installed on that PC.

    I gave for granted that the last version was installed.
    [Vb.NET 2008 (ex Express)]

  11. #11
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: [RESOLVED] Unexplained FIle save bug

    Not sure how you could have your PC set up then as Windows Vista and later will block writes to this folder if UAC is enabled unless you are launching using run as administrator or XP compatibility mode in which case I am not sure if it still blocks but I am 100% sure that the default configuration will block and give read only access.

    As a rule you should not try to write to the program files folder
    Always use [code][/code] tags when posting code.

  12. #12
    Join Date
    Mar 2007
    Location
    Argentina
    Posts
    579

    Re: [RESOLVED] Unexplained FIle save bug

    Quote Originally Posted by DataMiser View Post
    Not sure how you could have your PC set up then as Windows Vista and later will block writes to this folder if UAC is enabled unless you are launching using run as administrator or XP compatibility mode in which case I am not sure if it still blocks but I am 100% sure that the default configuration will block and give read only access.

    As a rule you should not try to write to the program files folder
    My plan was to just run the code on vista/7, and fix the bugs one by one, as they emerge when I run all the code. But VS does not raises any exception on seven. It works as if it had administrator privileges.
    [Vb.NET 2008 (ex Express)]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured