0

Hello, i wanted to know how to write a file path for every computer
For example:

objwrite = New System.IO.StreamWriter("C:\Users\EVGA\Desktop\VB.Net Example\myfile.txt", False)

this code work only on my pc cause of its path
so i wanted to know how to write it so i can use it in another pc
please help
thanks

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by sknake
0

Use Environment.GetFolderPath() :

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim sDir As String = GetMyCustomDesktopFolderPath()
    Dim fName As String = "myfile.txt"
    Dim sFull = System.IO.Path.Combine(sDir, fName)
    System.IO.File.WriteAllText(sFull, "abc123")
  End Sub

  Private Shared Function GetMyCustomDesktopFolderPath() As String
    Const exampleDirName As String = "VB.Net Example"
    Dim sFullPath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), exampleDirName)
    If Not System.IO.Directory.Exists(sFullPath) Then
      System.IO.Directory.CreateDirectory(sFullPath)
    End If
    Return sFullPath
  End Function
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.