What I try to do is create 2 files in the system, one where the user can choose where he wants to save his database file where all the records of his products will be. The other file txt
would be created without the user's permission and will already be located on disk C
, the point is that this file will have the path of the database txt file stored.
The only problem that I present is that the file that I create without the user's permission is that it does not store the path of the database file.
private void btnRuta_Click(object sender, EventArgs e)
{
//Aquí creo 2 variables que serian archivos .txt, creo una para la base de datos en un grid y otra que me almacene la ruta de esta base de dato.
string dataBase;
string rutaDataBase = @"Bibliotecas\Documentos\rutaDataBase.txt";
SaveFileDialog salvarArchivo = new SaveFileDialog();
salvarArchivo.Filter = "archivo de texto|*.txt";
if (salvarArchivo.ShowDialog() == DialogResult.OK) {
dataBase = new FileInfo(salvarArchivo.FileName).DirectoryName;
StreamWriter escritor = new StreamWriter(salvarArchivo.FileName);
escritor.Write(dataBase);//Aqui si funciona, pero el punto es que salga en el otro archivo txt.
escritor.Close();
System.IO.File.CreateText(rutaDataBase).Write(dataBase); //AQUÍ ES DONDE NO ME ESCRIBE LA RUTA DE MI DATABASE EN MI ARCHIVO DE TEXTO
}
}
It turns out that you are not saving the file to the document path, but to a path relative to your current application folder .
If you want to save it to documents, you need to query the system to get the path, for example:
Edition:
From the comments I see that you also have difficulty saving the file, what happens is, as @Orlando already commented, is that you don't close the file.
In order to close it, I recommend that you store a reference to the variable, for example: