Results 1 to 2 of 2

Thread: process call error

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2004
    Location
    Kolkata, India
    Posts
    290

    process call error

    Hi

    I am running the below mention code in vb.net but problem is
    when dir.txt already in c: drive it will read the text file
    but if i delete the dir.txt file and create new one and in next line
    try to read the same dir.txt file it will flash error "Could not find file c:\dir.txt"

    Pls guide me .. why this type of problem accured..


    thanks


    asm


    VB Code:
    1. Imports System
    2. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3.         Dim SFile As System.IO.File
    4.         Dim VolRead As System.IO.StreamReader
    5.         Dim ReadTxt As String
    6.         Try
    7.             If SFile.Exists("C:\DIR.TXT") Then
    8.                 System.Diagnostics.Process.Start("C:\Windows\System32\CMD.exe", " /C Del C:\DIR.TXT")
    9.             End If
    10.             System.Diagnostics.Process.Start("C:\Windows\System32\CMD.exe", " /C Dir C:  > C:\DIR.TXT")
    11.             VolRead = New System.IO.StreamReader("C:\DIR.TXT")
    12.             ReadTxt = VolRead.ReadToEnd()
    13.             Label1.Text = "READING TEXT FILE OK.."
    14.             VolRead.Close()
    15.             If SFile.Exists("C:\Dir.txt") Then
    16.                 System.Diagnostics.Process.Start("C:\Windows\System32\CMD.exe", " /C Del C:\DIR.TXT")
    17.             End If
    18.         Catch ex As Exception
    19.             Label1.Text = ex.Message
    20.         End Try
    21.     End Sub

  2. #2
    PowerPoster
    Join Date
    Aug 2005
    Location
    College Station, TX
    Posts
    4,521

    Re: process call error

    Probably because it is running that line of code before the process finishes. You can either sleep the thread for a few milliseconds (System.Threading.Thread.Sleep), or even better create a new process object, start it, and use the .WaitForExit method of the process object to halt code execution until the process finishes.

    However, you can just re-write the code to do what you want without using cmd.exe... there are classes to handle file and folder information on the system...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width