Use StreamWriter and save the file to specific path.

Create folder and then save the file in it.

Page 1 of 1

10 Replies - 18937 Views - Last Post: 31 October 2009 - 05:57 AM Rate Topic: -----

#1 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Use StreamWriter and save the file to specific path.

Posted 30 October 2009 - 01:25 PM

So, im using streamwriter and i want to create a folder and then save the text file in that folder.
Here is the code im using right now.
Imports System
Imports System.IO

Public Class Form1
	Dim nr As Integer = 0
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Using sw As StreamWriter = New StreamWriter(DateAndTime.Now.ToShortDateString & ".txt")
			' Add some text to the file.
			Dim s As String
			For Each s In TextBox1.Lines
				sw.WriteLine(TimeOfDay & s)
			Next
			sw.WriteLine("-------------------")
			' Arbitrary objects can also be written to the file.
			sw.Write("The date is: ")
			sw.WriteLine(DateTime.Now)
			sw.Close()
			nr = nr + 1
		End Using
	End Sub
End Class


Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Use StreamWriter and save the file to specific path.

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Use StreamWriter and save the file to specific path.

Posted 30 October 2009 - 01:29 PM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

#3 PDUNZ  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 376
  • Joined: 17-July 09

Re: Use StreamWriter and save the file to specific path.

Posted 30 October 2009 - 02:53 PM

To create a directory - its simple as using the System.IO.Directory.CreateDirectory("Path") or My.Computer.FileSystem.CreateDirectory("path")

Creating a file and wirting is similar.

This post has been edited by PDUNZ: 30 October 2009 - 02:53 PM

Was This Post Helpful? 0
  • +
  • -

#4 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Use StreamWriter and save the file to specific path.

Posted 30 October 2009 - 03:01 PM

View PostPDUNZ, on 30 Oct, 2009 - 01:53 PM, said:

To create a directory - its simple as using the System.IO.Directory.CreateDirectory("Path") or My.Computer.FileSystem.CreateDirectory("path")

Creating a file and wirting is similar.

Thanks :)
but i dont know how to create the file in the folder i now have created =/
If you want more information check this topic
http://www.dreaminco...topic134770.htm
Was This Post Helpful? 0
  • +
  • -

#5 sharpy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 36
  • View blog
  • Posts: 394
  • Joined: 02-June 07

Re: Use StreamWriter and save the file to specific path.

Posted 30 October 2009 - 04:26 PM

Hi endlessmind

Here is a straight answer to a straight question.

 imports System.IO

Public Class Form1

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Try
			If Not Directory.Exists("C:\myfile") Then

				Directory.CreateDirectory("C:\myfile")

			End If

		Catch ex As System.Exception
			System.Windows.Forms.MessageBox.Show(ex.Message)
		End Try

		Dim outputstream As StreamWriter = File.CreateText("C:\myfile\test.txt")
	End Sub
End Class


Hope this helps :D

This post has been edited by sharpy: 30 October 2009 - 04:27 PM

Was This Post Helpful? 1
  • +
  • -

#6 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Use StreamWriter and save the file to specific path.

Posted 30 October 2009 - 04:49 PM

View Postsharpy, on 30 Oct, 2009 - 03:26 PM, said:

Hi endlessmind

Here is a straight answer to a straight question.

 imports System.IO

Public Class Form1

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Try
			If Not Directory.Exists("C:\myfile") Then

				Directory.CreateDirectory("C:\myfile")

			End If

		Catch ex As System.Exception
			System.Windows.Forms.MessageBox.Show(ex.Message)
		End Try

		Dim outputstream As StreamWriter = File.CreateText("C:\myfile\test.txt")
	End Sub
End Class


Hope this helps :D

Thanks :)
i solve it like this ( just a test project )
Imports System
Imports System.IO

Public Class Form1
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Try
			If Not Directory.Exists("C:\myfile") Then

				Directory.CreateDirectory("C:\myfile")

			End If

		Catch ex As System.Exception
			System.Windows.Forms.MessageBox.Show(ex.Message)
		End Try

		Using outputstream As StreamWriter = File.CreateText("C:\myfile\" & DateAndTime.Now.ToShortDateString & ".txt")
			' Add some text to the file.
			Dim s As String
			For Each s In TextBox1.Lines
				outputstream.WriteLine(TimeOfDay & " " & s)
			Next
			outputstream.WriteLine("-------------------")
			' Arbitrary objects can also be written to the file.
			outputstream.Write("The date is: ")
			outputstream.WriteLine(DateTime.Now)
			outputstream.Close()
		End Using
	End Sub
End Class



but now i want to save the path in a databas. so if i do lets say 3 text files one day then i have a listbox here there is 3 items when i click on that day on the calender, and thoes items the program has added from the databas. and when i click on one for the items the text file should open in a multi-line textbox.

1. How do i open a text file in to a textbox?
2. How do i save the path of more then one file in a databas?
3. How do i do so i can se the files name in a listbox?

Hope you know what i mean :)
Was This Post Helpful? 0
  • +
  • -

#7 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Use StreamWriter and save the file to specific path.

Posted 30 October 2009 - 05:09 PM

is there anyway to make a onw file-format? :)
like replacing ".txt" with ".abc"?
becuase even if the computer dont know how to open it, then i can still read it if i drag it in to a open notepad right? or is there an other way?
Was This Post Helpful? 0
  • +
  • -

#8 sharpy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 36
  • View blog
  • Posts: 394
  • Joined: 02-June 07

Re: Use StreamWriter and save the file to specific path.

Posted 31 October 2009 - 01:17 AM

Hi Endlessmind

Yes you can have any suitable extension like .doc this will open up in microsft word


Have a look at this tut for the database issues
http://www.dreaminco...wtopic74114.htm

This code is what I use to stream read a txt doc in then split it into smaller txt files. However at the same time I write the data being read to a listview. Its only an example which may give you ideas to solve your issues

Imports System.Text
Imports System.IO
Public Class splitcode

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim fsIN As IO.FileStream
		Dim fsOut As IO.FileStream
		Dim sr As IO.StreamReader
		Dim sw As IO.StreamWriter
		Dim OutCount As Integer
		Dim title As String
		Dim titlechar As String = starttext.Text
		Dim Endchar As String = endtext.Text
		Dim path As String = sourcefile.Text
		Dim destination As String = sourcedestination.Text

		fsIN = New IO.FileStream(Path, IO.FileMode.Open, IO.FileAccess.Read)


		sr = New IO.StreamReader(fsIN, System.Text.Encoding.Default)

		Do
			Dim Line As String
		   
			Line = sr.ReadLine()

			If Line Is Nothing Then Exit Do
			If Line.Contains(titlechar) Then
				title = Line
			End If

			If fsOut Is Nothing Then
				OutCount += 1

				fsOut = New IO.FileStream(destination & "\" & title & ".txt", _
			  IO.FileMode.CreateNew, IO.FileAccess.Write)


				sw = New IO.StreamWriter(fsOut, System.Text.Encoding.Default)

			End If

			sw.WriteLine(Line)

			ListBox1.Items.Add(Line)
			If Line.Contains(Endchar) Then
				title = Line
				ListBox1.Items.Add(" ")
				ListBox1.Items.Add(" ")
				ListBox1.Items.Add("CODE SPLIT TO FILE")
				ListBox1.Items.Add(" ")
				ListBox1.Items.Add(" ")

			End If

			If Line = Endchar Then
				sw.Close()
				fsOut = Nothing
			End If

		Loop

		If fsOut IsNot Nothing Then
			sw.Close()
		End If

		fsIN.Close()

		MessageBox.Show("The file has been Split Successfully")

	End Sub



Hope this helps. Just take it one step at a time
Was This Post Helpful? 0
  • +
  • -

#9 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Use StreamWriter and save the file to specific path.

Posted 31 October 2009 - 02:06 AM

View Postsharpy, on 31 Oct, 2009 - 12:17 AM, said:

Hi Endlessmind

Yes you can have any suitable extension like .doc this will open up in microsft word


Have a look at this tut for the database issues
http://www.dreaminco...wtopic74114.htm

This code is what I use to stream read a txt doc in then split it into smaller txt files. However at the same time I write the data being read to a listview. Its only an example which may give you ideas to solve your issues

Imports System.Text
Imports System.IO
Public Class splitcode

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim fsIN As IO.FileStream
		Dim fsOut As IO.FileStream
		Dim sr As IO.StreamReader
		Dim sw As IO.StreamWriter
		Dim OutCount As Integer
		Dim title As String
		Dim titlechar As String = starttext.Text
		Dim Endchar As String = endtext.Text
		Dim path As String = sourcefile.Text
		Dim destination As String = sourcedestination.Text

		fsIN = New IO.FileStream(Path, IO.FileMode.Open, IO.FileAccess.Read)


		sr = New IO.StreamReader(fsIN, System.Text.Encoding.Default)

		Do
			Dim Line As String
		   
			Line = sr.ReadLine()

			If Line Is Nothing Then Exit Do
			If Line.Contains(titlechar) Then
				title = Line
			End If

			If fsOut Is Nothing Then
				OutCount += 1

				fsOut = New IO.FileStream(destination & "\" & title & ".txt", _
			  IO.FileMode.CreateNew, IO.FileAccess.Write)


				sw = New IO.StreamWriter(fsOut, System.Text.Encoding.Default)

			End If

			sw.WriteLine(Line)

			ListBox1.Items.Add(Line)
			If Line.Contains(Endchar) Then
				title = Line
				ListBox1.Items.Add(" ")
				ListBox1.Items.Add(" ")
				ListBox1.Items.Add("CODE SPLIT TO FILE")
				ListBox1.Items.Add(" ")
				ListBox1.Items.Add(" ")

			End If

			If Line = Endchar Then
				sw.Close()
				fsOut = Nothing
			End If

		Loop

		If fsOut IsNot Nothing Then
			sw.Close()
		End If

		fsIN.Close()

		MessageBox.Show("The file has been Split Successfully")

	End Sub



Hope this helps. Just take it one step at a time

Hm. Well, then the next step would be understandning the code right? so if you or someone could explain it to me, not details just rough.
:)
Was This Post Helpful? 0
  • +
  • -

#10 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Use StreamWriter and save the file to specific path.

Posted 31 October 2009 - 02:27 AM

But. couldnt i do like this?
		Dim s As String
		For Each s In Directory.GetFiles("C:\" & MonthCalendar1.Selectionstart.Date & "\")
			ListBox1.Items.Add(s)
		Next


but there is just this problem when the folder do exist. i dont want to create a new folder i just want to read from it. and how do i do so when i click on a item it opens that textfile in the same textbox that i wrote it in?

This post has been edited by EndLessMind: 31 October 2009 - 02:34 AM

Was This Post Helpful? 0
  • +
  • -

#11 EndLessMind  Icon User is offline

  • These are the droids you're looking for
  • member icon

Reputation: 194
  • View blog
  • Posts: 1,099
  • Joined: 13-March 09

Re: Use StreamWriter and save the file to specific path.

Posted 31 October 2009 - 05:57 AM

i solve that one. But now i want to change the text in a file, then the program creates a new file and the one i wanted to change the text in is still there. Is there anyway to change the text without creating a new file? like streamreader.save
sure i know that streamreader.save done exist but someting that would work like that.
Here is the code so far
Imports System
Imports System.IO
Public Class Form1
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Try
			If Not Directory.Exists("C:\" & MonthCalendar1.Selectionstart.Date & "\") Then
				Directory.CreateDirectory("C:\" & MonthCalendar1.Selectionstart.Date & "\")
			End If
		Catch ex As System.Exception
			System.Windows.Forms.MessageBox.Show(ex.Message)
		End Try
		Using outputstream As StreamWriter = File.CreateText("C:\" & MonthCalendar1.Selectionstart.Date & "\" & Date.Now.Hour & "_" & Date.Now.Minute & ".hej")
			Dim s As String
			For Each s In TextBox1.Lines
				outputstream.WriteLine(s)
			Next
			outputstream.Close()
		End Using
	End Sub

	Private Sub MonthCalendar1_DateChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar1.DateChanged
		Try
			If Not Directory.Exists("C:\" & MonthCalendar1.Selectionstart.Date & "\") Then
				Directory.CreateDirectory("C:\" & MonthCalendar1.Selectionstart.Date & "\")
			End If
		Catch ex As System.Exception
			System.Windows.Forms.MessageBox.Show(ex.Message)
		End Try
		Button2.PerformClick()
	End Sub

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		ListBox1.Items.Clear()
		Dim s As String
		For Each s In Directory.GetFiles("C:\" & MonthCalendar1.Selectionstart.Date & "\")
			ListBox1.Items.Add(s)
		Next
	End Sub

	Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
		Dim FILE_NAME As String = ListBox1.Text
		Dim objReader As New System.IO.StreamReader(FILE_NAME)
		TextBox1.Text = objReader.ReadToEnd
		objReader.Close()
	End Sub

	Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
		File.Delete(ListBox1.Text)
		Button2.PerformClick()
	End Sub

	Private Sub RedigeraToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RedigeraToolStripMenuItem.Click
		Form2.Show()
	End Sub
End Class




im tinking for changing the text in e new form. so here it the code for form2 so far
Public Class Form2

	Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Dim FILE_NAME As String = Form1.ListBox1.Text
		Dim objReader As New System.IO.StreamReader(FILE_NAME)
		TextBox1.Text = objReader.ReadToEnd
	End Sub
End Class

This post has been edited by EndLessMind: 31 October 2009 - 06:02 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1