Bonjour,
je veux créer un fichier text dans un répertoire sur le disque dur C de mon PC
or j'ai un message d'erreurj'ai testé plusieurs solutions sans succèsAccess to the path 'C:\Test.txt' is denied.
voici mon codeJe suis débutant merci pour votre aide
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
49
50
51
52
53 class Test { public static void Main() { // Specify the directory you want to manipulate. string path = @"c:\MyDir"; try { // Determine whether the directory exists. if (Directory.Exists(path)) { Console.WriteLine("That path exists already."); try { //Pass the filepath and filename to the StreamWriter Constructor StreamWriter sw = new StreamWriter("C:\\Test.txt"); //Write a line of text sw.WriteLine("Hello World!!"); //Write a second line of text sw.WriteLine("From the StreamWriter class"); //Close the file sw.Close(); } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); } finally { Console.WriteLine("Executing finally block."); } } // Try to create the directory. DirectoryInfo di = Directory.CreateDirectory(path); Console.WriteLine("The directory was created successfully at {0}.", Directory.GetCreationTime(path)); // Delete the directory. /* di.Delete(); Console.WriteLine("The directory was deleted successfully.");*/ } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } finally { } } }
Partager