SilverZorG
quinta-feira, 6 de maio de 2010
Escrevendo Arquivo (Writing File in C#)
Escrevendo Arquivo (Writing File)
private void EscreverArquivo(string nome, string Texto, string caminho)
{
System.IO.FileInfo File = new System.IO.FileInfo(caminho + nome + “.sql”);
if (File.Exists)
{
DialogResult ans = MessageBox.Show(“Arquivo ” + caminho + nome + “.sql” + ” já existente, deseja sobrescrver?”, “Confirmação”, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
if (ans == DialogResult.Yes)
{
EscreveArquivo(nome, Texto.Trim(), caminho);
}
}
else
{
EscreveArquivo(nome, Texto.Trim(), caminho);
}
}
private static void EscreveArquivo(string nome, string Texto, string caminho)
{
System.IO.FileStream fs = new System.IO.FileStream(caminho + @”\” + nome + “.sql”, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.UTF8);
sw.Write(Texto);
sw.Flush();
sw.Close();
fs.Close();
}