Le code que j'ai inclus ci-dessous écrit avec succès dans un fichier CSV. Mais si le fichier CSV dans lequel j'écris est ouvert dans Excel, j'obtiens un System.IO.Exception
qui indique que "le fichier est utilisé par un autre processus".Éviter "fichier utilisé par un autre processus" Exception d'E/S
Comment puis-je modifier mon code pour que le programme continue à fonctionner et attendre que le fichier CSV ne soit plus ouvert dans Excel?
private void timer1_Tick(object sender, EventArgs e)
{
int actmonth, actyear, actsecond;
System.DateTime fecha = System.DateTime.Now;
actmonth = fecha.Month;
actyear = fecha.Year;
if (actmonth <= 9)
{
valorfechaact = System.Convert.ToString(actyear) + "00" + System.Convert.ToString(actmonth);
}
else
{
valorfechaact = System.Convert.ToString(actyear) + "0" + System.Convert.ToString(actmonth);
}
actsecond = fecha.Second;
string label;
label = label1.Text;
string at = "@";
string filename = valorfechaact + ".csv";
string ruta3 = System.IO.Path.Combine(at, label, filename);
if (Directory.Exists(label1.Text))
{
StreamWriter wr = new StreamWriter(ruta3, true);
wr.WriteLine("1asd" + actsecond);
wr.Close();
wr.Dispose();
}
else
{
System.Console.WriteLine("no se puede escribir en el archivo");
timer1.Stop();
}
}
Vous ne pouvez pas écraser le fichier si son ouverture d'Excel comme Excel verrouiller. Vous pouvez toujours réécrire le fichier avec un nom unique si vous rencontrez l'erreur d'E/S. –
@AlexK. Merci pour votre réponse rapide, comment puis-je faire cela? –
essayer/attraper sur cette exception? –