Copy Files and Folders from CURRENT directory to User Home Folder VB.Net

~OSD~ 2,176 Reputation points
2020-12-04T09:00:50.813+00:00

Hi,

Is it possible to use "Current Directory" as source (instead of full /absolute path) to copy file /folder to current user profile?

Source: C:\myData\Backups\Latest**UserBackup**\SavedData\StdDoc.txt
<Instead of using the whole path, can we limit to the current directory, In my case it's UserBackup where the VB app should run>
Possibility to define source as: CurrentDir\SavedData\StdDoc.txt ?
Destination: %UserName%\Documents\StdDoc.txt

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,726 questions
0 comments No comments
{count} votes

7 answers

Sort by: Most helpful
  1. Cory Smith 11 Reputation points
    2020-12-04T19:55:39.507+00:00

    I think before anyone can answer this question, need to clarify when you state "current directory", do you mean...

    • running application directory...
    • running application start folder (this can be changed from within the icon that launches the application or set in debug)...
    • current file system directory (assuming it was changed or you launched the application from the command line)...
    • or current users "home" folder?

    Thanks.

    2 people found this answer helpful.

  2. Castorix31 85,131 Reputation points
    2020-12-04T09:25:41.923+00:00

    One of the ways :

     Dim sCurrentDirectory As String = AppDomain.CurrentDomain.BaseDirectory
    
    0 comments No comments

  3. ~OSD~ 2,176 Reputation points
    2020-12-04T10:15:56.203+00:00

    Tried following but seems to be not working:

     Private Sub Button18_Click(sender As Object, e As EventArgs) Handles Button18.Click
            ' Define Current Directory
            ' Absolute Path >> C:\Users\Public\Desktop\myData\UserA\Documents\ReadMe-UserA.txt
            ' How to define Current Directory >> ?? so regardless of UserA or UserB, VB app should check the current dir if it's inside UserA or UserB etc.
            Dim currDir() As String = IO.Directory.GetFiles(IO.Path.Combine("C:\Users", "Public", "Desktop", "myData"), "*.*", IO.SearchOption.AllDirectories)
    
    
            'Define Target Directory
            Dim tarDir() As String = IO.Directory.GetFiles(IO.Path.Combine("C:\Users", Environment.UserName, "Documents"), "*.*")
    
            'Copy required file from current location to user's documents
            System.IO.File.Copy(currDir(0), tarDir(0), overwrite:=True)
        End Sub
    

  4. ~OSD~ 2,176 Reputation points
    2020-12-07T09:07:41.373+00:00

    Hi,

    Current Directory >> running application directory.

    0 comments No comments

  5. Cory Smith 11 Reputation points
    2020-12-07T16:41:07.86+00:00

    If a WinForms application, you can try Application.StartupPath or IO.Path.GetDirectoryName(Application.ExecutablePath).

    Things are a little more complicated if you are doing a different kind of project (Console, ASP.NET, etc.). Things get even more complicated once you start talking about .NET Core / .NET 5. And even more so once you start talking about types of deployments (single-file).

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.