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

Recommended Answers

All 10 Replies

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 ><

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

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

Add Both:

Imports System.IO
Imports System

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")

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.