Bonjour,
Mon but est d'enregistrer périodiquement un entier dans un fichier excel dans un répertoire spécifique.
Lorsque le fichier existe, tout marche bien ça enregistre comme il faut. Mais lorsqu'il n'existe pas, il est bien créé mais les valeurs ne sont pas enregistrées.
L'erreur affichée est :
On dirait que lorsque je crée le fichier je garde la main dessus. Comment faire pour reprendre la main?System.IO.IOException: The process cannot access the file "C:\Documents and Settings\augiraud\My Documents\Visual Studio Projects\WindowsApplication4\bin\Debug\data\latency_20100802.xls" because it is being used by another process. at System.IO.__Error.WinIOError(Int32 errorCode, String str) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) at System.IO.StreamWriter.CreateFile(String path, Boolean append) at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize) at System.IO.StreamWriter..ctor(String path, Boolean append) at System.IO.File.AppendText(String path) at WindowsApplication4.EnregistrementFichier.enregistrementDelta(Int32 delta) in c:\documents and settings\augiraud\my documents\visual studio projects\windowsapplication4\enregistrementfichier.cs:line 49
Voici le code:
Merci de m'aider.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public EnregistrementFichier() { try { m_path = Directory.GetCurrentDirectory(); m_fichier = string.Format("latency_{0}.xls",DateTime.Now.ToString("yyyyMMdd")); if (!Directory.Exists(".\\data")) { CEventLog.OnDebug("Création du répertoire data."); Directory.CreateDirectory(".\\data"); } if (!File.Exists(".\\data\\" + m_fichier)) { CEventLog.OnDebug("Création du fichier."); File.Create(".\\data\\" + m_fichier); } } catch (Exception e) { CEventLog.OnError(e.ToString()); } } public void enregistrementDelta (int delta) { StreamWriter sw = null; try { string heure = DateTime.Now.ToLongTimeString(); using (sw = File.AppendText(".\\data\\" + m_fichier)) { sw.WriteLine(heure + "\t" + delta.ToString()); } } catch (Exception e) { CEventLog.OnError(e.ToString()); } finally { if (sw != null) sw.Close(); } }
Partager