Write a text file in XP vs Win7
Write a text file in XP vs Win7
Write a text file in XP vs Win7
Write a text file in XP vs Win7
Write a text file in XP vs Win7
Write a text file in XP vs Win7 Write a text file in XP vs Win7 Write a text file in XP vs Win7 Write a text file in XP vs Win7 Write a text file in XP vs Win7 Write a text file in XP vs Win7 Write a text file in XP vs Win7 Write a text file in XP vs Win7
Write a text file in XP vs Win7 Write a text file in XP vs Win7
Write a text file in XP vs Win7
Go Back  Xtreme Visual Basic Talk > > > Write a text file in XP vs Win7


Reply
 
Thread Tools Display Modes
  #1  
Old 09-28-2011, 11:33 PM
easoftware easoftware is offline
Freshman
 
Join Date: Aug 2006
Posts: 48
Default Write a text file in XP vs Win7


I am using Visual Studio 2008 on Windows XP SP3. User has Windows 7 Pro (32 bit). The IO code below works for me. The read code works for user, but the write code only works for new files. If the user tries to overwrite a current data file, then no data is written to disk.

I have done all the usual IO checking (valid file name, etc.) and have excluded it from my same code. In my app, FileSave and FileSaveAs both call WriteDataFile(FullFileName).

Any reasons why FIleSave would work on Windows XP, but not on Windows 7 Pro (32 bit)?
Code:
Public Function ReadDataFile(ByVal FullFileName As String) As Integer ' reads a Data file from disk ' ' Format is a text file, with [TAB] seperators and [CR][LF] as line terminators ' this format can also be read by Excel and Notepad ' 1st line, Header data, all text: ' ItemName[TAB]Id[TAB]Units[TAB]Value1[TAB]Value2[TAB]Code[CR][LF] ' 2nd through Last row: ' [ItemName][TAB][Id][TAB][Value1][TAB][Value2][TAB][Code][CR][LF] ' ' vars passed: ' FullFileName - full file name (full path and file name) ' ' returns ' 0 - no errors ' -1 - other error ' -2 - read error Try Dim DataReader As New FileIO.TextFieldParser(FullFileName) ' get text file field parser DataReader.TextFieldType = FileIO.FieldType.Delimited ' text file is deliminated DataReader.SetDelimiters(vbTab) ' delininator is [TAB] char Dim ReadErr As Integer ' read error Dim TextRow As String() ' array of data for floor Dim RowCount As Integer = 0 ' row count While Not DataReader.EndOfData ' while got data Try TextRow = DataReader.ReadFields ' read one row of data If RowCount = 0 Then ' if first row, header row ReadErr = ParseHeaderLine(TextRow) ' parse data in header line of text Else ' else data line ReadErr = ParseDataLine(TextRow, RowCount - 1) ' parse data for line End If If ReadErr < 0 Then ' if got an in parse MessageBox.Show("Error reading data in file """ & FullFileName & """.", _ "Read Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Return -2 ' return the error End If RowCount += 1 ' increment row counter Catch InvalidDataEx As FileIO.MalformedLineException MessageBox.Show("Malformed data row # " & RowCount.ToString & " in data file """ & FullFileName & _ """. Cannot read file. " & InvalidDataEx.Message, _ "Read Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Catch ex As Exception Return -1 End Try End While Return 0 ' if got here, then no errors Catch ex As Exception MessageBox.Show("Other error in ReadDataFile. " & ex.Message, _ "Other Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Return -1 End Try End Function Public Function WriteDataFile(ByVal FullFileName As String) As Integer ' writes a data file to disk ' ' Format is a text file, with [TAB] seperators and [CR][LF] as line terminators ' this format can also be read by Excel and Notepad ' 1st line, Header data, all text: ' ItemName[TAB]Id[TAB]Units[TAB]Value1[TAB]Value2[TAB]Code[CR][LF] ' 2nd through Last row: ' [ItemName][TAB][Id][TAB][Value1][TAB][Value2][TAB][Code][CR][LF] ' ' vars passed: ' FullFileName - full file name (full path and file name) ' ' returns ' 0 - no errors ' -1 - other error ' -2 - io error Try Dim DataText As String ' can hold aprox 2 billion chars ' first line of file DataText = "ItemName" & vbTab & "Id" & vbTab & "Units" & vbTab & "Value1" & vbTab & "Value2" & vbTab & "Code" & vbCrLf ' data lines text For i As Integer = 0 To DataTable.DefaultView.Count - 1 ' for each row of data DataText &= GetDataTextToWrite(DataTable.DefaultView(i).Row) ' get data from row as formatted string Next My.Computer.FileSystem.WriteAllText(FullFileName, DataText, False) ' write the file to disk Return 0 ' if got here, then no errors Catch FileEx As System.IO.IOException MessageBox.Show("IO error in WriteDataFile. " & FileEx.Message, _ "IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Return -2 Catch ex As Exception MessageBox.Show("Other error in WriteDataFile. " & ex.Message, _ "Other Error", MessageBoxButtons.OK, MessageBoxIcon.Error) Return -1 End Try End Function

Last edited by Flyguy; 09-29-2011 at 01:57 AM. Reason: Added [code][/code] tags
Reply With Quote
  #2  
Old 09-29-2011, 08:15 AM
AtmaWeapon's Avatar
AtmaWeaponWrite a text file in XP vs Win7 AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

My guess is the full file path is in the same directory hierarchy as the program. More specifically, it's somewhere in the Program Files/Program Files (x86) directory tree. Microsoft has been warning developers against this since Windows 2000 released, and Vista is where they finally swung the hammer (though limited users in XP can be affected.)

Before I write a ton about it, how about you confirm if I'm right or not? What's the path to one of the files that fails?
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 09-29-2011, 09:10 AM
easoftware easoftware is offline
Freshman
 
Join Date: Aug 2006
Posts: 48
Default

Program is installed in the default folder per OS "C:\Programs\Company"
Data is stored in user defined folder. My computer: "My Documents\AppName".
User's folder: "Z:\Projects\ProjectName\AppName\" where z is a mapped network drive

IO works on my computer (Win XP R3) no matter where I save the data. I know the user does not save the data in the same folder as the application.
Reply With Quote
  #4  
Old 09-29-2011, 02:57 PM
easoftware easoftware is offline
Freshman
 
Join Date: Aug 2006
Posts: 48
Default

Updated the code to test that all parts of the write function are executing. Now MessageBox pops up to show progress. Also try to write to two different files, one as a text file and one using BinWriter.

When the user saves(File > Save), all the messagebox popup show, but the data file is not saved and the two test files are not created.

When the user saves as (File > Save As) all the messagebox popup show, but the new data file is saved and the two test files are created.

Public Function WriteDataFile(ByVal FullFileName As String) As Integer

' writes a data file to disk
'
' Format is a text file, with [TAB] seperators and [CR][LF] as line terminators
' this format can also be read by Excel and Notepad
' 1st line, Header data, all text:
' ItemName[TAB]Id[TAB]Units[TAB]Value1[TAB]Value2[TAB]Code[CR][LF]
' 2nd through Last row:
' [ItemName][TAB][Id][TAB][Value1][TAB][Value2][TAB][Code][CR][LF]
'
' vars passed:
' FullFileName - full file name (full path and file name)
'
' returns
' 0 - no errors
' -1 - other error
' -2 - io error

Try
Dim DataText As String ' can hold aprox 2 billion chars

' first line of file
DataText = "ItemName" & vbTab & "Id" & vbTab & "Units" & vbTab & "Value1" & vbTab & "Value2" & vbTab & "Code" & vbCrLf

' data lines text
For i As Integer = 0 To DataTable.DefaultView.Count - 1 ' for each row of data
DataText &= GetDataTextToWrite(DataTable.DefaultView(i).Row) ' get data from row as formatted string
Next

MessageBox.Show("Ready to write to file """ & FullFileName & """ .", _
"Debugging", MessageBoxButtons.OK, MessageBoxIcon.Information)

My.Computer.FileSystem.WriteAllText(FullFileName, DataText, False) ' write the file to disk

Dim fInfo As IO.FileInfo = New IO.FileInfo(FullFileName)
Dim DebugFileName As String = fInfo.DirectoryName & "\" & fInfo.Name.Substring(0, fInfo.Name.IndexOf(".")) & "_T.txt"
MessageBox.Show("Ready to write to debugging text file """ & DebugFileName & """ .", _
"Debugging", MessageBoxButtons.OK, MessageBoxIcon.Information)
My.Computer.FileSystem.WriteAllText(DebugFileName, DataText, False) ' write the file to disk


DebugFileName = fInfo.DirectoryName & "\" & fInfo.Name.Substring(0, fInfo.Name.IndexOf(".")) & "_B.txt"
MessageBox.Show("Ready to write to debugging binary file """ & DebugFileName & """ .", _
"Debugging", MessageBoxButtons.OK, MessageBoxIcon.Information)
Using aFileStream As System.IO.FileStream = System.IO.File.OpenWrite(DebugFileName)
Using BinWriter As System.IO.BinaryWriter = New System.IO.BinaryWriter(aFileStream)
BinWriter.Write("Binary Test Text")
End Using
End Using

MessageBox.Show("Done writing " & DataText.Length.ToString & " chars to file """ & FullFileName & """ .", _
"Debugging", MessageBoxButtons.OK, MessageBoxIcon.Information)

Return 0 ' if got here, then no errors
Catch FileEx As System.IO.IOException
MessageBox.Show("IO error in WriteDataFile. " & FileEx.Message, _
"IO Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return -2
Catch ex As Exception
MessageBox.Show("Other error in WriteDataFile. " & ex.Message, _
"Other Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return -1
End Try
End Function
Reply With Quote
  #5  
Old 10-01-2011, 08:15 AM
AtmaWeapon's Avatar
AtmaWeaponWrite a text file in XP vs Win7 AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I do not know what could be causing the problem based on the information you have provided. You've left one of the original questions unanswered and led me to ask more. Here are some questions:
  • What is the path to a file that can be successfully saved?
  • Is there a difference between the Save and Save As code? You've only provided one block of unformatted code.
  • Has the user tried running it from a hard drive instead of a networked drive?
I ask these questions because, in order:
  • Vista/Win7 virtualize some file locations. When a program tries to save to the locations, it's actually saved somewhere else. For example, when Lotus Notes tries to save data to Program Files, it ends up in "C:\Program Data" instead and I have to remember this when it corrupts itself monthly. I need to know the value of FullFileName.
  • If the Save As code is different that difference is likely the answer.
  • In certain situations programs executed from removable/networked drives get lower permissions. I'd expect an exception, but it looks like you've got a debugging catch-all in there.
The code is simple enough; there's some improvements I'd make but let's focus on the "doesn't work" part. I need to know every detail that is different between "save" and "save as", because you stated one works and the other doesn't. The difference might be something innocent manipulated by something else, but it's a difference and identifying it will solve the mystery.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #6  
Old 10-01-2011, 05:50 PM
easoftware easoftware is offline
Freshman
 
Join Date: Aug 2006
Posts: 48
Default

I double checked the code. Both Save and SaveAs call WriteDataFile(). But Save used just the File Name (no path) , and SaveAs used the full path file name. I use just the file name (no path) to show the file name in the form's .text property, so use can see the file name in the form's title caption.

I will update my code and have the user run it. I will also have user check C:\ folder for any data files. Weird that it worked as desired in Win XP, but not in Win 7.

I up update with user results. Thanks again for all your help.

Last edited by easoftware; 10-01-2011 at 05:59 PM.
Reply With Quote
  #7  
Old 10-02-2011, 08:48 AM
AtmaWeapon's Avatar
AtmaWeaponWrite a text file in XP vs Win7 AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Quote:
But Save used just the File Name (no path) , and SaveAs used the full path file name.
So the difference is "Save" uses something like "file.txt" where "Save As" uses "C:\something\file.txt"?

When you don't specify a full path, the file is saved relative to the location of the application's working directory. By default, that's the application's folder. As I stated before, if that happens to be Program Files Vista and Win7 will generally reject attempts to write. It's possible your user is under further restrictions and not allowed to write to the remote drive; those settings can vary by machine but in general Windows considers running things from a network drive to be more risky than running them from your own hard drive. It's not weird that it worked in XP because XP did not have the additional security of Vista and Windows 7; that's one of the many reasons to upgrade. What's weird is the file is not written and you don't get any exceptions. I'm curious if something I can't see is interfering.

I can't say it for sure becuase I don't have a convenient way to reproduce; I don't have a network drive to work with. The easiest thing you could try is have the user run the program from their own drive if possible; that'd rule out the network drive.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #8  
Old 10-03-2011, 08:08 PM
easoftware easoftware is offline
Freshman
 
Join Date: Aug 2006
Posts: 48
Default

Winner! Winner! Chicken Dinner!

That was the problem. Now File Save and File SaveAs work the same!

Thanks again for all your help. User is checking the application folder for extra data files. Will post results.
Reply With Quote
  #9  
Old 10-03-2011, 11:15 PM
easoftware easoftware is offline
Freshman
 
Join Date: Aug 2006
Posts: 48
Default

No data files in user's application folder.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Write a text file in XP vs Win7
Write a text file in XP vs Win7
Write a text file in XP vs Win7 Write a text file in XP vs Win7
Write a text file in XP vs Win7
Write a text file in XP vs Win7
Write a text file in XP vs Win7 Write a text file in XP vs Win7 Write a text file in XP vs Win7 Write a text file in XP vs Win7 Write a text file in XP vs Win7 Write a text file in XP vs Win7 Write a text file in XP vs Win7
Write a text file in XP vs Win7
Write a text file in XP vs Win7
 
Write a text file in XP vs Win7
Write a text file in XP vs Win7
 
-->