Solved

copy file from \\network share to c:\... VB.NET 2003\2010 Express

Posted on 2011-03-25
8
1,088 Views
Last Modified: 2012-06-21
Hi Experts,

I want to build a winapp which will list files in a listbox… if the listbox-item is selected and button1 clicked, it will copy/transfer the document from the network repository/folder to local machine (maybe a progress bar to show progress??????? optional).

1.      User opens App
2.      Listbox1 displays e.g. 9 (RTF) files.
3.      user selects Listbox1 item e.g. Outlook
4.      clicks Button1
5.      Outlook - something.rtf copies from network share to c:\dir

The reason I’ve chosen this is because stream-reading RTF docs from a network location can take time to view.

I have code to streamread into Listbox from DIR (which i will add to call, once I get chance).
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim path As String
        Dim di As System.IO.DirectoryInfo

        path = "c:\temp\"
        di = New System.IO.DirectoryInfo(path)

        If (Not di.Exists) Then
            di.Create()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Try
            FileCopy("C:\Documents and Settings\Bob\My Documents\test.txt", "C:\temp")

        Catch ex As Exception
            MsgBox("ok")
        End Try
       
    End Sub

Open in new window

0
Comment
Question by:RobertoFreemano
  • 5
  • 3
8 Comments
 

Author Comment

by:RobertoFreemano
ID: 35221465
If I don't add the TRY code, then I get the following error:

---error--------------------------------------------------------------------------------------------
An unhandled exception of type 'System.IO.IOException' occurred in microsoft.visualbasic.dll

Additional information: The target directory already exists.
------------------------------------------------------------------------------------------------------
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 35222058
Are you asking for something more than My.Computer.FileSystem.CopyFile, which has an option for a progress bar, ala Windows Explorer?

File Copy Progress, Custom Thread Pools
http://msdn.microsoft.com/en-us/magazine/cc163851.aspx
0
 

Author Comment

by:RobertoFreemano
ID: 35235154
Sorry TheLearnedOne,

I fond a solution which works but I was wondering if it could incorporate a progress bar?

Thanks,
Roberto
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim path As String
        Dim di As System.IO.DirectoryInfo

        path = "c:\temp\"
        di = New System.IO.DirectoryInfo(path)

        If (Not di.Exists) Then
            di.Create()
        End If

        Dim di2 As System.IO.DirectoryInfo

        path = "c:\temp\temp2"
        di2 = New System.IO.DirectoryInfo(path)

        If (Not di2.Exists) Then
            di2.Create()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim inputStream, outputStream As FileStream
        Dim fileSize As Long
        Dim ipFilebyte As Byte
        inputStream = File.OpenRead("c:\temp\test.txt") 'server path
        fileSize = inputStream.Length()

        outputStream = File.OpenWrite("c:\temp\temp2\test.txt") 'local path

        While fileSize
            ipFilebyte = inputStream.ReadByte()
            outputStream.WriteByte(ipFilebyte)
            fileSize -= 1
        End While

        outputStream.Close()
        inputStream.Close()

    End Sub

Open in new window

0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 35235271
1) You don't need to use a FileStream to get a progress bar, you can use the FileSystem.CopyFile to copy with a progress bar in 1 line.

2) If you insist on the FileStream approach, then I would suggest using a BackgroundWorker, with progress enabled, and the ReportProgress.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:RobertoFreemano
ID: 35235308
Thanks TheLearnedOne,

I'm clueless when it comes to practical coding :(

I wonder if you could assist with "FileSystem.CopyFile "
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 35237580
That would be something like this:

      My.Computer.FileSystem.CopyFile(sourceFileName, destinationFileName, FileIO.UIOption.AllDialogs)
0
 

Author Comment

by:RobertoFreemano
ID: 35314307
Hi TLO,

I'll test this now ;)

Sorry about the delay :(
0
 

Author Closing Comment

by:RobertoFreemano
ID: 35386623
Thanks ;)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

937 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now