Filestream file directory and extension

Asked

Viewed 209 times

0

How to specify the file creation directory and its extension?

byte[] vetorImagem = (byte[])cmdSelect.ExecuteScalar();
string strNomeArquivo = Convert.ToString(DateTime.Now.ToFileTime());
FileStream fs = new FileStream(strNomeArquivo, FileMode.CreateNew, FileAccess.Write);
fs.Write(vetorImagem, 0, vetorImagem.Length);
fs.Flush();
fs.Close();
pbDefault.Visible = false;
pbUsuario.Image = Image.FromFile(strNomeArquivo);

1 answer

2

Actually what you pass as the first parameter in builder of FileStream is not the name of the file, but the address (path) of the archive.

Its address must contain the path, name and extension of the file in one string.
So after getting the file name you should concatenate the rest of the information to get the full address:

string strNomeArquivo = Convert.ToString(DateTime.Now.ToFileTime()),
localizacaoArquivo = "c:/",
extensaoArquivo = ".png",
enderecoArquivo = localizacaoArquivo + strNomeArquivo + extensaoArquivo;
FileStream fs = new FileStream(enderecoArquivo, FileMode.CreateNew, FileAccess.Write);
...

Note that if you want the path of some default Windows folder, you can get them using the Enum of special system folders.
To the folder My Images user, for example:

string localizacaoArquivo = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
  • my system created the file in the default directory with name ex: Cache131113874626944091 and did not execute the expected action

  • string strNomeArquivo = Convert.ToString("Cache\\" + txtUsuario.Text + DateTime.Now.ToFileTime() + ".ebtemp"),
 extensaoArquivo = "",
 enderecoArquivo = strNomeArquivo + extensaoArquivo;
 FileStream fs = new FileStream(enderecoArquivo, FileMode.Create, FileAccess.ReadWrite);
 fs.Write(vetorImagem, 0, vetorImagem.Length);
 fs.Flush();
 fs.Close();
 pbDefault.Visible = false;
 pbUsuario.Image = Image.FromFile(strNomeArquivo); Modified and worked!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.