how to delete a file after read it VB.NET?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: how to delete a file after read it VB.NET?

  1. #1
    Join Date
    Aug 2005
    Posts
    5

    how to delete a file after read it VB.NET?

    Hello,
    sure it is strange question but i spend today to figure out how to delete the file i just read in vb.net.
    this is the code I have:
    Imports System.Data.SqlClient
    Imports System.IO.File
    Imports System.IO
    Module Module1
    Sub ListDirectory(ByVal dir As DirectoryInfo)
    Console.WriteLine("C:\Documents and Settings\Desktop\New Folder\ ")
    Dim f As FileInfo
    For Each f In dir.GetFiles()
    Console.WriteLine(f.FullName)
    IO.File.Delete(f.FullName)
    Next
    IO.File.Delete("f.FullName")
    ' list directories and files together from FileSystemInfos
    Console.WriteLine("*** Files and directories ***")
    Dim fi As FileSystemInfo
    End Sub


    Sub Main()

    Try

    Dim dir As New DirectoryInfo("C:\Documents and Settings\pnoorass\Desktop\read_test\New Folder\")
    fileList = dir.GetFiles("test*.txt")
    For index As Integer = 0 To fileList.Length - 1
    Limit = fileList.Length

    ....some parseing
    .... transfer to sql server

    Next
    ListDirectory(dir)

    Catch ex As Exception
    Throw ex
    Finally
    If Not readStream Is Nothing Then
    readStream.Close()
    End If
    If Not writeStream Is Nothing Then
    readStream.Close()
    End If

    End Try
    End Sub
    End Module


    'everytime i run this program instead of deleteing the file it gives me message that The process cannot access the file "C:\Documents and Settings\Desktop\New Folder\test1.txt" because it is being used by another process.
    Although it dose the parse perfectly just not deleting any file!


    Will appreciate your help

  2. #2
    Join Date
    Dec 2004
    Posts
    717
    You should read the file as ReadOnly...
    like..
    Dim fs As System.IO.FileStream = New System.IO.FileStream(strFilePath, IO.FileMode.Open, IO.FileAccess.Read)
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

  3. #3
    Join Date
    Aug 2005
    Posts
    5
    could you please give me more specific hint. i want to open directory and read all files in directory which i know are .txt files and then delete all of the files. the reading part works by using:
    Dim dir As New DirectoryInfo("C:\Documents and Settings\Desktop\R\ Folder\", )
    fileList = dir.GetFiles("test*.txt")
    and then i read all files.
    then i want to use:
    IO.File.Delete(f.FullName)
    and it is not working.


    Thank you,
    Sahar

  4. #4
    Join Date
    Dec 2004
    Posts
    717
    I think the code that you show me is working fine.
    I've tested with new project...

    Imports System.Data.SqlClient
    Imports System.IO.File
    Imports System.IO
    Module Module1
    Sub ListDirectory(ByVal dir As DirectoryInfo)
    Console.WriteLine("C:\SS\ ")
    Dim f As FileInfo
    For Each f In dir.GetFiles()
    Console.WriteLine(f.FullName)
    IO.File.Delete(f.FullName)
    Next
    IO.File.Delete("f.FullName")
    ' list directories and files together from FileSystemInfos
    Console.WriteLine("*** Files and directories ***")
    Dim fi As FileSystemInfo
    End Sub

    Sub Main()
    Try
    Dim dir As New DirectoryInfo("C:\SS\")
    Dim fileList() As FileInfo
    Dim Limit As Integer
    fileList = dir.GetFiles("test*.txt")
    For index As Integer = 0 To fileList.Length - 1
    Limit = fileList.Length
    '''....some parseing
    '''.... transfer to sql server
    Next
    ListDirectory(dir)
    Catch ex As Exception
    Throw ex
    Finally
    End Try
    End Sub
    End Module
    If you don't believe that, get new project.. Then, lets start test ur code. )

    I'd say you should read the files as ReadOnly...
    You should also dispose objects after finish...

    If you're still facing these problem, you should show us the whole coding or processes that u have done.....
    Best Regards,
    Michael Sync
    http://michaelsync.net

    The more you share,The more you get

Similar Threads

  1. Replies: 1
    Last Post: 03-23-2005, 12:18 AM
  2. Read a file on the client
    By Andrea in forum ASP.NET
    Replies: 1
    Last Post: 09-28-2001, 06:25 PM
  3. NullPointerException when reading text file
    By Andrew McLellan in forum Java
    Replies: 3
    Last Post: 05-09-2001, 06:34 PM
  4. Replies: 0
    Last Post: 08-22-2000, 11:54 PM
  5. Replies: 0
    Last Post: 05-03-2000, 03:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center