1
I need to leave the file free, without getting in use, because it is blocking. Follow the code:
StringWriter sw = new StringWriter();
XmlTextWriter tw = new XmlTextWriter(sw);
XmlSerializerNamespaces xsn = new XmlSerializerNamespaces();
xsn.Add("", "http://www.portalfiscal.inf.br/nfe");
XmlSerializer ser = new XmlSerializer(typeof(TNFe));
FileStream arquivo = new FileStream("C:\\" + chave_nfe + "-NFe.xml", FileMode.CreateNew);
ser.Serialize(arquivo, nfe, xsn);
If I do:
sw.Close();
The file is still in use, and does not work as expected. What if I do:
arquivo.Dispose();
It works, only it does not monitor the folder, do not understand why this occurs, the monitoring code is soon after:
form = new FormProgressBar();
form.Show();
int X = 6000;
form.MaximumBar(X);
// Faço o laço para atualizar a barra
for (int i = 0; i < X; i++)
{
// Método que atualiza a barra e o texto da barra
form.AtualizaBarra("Aguarde...");
// Insiro algum código que desejo e vou mostrando o status da atualização
}
// clsdb.ExecutaSQL("insert into nfe (n_nota, chave) values ('" + txtnumero.Text + "','" + digito(chave) + "')");
messagebox = 0;
messageboxxml = 0;
#region MONITORAR PASTA
//Dizemos agora se é para monitorar os subdiretórios ou não
fsw.IncludeSubdirectories = false;
//Através de um Enum dizemos quais eventos devem ser monitorados, modificação da data do arquivo, tamanho, etc...
fsw.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite;
//Dizemos quais tipos de arquivos devem ser monitorados, *.*, *.xml, *.txt, etc...
//fsw.Filter = "*.xml";
//fsw.Filter = "*.ERR";
//Definimos agora os eventos a serem gerados
fsw.Created += new FileSystemEventHandler(fsw_Created);
fsw.Changed += new FileSystemEventHandler(fsw_Changed);
fsw.Error += new ErrorEventHandler(fsw_Error);
// A propriedade abaixo define que a monitoração deve iniciar, se false, a pasta não será monitorada
fsw.EnableRaisingEvents = true;
That’s the code that gets both in the event Created
or Changed
, he does not perform.
FileInfo fileinfo = new FileInfo(e.FullPath);
while (arquivoTravado(fileinfo))
{
Thread.Sleep(1000);
}
System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
xmldoc.Load(e.FullPath);
if (e.FullPath.Length == 97)
{
System.Xml.XmlNode DadosLote = xmldoc.SelectSingleNode("DadosLoteNfe");
System.Xml.XmlNode NumeroLote = DadosLote.SelectSingleNode("NumeroLoteGerado");
string nlote = NumeroLote.InnerText;
clsdb.ExecutaSQL("update nfe set num_lote = '" + nlote + "' where n_nota = '" + txtnumero.Text + "'");
}
How can I do, so that the file is free to be treated, and the monitoring continues to work?
you’ve tried using a block
using
?– JcSaint
No, I’ll try. Thank you.
– Mariana
Pay attention to how the code should be placed in the question for the next few times. Do not create code snippets for c#, for example.
– perozzo
I’m sorry for the mistake.
– Mariana
is using the
FileSystemWatcher
correct ? I believe you are using it wrong. If the file has not yet been closed, it must be firing the eventfsw_Changed
and appears to be working for you. If you close the file, it will not trigger the eventfsw_Created
because the file was created before the event was associated withfsw
. It would be nice if you put in the full code and without these breaks so you can understand what you’re really doing– Rovann Linhalis