'How to save the filepath of a picture to a database to be dynamic based on the computer in Visual Studio Windows form
I am saving pictures to a database by saving the filepath, then copying the file to the root folder of the database. However, although the code to get the picture of the computer and save it is dynamic, actually saving the filepath of the picture to the database is not.
OpenFileDialog open = new OpenFileDialog(); //open file explorer
open.Filter = "All Images Files (*.png;*.jpeg;*jpg) | *.png;*.jpeg;*jpg";
if (open.ShowDialog() == DialogResult.OK)
{
string path = @open.FileName;
pictureBox1.ImageLocation = @open.FileName;
}
string parent = System.AppDomain.CurrentDomain.BaseDirectory;
string source = @pictureBox1.ImageLocation;
string filename = Path.GetFileName(source);
parent = @parent + @"images\";
string destfile = System.IO.Path.Combine(parent, filename);
pictureBox1.ImageLocation = @destfile;
File.Copy(source, destfile);
The former code was to save and copy the file to the project folder then to get the new file path of that file in the project folder. The path is then saved to the database in a nvarchar column.
But as you can see, the file path only works for 1 PC. Is there a way to make it so that saving it in the database only requires the location of the root folder?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
