Saving to bin folder

bettyboop

Member
Joined
Jan 20, 2022
Messages
6
Programming Experience
Beginner
I would like to allow a user to browse anywhere on their computer for an image or pdf file and have it saved in the bin folder and to the database. We learned in class how to save to the data base BUT the file has to be in the bin folder.
I am writing in c# . An end user would have no idea where that bin folder is. so would like it programmed Hope this makes sense
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,245
Location
Chesapeake, VA
Programming Experience
10+
I am assuming that you are talking about the bin folder of an ASP.NET application. Your choice of VS.NET forum above doesn't make things clear because VS.NET is the IDE, and it has its own bin folder and would not make sense for you to put data there. Also SQL itself also has a bin folder, and it also would not make sense to put data there, either.

If you know the path to the bin folder, then you can just use File.Copy() and use the appropriate paths.

Be warned that this is a dangerous practice of taking potentially untrusted files and copying them into the bin folder of any application. 'bin' traditionally is meant to store the executable binaries -- not data. You should really put those files in different location. In an ASP.NET application that location would be the App_Data folder -- assuming that your web app is running on only one web front end.
 
Last edited:

bettyboop

Member
Joined
Jan 20, 2022
Messages
6
Programming Experience
Beginner
I am assuming that you are talking about the bin folder of an ASP.NET application. Your choice of VS.NET forum above doesn't make things clear because VS.NET is the IDE, and it has its own bin folder and would not make sense for you to put data there. Also SQL itself also has a bin folder, and it also would not make sense to put data there, either.

If you know the path to the bin folder, then you can just use File.Copy() and use the appropriate paths.

Be warned that this is a dangerous practice of taking potentially untrusted files and copying them into the bin folder of any application. 'bin' traditionally is meant to store the executable binaries -- not data. You should really put those files in different location. In an ASP.NET application that location would be the App_Data folder -- assuming that your web app is running on only one web front end.

I will have to research what you are saying as teacher had said but all images in bin folder and put the name of the image in the field of database ( I'm using access) More I dig into this the les I think the teacher knows
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,245
Location
Chesapeake, VA
Programming Experience
10+
On second reading of your post, it doesn't sound like you working on an ASP.NET application since you never mentioned anything about uploading files.

I suspect that the reason why he wanted the files saved in the bin folder is because it makes things simpler if the relative path of that files to the running executable is basically the empty string.

This is okay for demonstrations or prototypes, but definitely not what you want to do in your production code. Also consider that if you follow Windows application guidelines, your app should be installed under the Program Files directory tree. Users will not have write access to that tree normally unless you are running as Administrator. Unlike the pre Windows XP days, Windows guidelines now says that you shouldn't run your app as Administrator.

Imagine if you goofed and forgot to only allow PDF and image files to be copied but allowed executables and DLLs to also be copied. You could potentially overwite one of your own files. At best, it just breaks your app. At worse, you start running a binary that is infected with a virus.
 
Last edited:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,466
Location
Sydney, Australia
Programming Experience
10+
Assuming that this is a Windows Forms application, the Application.StartupPath property will give you the path of the folder that the current executable was run from. That means that you can simply add a file name to that and you have the path of a file in that folder, e.g.
C#:
using (var ofd = new OpenFileDialog())
{
    if (ofd.ShowDialog() == DialogResult.OK)
    {
        var sourceFilePath = ofd.FileName;
        var fileName = Path.GetFileName(sourceFilePath);
        var destinationFilePath = Path.Combine(Application.StartupPath, fileName);

        File.Copy(sourceFilePath, destinationFilePath);
    }
}
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,466
Location
Sydney, Australia
Programming Experience
10+
You may find that some people simply use the file name and omit the path, in which case the path is assumed to be the current directory, i.e. the value of Environment.CurrentDirectory. That will generally be the same as Application.StartupPath for a WinForms app but it won't always be and it can change over the course of a session. If you specifically want the startup folder then you should explicitly state that, as shown. If you want to use the current directory, regardless of where that might be, it's best to be explicit about that too. If you simply omit the folder path and let it assume the current folder, anyone reading the code will not know whether that was on purpose or by mistake.
 

Latest posts

Top Bottom