Results 1 to 16 of 16

Thread: [RESOLVED] Unexplainable file save bug

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2013
    Posts
    8

    Resolved [RESOLVED] Unexplainable 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(PathRSTFile, 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
    Last edited by marraco; Aug 13th, 2013 at 06:14 PM. Reason: fixed posting mistake

  2. #2
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: Unexplainable file save bug

    Welcome to VBF

    You are using "PathRST" for both Save-Methods, however in the first method you are using a Regex.Replace together with a FileSystem.Getname. The later will strip the file-name ONLY from the complete path&filename! IMHo that would be the reason for saving in the Temp folder!
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2013
    Posts
    8

    Re: Unexplainable file save bug

    Quote Originally Posted by opus View Post
    Welcome to VBF

    You are using "PathRST" for both Save-Methods, however in the first method you are using a Regex.Replace together with a FileSystem.Getname. The later will strip the file-name ONLY from the complete path&filename! IMHo that would be the reason for saving in the Temp folder!
    But Getname only modifies his output to the variable TextoRDC, which does not stores the path. The path is stored on PathRST variable.

    If Getname were changing PathRST, then the second function call SaveOtherFile would also receive a modified path, and both files would be saved on the same folder.

    But the second file is saved on the right folder, so PathRST is not modified.

    SaveTextOnFile "receives" ByVal PathRST, so it can modify the path without changing PathRST.

    And yes, it does not receive PathRST, but a regex output (string ). But as I understand, the regex only replaces the file extension, not the path.
    If the regex changes the path, how does it know that the string it is processing is a path, and why it would make a system call to get the temp folder? Makes nonsense.

    The main suspect, I think, are the lines

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

    It makes sense for the OS to create a file on a system temp folder, and then failing to move it to the actual folder, but why would that occur?
    Last edited by marraco; Aug 13th, 2013 at 08:25 PM. Reason: replaced wrong references to TextoRDC with the correct PathRST var

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Unexplainable file save bug

    You seem to have some very odd ideas about the appropriateness of ByVal and ByRef in your Sub declarations. I haven't looked at them in detail but there's at least one where you declare ByRef and in the first line of the code use its value. I'd frankly be surprised if you are actually using anything ByRef at all.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2013
    Posts
    8

    Re: Unexplainable file save bug

    Quote Originally Posted by dunfiddlin View Post
    You seem to have some very odd ideas about the appropriateness of ByVal and ByRef in your Sub declarations. I haven't looked at them in detail but there's at least one where you declare ByRef and in the first line of the code use its value. I'd frankly be surprised if you are actually using anything ByRef at all.
    I fixed wrong references to TextoRDC with the correct PathRST var on my last post. (it was a copy paste mistake of the formated bbcode).

    Please correct me if I'm wrong: As I understand, ByRef passes a pointer to the var, and byVal passes a copy of the var. I did it this way because the data to be saved may be large, and I do not want to waste time copying large data (sometimes, it is really huge). But I pass the path ByVal because it is a short string.

    The ByRef PathRSTFile on the SaveTextOnFile function was just some editing trial I did when trying to debug, out of "despair" for not understanding what is going on.

  6. #6
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Unexplainable file save bug

    Quote Originally Posted by dunfiddlin View Post
    You seem to have some very odd ideas about the appropriateness of ByVal and ByRef in your Sub declarations. I haven't looked at them in detail but there's at least one where you declare ByRef and in the first line of the code use its value. I'd frankly be surprised if you are actually using anything ByRef at all.
    Huh? Whilst I agree there's some weird and pointless usage of ByRef here because the types being passed in are reference types so you're effectively always using them "ByRef" anyway (i.e. you've just got a reference/pointer to them even if you pass them in ByVal, using ByRef just means you've got a pointer to a pointer), I don't see understand why you say there's a problem with passing in ByRef and then using its value. What's wrong with that? It is modifying its value that can cause unexpected issues (if the person is not completely familiar with how reference types etc work) but I don't see any harm in just using its value. If you couldn't use it then what would be the point in even being able to pass something in ByRef? In this particular example they should ideally all be ByVal as far as I can see just like they normally would be, but using ByRef isn't actually doing any harm here.
    While we're on the ByRef/ByVal subject:

    Quote Originally Posted by marraco
    SaveTextOnFile "receives" ByVal PathRST, so it can modify the path without changing PathRST.
    But SaveTextOnFile never modifies the path anyway as far as I can see. Even if it did, it wouldn't affect the variable in your other method because Strings are immutable - that means that a new instance is created and returned every time you modify a string, so any changes you make to one reference will not affect another reference to that string because its now two different references to two different string instances.

    You should generally pass everything ByVal anyway, it is very rare that you'll need to use ByRef.
    Last edited by chris128; Aug 13th, 2013 at 08:57 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  7. #7
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Unexplainable file save bug

    Quote Originally Posted by marraco View Post
    I fixed wrong references to TextoRDC with the correct PathRST var on my last post. (it was a copy paste mistake of the formated bbcode).
    Yeah I thought something seemed to not make sense after I just looked back at your original code

    Quote Originally Posted by marraco View Post
    Please correct me if I'm wrong: As I understand, ByRef passes a pointer to the var, and byVal passes a copy of the var. I did it this way because the data to be saved may be large, and I do not want to waste time copying large data (sometimes, it is really huge). But I pass the path ByVal because it is a short string.
    Yes you are correct but there's one big thing that perhaps you don't know. If you pass a "reference type" object in ByVal then you're just passing in a copy of the reference to that object, not the object itself (so you don't need to worry about large amounts of data). ByRef only really has an effect on "value type" objects. So how do you know if something is a reference type or a value type? Well basically anything that is a Class is a reference type, and anything that is a Structure is a value type. The vast majority of things you will use will be reference types, apart from primitive types such as Integer, Byte, Int64 (they are all value types). The only primitive type that is not a value type is String, as that is actually a Class... but it behaves a lot like a value type because it is immutable, as explained in my previous post. You can find a definitive list of the built in Value Types here: http://msdn.microsoft.com/en-us/libr...(v=vs.90).aspx (note the explanation below it stating that everything in that list is a Value Type other than String and Object)

    Oh and why do you actually need that RegEx? Isn't it just replacing the file extension at the end of the path? If that's all it is doing then a much simpler way would be to just trim the end of the string by 4 characters, e.g:

    Code:
    With TextoRDC
              .AppendLine("file format : IDRISI Raster A.1")
              .AppendLine("file title  : Indice " & PathRST.Remove(PathRST.Length - 4, 4))
    End With 
    
    SaveTextOnFile(TextoRDC.ToString, PathRST.Remove(PathRST.Length - 4, 4) & ".RDC")
    SaveOtherFile(binaryData, PathRST)
    Sorry if the syntax for the Remove method is not completely correct, just writing it off the top of my head
    Last edited by chris128; Aug 13th, 2013 at 09:15 PM.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Unexplainable file save bug

    If you want to save two files with the same path but the extension then you should be using the Path.ChangeExtension method, e.g.
    Code:
    Dim path1 = "C:\Folder\Subfolder\FileName.txt"
    Dim path2 = IO.Path.ChangeExtension(path1, ".dat")
    
    MessageBox.Show(path1)
    MessageBox.Show(path2)

  9. #9
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Unexplainable file save bug

    Oh cool I've never noticed that method before (then again I've never needed to do this before). Just took a look at it in Reflector to see if it does anything special and basically it just loops backwards through the characters in the string until it finds a full stop, then creates a substring from 0 to that position where the full stop was, then adds your new extension to that substring. Still, better to use that than re write the same thing yourself I guess
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  10. #10

    Thread Starter
    New Member
    Join Date
    Aug 2013
    Posts
    8

    Re: Unexplainable file save bug

    Quote Originally Posted by chris128 View Post
    Yeah I thought something seemed to not make sense after I just looked back at your original code



    Yes you are correct but there's one big thing that perhaps you don't know. If you pass a "reference type" object in ByVal then you're just passing in a copy of the reference to that object, not the object itself (so you don't need to worry about large amounts of data). ByRef only really has an effect on "value type" objects. So how do you know if something is a reference type or a value type? Well basically anything that is a Class is a reference type, and anything that is a Structure is a value type. The vast majority of things you will use will be reference types, apart from primitive types such as Integer, Byte, Int64 (they are all value types). The only primitive type that is not a value type is String, as that is actually a Class... but it behaves a lot like a value type because it is immutable, as explained in my previous post. You can find a definitive list of the built in Value Types here: http://msdn.microsoft.com/en-us/libr...(v=vs.90).aspx (note the explanation below it stating that everything in that list is a Value Type other than String and Object)
    Thanks for the valuable lesson
    Quote Originally Posted by chris128 View Post
    Oh and why do you actually need that RegEx? Isn't it just replacing the file extension at the end of the path? If that's all it is doing then a much simpler way would be to just trim the end of the string by 4 characters, e.g:
    I see it as "specifying the result" instead of "programming the code".

    It specifies replacing the string ".rst" at the end of the string with ".rdc".

    Otherwise, I would need to write the code to check if the string ends with ".rst", then delete it, and then add the ".rdc" string. That's takes longer lines of code, which are harder to read and maintain to me.

    Of course, there are many alternative solutions, but my code did a lot of regex, so at the time of writing these lines, it seemed natural to use that solution instead of other.

    Anyways, I'm no expert on regex, and I recognize that they may be treacherous.

  11. #11

    Thread Starter
    New Member
    Join Date
    Aug 2013
    Posts
    8

    Re: Unexplainable file save bug

    Quote Originally Posted by jmcilhinney View Post
    If you want to save two files with the same path but the extension then you should be using the Path.ChangeExtension method, e.g.
    Code:
    Dim path1 = "C:\Folder\Subfolder\FileName.txt"
    Dim path2 = IO.Path.ChangeExtension(path1, ".dat")
    
    MessageBox.Show(path1)
    MessageBox.Show(path2)
    That's an useful hint.

    Do you know why there are 2 versions of Io.Path, and what one is the one working?


  12. #12
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Unexplainable file save bug

    Quote Originally Posted by marraco View Post

    Do you know why there are 2 versions of Io.Path, and what one is the one working?

    One of them is coming from Silverlight and one of them is coming from the full .NET Framework. You can tell this by right clicking on one of them and going to Browse Definition, then scrolling up to see which heading it comes under - "mscorlib [2.0.0.0]" is the full .NET Framework and mscorlib [2.0.5.0] is Silverlight. You can tell this by clicking on one of them, you'll then see this in the details pane at the bottom:

    Member of .NET Framework 2.0, .NET Framework 3.0, .NET Framework 3.5, My Solution
    C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll

    or for the other one you'll see this:

    Member of Silverlight 3.0, My Solution
    C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v3.0\mscorlib.dll
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  13. #13

    Thread Starter
    New Member
    Join Date
    Aug 2013
    Posts
    8

    Re: Unexplainable file save bug

    Quote Originally Posted by chris128 View Post
    One of them is coming from Silverlight and one of them is coming from the full .NET Framework. You can tell this by right clicking on one of them and going to Browse Definition, then scrolling up to see which heading it comes under - "mscorlib [2.0.0.0]" is the full .NET Framework and mscorlib [2.0.5.0] is Silverlight. You can tell this by clicking on one of them, you'll then see this in the details pane at the bottom:

    Member of .NET Framework 2.0, .NET Framework 3.0, .NET Framework 3.5, My Solution
    C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll

    or for the other one you'll see this:

    Member of Silverlight 3.0, My Solution
    C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v3.0\mscorlib.dll
    I also found a mscorlib [4.0.0.0], member of .NET Framework 4.0.

    But why .NET Framework 4 is not listed and Silverlight is listed?

    My application targets .NET Framework 3.5, so it makes sense that mscorlib [4.0.0.0] is not listed, but I never used neither added a reference to Silverlight.

    I'm worried that it is linked, and I may be using it without knowing. How do I determine if the installed code on another PC will require Silverlight?

  14. #14
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Unexplainable file save bug

    No it is not linked to your project at all, you don't need to worry about it - I get the exact same behaviour on my projects that have nothing to do with Silverlight and don't require it. The only time you'll require Silverlight on the end user's computers is if you've specifically created a Silverlight project.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  15. #15

    Thread Starter
    New Member
    Join Date
    Aug 2013
    Posts
    8

    Re: Unexplainable file save bug

    Quote Originally Posted by chris128 View Post
    No it is not linked to your project at all, you don't need to worry about it - I get the exact same behaviour on my projects that have nothing to do with Silverlight and don't require it. The only time you'll require Silverlight on the end user's computers is if you've specifically created a Silverlight project.
    That's good to know.

    I made this program to simplify my own work, but then I shared it, and now I'm in charge of free maintenance. The issues seem to explode exponentially with each new user. So when I saw the Silverlight thing a chill ran over my spine. Another bug source!

  16. #16

    Thread Starter
    New Member
    Join Date
    Aug 2013
    Posts
    8

    Re: Unexplainable file save bug

    This thread is solved. That PC had a very old version installed.

    I tough that the last version was installed. I noticed that something was wrong when I found a behavior that I patched long time ago.

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