Problem With OutPut to Txt File

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 775 Views - Last Post: 11 September 2011 - 06:51 AM Rate Topic: -----

#1 PNJLj  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 18-May 09

Problem With OutPut to Txt File

Posted 05 September 2011 - 05:19 AM

So, i've encountered more problems, Majority of this code is from VB but i am trying to implement it to VB.NET.
So my problem begins at Line 20 when I am trying to Open the file For Output. It just does not seem to be working, Please Help.



Private Sub btnSaveYL_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveYL.Click
        Dim Location As String
        Dim NextItem As Long
        Dim Answer As MsgBoxResult

        If lstYourList.Items.Count <= 0 Then
            MsgBox("Sorry, not enough entries. You need at least 1 item in 'Your List' to be able to save.", vbOKOnly & vbCritical, "Nothing In List!")
        Else
            Answer = MsgBox("Are you sure you want to Save 'Your List' to a location that you specify?" & vbCrLf & "You will need to Attach this in an E-Mail and send to me." & vbCrLf & "Are You Sure?", vbYesNo + vbQuestion, "Save?")
            If Answer = vbYes Then
                With saveYourList
                    .InitialDirectory = "C:\"
                    '.Filter = "Text Files (*.txt)|*.txt"
                    .ShowDialog()
                End With
                If Len(saveYourList.FileName) = 0 Then
                    MsgBox("You pressed the Cancel button.", vbOKOnly + vbExclamation, "Canceled!")
                Else
                    Location = saveYourList.FileName
                    FileOpen Output As #1
                    For NextItem = 0 To lstYourList.Items.Count - 1
                        Print #1, lstYourList.Items(NextItem)
                    Next
                        Close #1
                End If
            ElseIf Answer = vbNo Then
                MsgBox("You decided not to save your list.", vbOKOnly, "No Save.")
            End If
        End If
    End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: Problem With OutPut to Txt File

#2 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Problem With OutPut to Txt File

Posted 05 September 2011 - 06:11 AM

What is #1 here
FileOpen Output As #1
? is it a data type?
Was This Post Helpful? 0
  • +
  • -

#3 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,053
  • Joined: 17-July 10

Re: Problem With OutPut to Txt File

Posted 05 September 2011 - 06:58 AM

Quote

What is #1 here

In VB6 this is an alias for the opened file or a temporary variable. The definition is approximate.

PNJLj forget about that old method. Now you have .NET and
System.IO namespace. In this namespace, you can find StreamWriter class that is perfect for what you need. In the link I posted you will find an example to make your life easier.

This post has been edited by Ionut: 05 September 2011 - 06:59 AM

Was This Post Helpful? 3
  • +
  • -

#4 PNJLj  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 18-May 09

Re: Problem With OutPut to Txt File

Posted 05 September 2011 - 06:06 PM

cheers, i will give it a fiddle and see what i can come up with.
Was This Post Helpful? 0
  • +
  • -

#5 PNJLj  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 18-May 09

Re: Problem With OutPut to Txt File

Posted 07 September 2011 - 05:03 PM

Dim myDocPath As String = "D:\List.txt" 
        Dim SB As New StringBuilder()
        Dim SW As StreamWriter

        
        If File.Exists(myDocPath) = False Then
           Dim AddList As String = lstYourList.Text
           File.WriteAllLines(myDocPath, AddList, Encoding.UTF8)
        End If
        Dim apndList As String = "This is extra text" + Environment.NewLine
        File.AppendAllText(myDocPath, apndList, Encoding.UTF8)


having problems with the line 8 at the AddList part.
'String' to 'System.Collections.Generic.IEnumerable(Of String)'

Any Ideas?
Was This Post Helpful? 0
  • +
  • -

#6 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,053
  • Joined: 17-July 10

Re: Problem With OutPut to Txt File

Posted 07 September 2011 - 11:58 PM

WriteAllLines method expects as second parameter an IEnumerable(of String) which means you have to put there a List(Of String) or any other type o collection that inherits from that interface. And you pass a string value(AddList). I don't know what lstYourList is, but sounds that might work using it instead of AddList(taking that lst preffix into consideration, I suppose it is a list)
Was This Post Helpful? 0
  • +
  • -

#7 PNJLj  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 18-May 09

Re: Problem With OutPut to Txt File

Posted 08 September 2011 - 02:24 AM

eg: from form1 you have a listbox and when you like something you send it to form2 with listbox there also. ie, lstTitle(on Form1) and lstYourList(Form2). So everything in the lstYourList on Form2 i want to save to a txt file when you click the save button. Hope this explains it a little better. I fit's any conselation everything in lstTitle on Form1 is put there from a database created with Access07.
Anymore questons?

Just re-read over what you wrote again and you are saying, from what i gather, that you need to pass a string to the WriteAllLines function, which in effect i am doing by Dim.ing the AddList. AddList = lstYourList.Text. Make sense?

This post has been edited by PNJLj: 08 September 2011 - 02:26 AM

Was This Post Helpful? 0
  • +
  • -

#8 PNJLj  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 18-May 09

Re: Problem With OutPut to Txt File

Posted 08 September 2011 - 03:01 AM

okay then, so after a bit of research i found this, i used it and it works but is it the correct method?

Dim sfd As New SaveFileDialog
        sfd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        sfd.Filter = "Text files (*.txt)|*.txt"

        Dim result As DialogResult = sfd.ShowDialog
        If result = Windows.Forms.DialogResult.OK AndAlso sfd.FileName <> String.Empty Then

            If lstYourList.Items.Count > 0 Then
                Using sw As New System.IO.StreamWriter(sfd.FileName)
                    For index As Integer = 0 To lstYourList.Items.Count - 1
                        sw.WriteLine(lstYourList.Items(index).ToString)
                    Next
                    sw.Close()
                End Using
            End If
        End If

Was This Post Helpful? 0
  • +
  • -

#9 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 209
  • View blog
  • Posts: 1,365
  • Joined: 16-March 11

Re: Problem With OutPut to Txt File

Posted 08 September 2011 - 04:26 AM

Correct method? Who knows. It comes down to how well it helped you solve your problem. Granted there are probably much more efficient ways, but there's always some other way you didn't know. ;p Just the nature of this beast.

I would just use a for each loop on the items in your form2 listbox, and then use streamwriter as suggested before.

        Dim StreamWriter As System.IO.StreamWriter = New System.IO.StreamWriter(*example*"C:\Users\Trevor\Desktop\VB.NET Programs\Test1.txt")
        For Each string1 As String In ListBox1.Items
            StreamWriter.WriteLine(string1)
        Next

        StreamWriter.Close()
        StreamWriter.Dispose()
    End Sub



Doesn't have to be complex at all. Not to make you change your mind on how you'd like it done, just showing you. :)
Was This Post Helpful? 1
  • +
  • -

#10 PNJLj  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 18-May 09

Re: Problem With OutPut to Txt File

Posted 08 September 2011 - 06:31 PM

okay, so i am using yours as it is alot more basic than my way, so now i am trying to figureout how to append to a file if that file already exists and also lst the user put in their own file name. Will let you know how i go.
Was This Post Helpful? 0
  • +
  • -

#11 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 209
  • View blog
  • Posts: 1,365
  • Joined: 16-March 11

Re: Problem With OutPut to Txt File

Posted 08 September 2011 - 07:16 PM

View PostPNJLj, on 08 September 2011 - 06:31 PM, said:

okay, so i am using yours as it is alot more basic than my way, so now i am trying to figureout how to append to a file if that file already exists and also lst the user put in their own file name. Will let you know how i go.


If I'm not mistaken after your path for the stream writer there is a parameter which you can pass say after a if file.exists(path) = true statement, which will allow the appending of text to the file. Although I usually just streamwrite that exact way and it automatically appends the text to the file if it exists. :) not sure at this moment I'll get back to you, but just tinker around with it. :)
Was This Post Helpful? 0
  • +
  • -

#12 Aez  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 2
  • Joined: 05-September 11

Re: Problem With OutPut to Txt File

Posted 08 September 2011 - 10:11 PM

View PostPNJLj, on 08 September 2011 - 06:31 PM, said:

okay, so i am using yours as it is alot more basic than my way, so now i am trying to figureout how to append to a file if that file already exists and also lst the user put in their own file name. Will let you know how i go.


Dim StreamWriter As System.IO.StreamWriter = New System.IO.StreamWriter(*example*"C:\Users\Trevor\Desktop\VB.NET Programs\Test1.txt", true)  

Was This Post Helpful? -1
  • +
  • -

#13 PNJLj  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 18-May 09

Re: Problem With OutPut to Txt File

Posted 11 September 2011 - 01:25 AM

So I have decided to let the user put in thier own filename by using an inputbox and then placing the string into the filename area of line 3. However, when the program tries to access the MyDocuments folder it is unauthorized?? how do i allow the program to authorize the mydocuments folder?


Dim YourName As String
        YourName = InputBox("Enter the name of the file you want to save." & vbCrLf & "EG: 'YourList", "SaveFile Name", My.Computer.Name)

        Dim StreamWriter As System.IO.StreamWriter = New System.IO.StreamWriter(My.Computer.FileSystem.SpecialDirectories.MyDocuments(YourName) & ".txt")
        For Each ListItem As String In lstYourList.Items
            StreamWriter.WriteLine(ListItem)
        Next
        StreamWriter.Flush()
        StreamWriter.Close()
        StreamWriter.Dispose()

Was This Post Helpful? 0
  • +
  • -

#14 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1746
  • View blog
  • Posts: 4,409
  • Joined: 14-March 10

Re: Problem With OutPut to Txt File

Posted 11 September 2011 - 02:21 AM

Accessing some directories is impossible using that user level, that is why it is not advised to use those directories unless if you are sure that all users of your program will have Administrative privilege, which is pain!

This is because your program should run in Administrative mode to get access of those directories, and to make that possible(which means your program cant be run by users with no admins privilege) go to my project at solution explorer, then click on View windows Settings and change this line
<requestedExecutionLevel level="asInvoker" uiAccess="false" />

to
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Then you will be asked to restart your Visual Studio to run as admin and you are done
Was This Post Helpful? 0
  • +
  • -

#15 PNJLj  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 174
  • Joined: 18-May 09

Re: Problem With OutPut to Txt File

Posted 11 September 2011 - 04:10 AM

so would you suggest then, instead of using that directory, i should create new directory under c: drive? EG: C:\List\YourFile.txt? or is there another static address like shared desktop that i could use?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2