Txt file is not created in C: //

1

I have a problem, I made a registry application with web services and I added a Log to verify the failures in case something strange happens, like connection failures.

When I install my program on other computers and I run it, do not believe it, can someone help me? The computer on which I install it has administrator permissions.

This is the sentence I am using:

System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Program Files (x86)\Registro Leon-Comanja\My Product Name\logLeonComanja.txt", true); 

This issue was resolved, well, concluded, because the truth never found my colleagues and I was the reason why he did not create the file, we made all the suggestions mentioned here but he did not leave us, in the end I had to check everything the code every time an error occurred. But I still appreciate that you have commented:)

    
asked by Sandra 14.07.2016 в 17:00
source

2 answers

1

When you add a @ in front of a string ( string ), the backslash \ are interpreted literally, that is, as they are written. So you have to remove a \ from each pair and you will see how it works. If you notice, you will never see a route that has 2 backslash ( \ ) in windows unless it is network and it is only at the beginning.

So it would be like this:

System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Program Files (x86)\Registro Leon-Comanja\My Product Name\logLeonComanja.txt", true); 

Or simply remove the arroba since putting 2 blackslash is the same as using @ (in the case of backslash):

System.IO.StreamWriter file = new System.IO.StreamWriter("C:\Program Files (x86)\Registro Leon-Comanja\My Product Name\logLeonComanja.txt", true); 

Note: verify that the route exists

    
answered by 15.08.2016 в 15:03
1

It is probably necessary to add an at ( @ ) before defining the route:

string[] lines = { "Primera linea", "Segunda linea", "Tercera linea" };
// el método WriteAllLines crea un archivo donde se escribe la coleccion de
// cadenas que definimos en el array 'lines', luego se cierra el archivo.
// No se necesita llamar al metodo Flush() o Close()
System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);

Reference: How to: Write to a Text File (C # Programming Guide) .

Update August 16

The problem is that normal users can not generate files in Program Files and in other locations in system folders ( Program Files x86 ).

What you could do is use a folder in Documentos , in this way it would be saved like this:

C:\Users\<Usuario>\Documents\logLeonComanja.txt

Now, to get the aforementioned route: you need to import using System.IO; and the following code:

var pathToFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal),"logLeonComanja.txt");
    
answered by 14.07.2016 в 20:09