In my code I want to upload a file to my project(named Musiker), under the folder music. Here is what I did so far, it's not working.

'to get the path where to upload it, I did the following

Dim strpath As String = Application.StartupPath
Dim num As Integer = strpath.IndexOf("Musiker")
strpath = strpath.Remove(num + 7) & "\music"

'now I want to upload a file

Dim clsRequest As System.Net.FileWebRequest = (System.Net.WebRequest.Create(strpath))
clsRequest.Method = System.Net.WebRequestMethods.File.UploadFile

' read in file...
Dim bFile() As Byte = System.IO.File.ReadAllBytes("pathofFile")

' upload file...
Dim clsStream As System.IO.Stream = clsRequest.GetRequestStream()
clsStream.Write(bFile, 0, bFile.Length)
clsStream.Close()
clsStream.Dispose()

It gives me an error, access not allowed or something like that.

Dani AI

Generated

Summary and practical next steps

attempted a local "upload" into the project folder and ran into access errors; suggested remote UploadFile (which is for network/HTTP/FTP-style uploads), and later realised a local copy is the right approach. Two important clarifications: a runtime file write (what the app does when it runs) is not the same as adding a file to the Visual Studio project (what the IDE shows and includes in builds). To make a file part of the project for distribution it must be added in the IDE (or the project file) and marked as content so it is copied into output during build. (learn.microsoft.com)

Where to save files at runtime

User data should go into a per-user writable folder (AppData, LocalApplicationData, MyDocuments) instead of a program install folder. Writing into Program Files or the installation folder will often fail under UAC unless the process is elevated; that explains the "access denied" errors. Use Environment.GetFolderPath to find the right OS folder and avoid protected locations. (learn.microsoft.com)

Reliable VB.NET pattern

Use Path.Combine to build paths, Directory.CreateDirectory to ensure the folder exists, then stream or write the file. Example pattern (VB.NET):

Imports System.IO

Dim sourcePath As String = "C:\temp\song.mp3"
Dim destFolder As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Musiker", "music")
Directory.CreateDirectory(destFolder)
Dim destPath As String = Path.Combine(destFolder, Path.GetFileName(sourcePath))

Using inFs As New FileStream(sourcePath, FileMode.Open, FileAccess.Read)
    Using outFs As New FileStream(destPath, FileMode.Create, FileAccess.Write)
        inFs.CopyTo(outFs)
    End Using
End Using

Use Path.Combine rather than manual string manipulation and let CreateDirectory handle missing folders. (learn.microsoft.com)

Quick troubleshooting notes

Catch UnauthorizedAccessException to detect permission problems, verify the final path printed to logs, and remember that copying a file at runtime does not add it to the project file or source control — to ship the file with the project use the IDE "Add Existing Item" or set the file as Content/CopyToOutputDirectory before building. (learn.microsoft.com)

Recommended Answers

All 6 Replies

You should use only this line:

My.Computer.Network.UploadFile("source file path","destination address")

:-)

plusplus & martonx.

You are requested to use BB code tags.

My.Computer.Network.UploadFile("source file path","destination address")

Let me change my question. Since I am new to vb.net, but worked in asp.net, I thought I have to upload the file. But really what I have to do, is save it to my project. So what I really needed was

File.Copy(sourceFile, destinationFile)

My question is, when using a path for the destination file, where would I put it, so that it should be part of my project?

Sorry, here with code tags

Let me change my question. Since I am new to vb.net, but worked in asp.net, I thought I have to upload the file. But really what I have to do, is save it to my project. So what I really needed was

File.Copy(sourceFile, destinationFile)

My question is, when using a path for the destination file, where would I put it, so that it should be part of my project?

plusplus & martonx.

You are requested to use BB code tags.

My.Computer.Network.UploadFile("source file path","destination address")

Can you give us example of the destination address with IP
as i am doing a project

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.