Hi, i am writing a mobile application to save some task. In this application i want save the data of task to the text file(*.txt). however, i faced some of the issues that i unable to save data to txt file due to the runtime error on file not exist but i have checked it and the txt file is existed. Hope anybody can help. Here my code as below

Imports System.IO

Public Class Form1

Public showdisplay As New ArrayList
Public Task As New task

Dim showdetails As New showdetails

Private Sub mnuadd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuadd.Click


Task.Write_File(a:=txtTitle.Text, b:=txtDescription.Text, c:=dtpDate.Value)

If txtTitle.Text = " " And txtDescription.Text = " " Then

MessageBox.Show("Please Input Task Title and Task Description")

Else

showdisplay.Add(Task)


ListBox1.Items.Add(Task)

MessageBox.Show("Task Save", "Task Save", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)

End If

txtTitle.Text = " "
txtDescription.Text = " "
dtpDate.Value = Date.Today


End Sub


The function for Write_file as below

Imports System.IO
Public Class task

Public Title As String
Public Description As String
Public Dates As Date

Public Sub Write_File(ByVal a As String, ByVal b As String, ByVal c As Date)

Title = a
Description = b
Dates = c

Dim writer As IO.StreamWriter = New IO.StreamWriter("MyData.txt\", False)

Dim strwritedata As String

strwritedata = a & "," & b & "," & c

writer.WriteLine(strwritedata)


writer.Close()
End Sub

Try to write a full path to the file:

Dim writer As New System.IO.StreamWriter("C:\MyFolder\myFile.txt", False)

Hi

I have try implemet the code you suggested but i throw me run time error ><

Attachments Untitled.png 201.77 KB

Then there is no such file. Why you dont create it and then insert into it at the same time? This way you for sure will not get this error:

Dim path As String = "C:\MyFolder\myFiletxt"
'write your own file path - full one (no need if file exists, just make sure Directories exist!!!
Using fs As New FileStream(path, FileMode.OpenOrCreate)
	Using tw As TextWriter = New StreamWriter(fs)
			'
		tw.WriteLine("some text in here") 'use your own text
	End Using
End Using

Hi!

In response of your post that have an image attached:

The path you have entered is not exists it is missing a part of path.

If your file is in your project "Bin/Debug" folder, you should try like this:

Dim writer As New System.IO.StreamWriter(AppDomain.CurrentDomain.BaseDirectory & "myFile.txt", False)

Is this helpful ??

after i implement your code it give me this show at below picture

Attachments Untitled.png 33.39 KB

Okay

In your very first post you have this line:

The function for Write_file as below

below that you just have this:

Imports System.IO

please, Add this as well:

Imports System

Ya Have imports system.IO

Imports System.IO
Public Class task

    Public Title As String
    Public Description As String
    Public Dates As Date
    Public Times As String

    Public Overrides Function ToString() As String
        Return Title & "  " & "(" & Description & ")" & "  " & Dates & " " & Times
    End Function


    Public Sub Write_File(ByVal a As String, ByVal b As String, ByVal c As Date, ByVal d As String)

        Title = a
        Description = b
        Dates = c
        Times = d

      

        Dim writer As New System.IO.StreamWriter("MyData.txt", False)
        Dim strwritedata As String

        strwritedata = a & "," & b & "," & c & "," & d

        writer.WriteLine(strwritedata)


        writer.Close()

    End Sub

From your attached, it shows that the application cannot find the specified file. Make sure you point to the exact directory of the text file

Hi

I have try implemet the code you suggested but i throw me run time error ><


[ATTACH]21707[/ATTACH]

Is that a space after the dot and before the txt ?
Are you sure that this is the way the file is named? ("MyData. txt")

Edited 5 Years Ago by adam_k: n/a

This article has been dead for over six months. Start a new discussion instead.