user1778459
user1778459

Reputation: 115

C# winforms - Can't find the text file created at Application.ExecutablePath location

I'm creating a text file at executablePath location.

 Path.GetDirectoryName(Application.ExecutablePath) + "\\Paths.txt";

The application works perfectly fine, but for some weird reason when I check the executable directory, the text file is not there.

I have a feeling that it creates the file at some other location, but I can't seem to find it.

I'm creating the file like this;

string PathsDirectory =Path.GetDirectoryName(Application.ExecutablePath)+"\\Paths.txt";
TextWriter tw = new StreamWriter(PathsDirectory);
tw.WriteLine(Data);
tw.Close();

And reading it like this;

string PathsDirectory =Path.GetDirectoryName(Application.ExecutablePath)+"\\Paths.txt";
TextReader tr = new StreamReader(PathsDirectory);
string line;
while ((line = tr.ReadLine()) != null)
{
}

I checked the path after concat and everything looks fine except I can't see the file there.

Ok finally I found the file it's inside;

C:\Users\Alican\AppData\Local\VirtualStore\Program Files (x86)\MyMovies\MyMovies

insted of

C:\Program Files (x86)\MyMovies\MyMovies

Upvotes: 0

Views: 1660

Answers (3)

DonBoitnott
DonBoitnott

Reputation: 11025

You should consider your level of rights when the application is running.

The fact that it's going into the VirtualStore folder tells me that you don't have sufficient rights to write into the ProgramFiles directory as you would like.

Try running as Admin. Generally, writing into the ProgramFiles directory is a bad idea. I would recommend writing to the user space with something like this:

String PathsDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Paths.txt");

That Environment.SpecialFolder enum provides all of the standard user-accessible locations, also things like Desktop.

Upvotes: 1

ja_mesa
ja_mesa

Reputation: 1969

I've tried this and it worked in my computer:

string filePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\path.txt";
TextWriter tw = new StreamWriter(filePath);
tw.WriteLine("How are you?");
tw.Close();
Console.WriteLine(filePath);

I've looked inside the directory and the file is right there, and contains the phrase I wrote:

c:\CDash\tests\path\path\bin\Debug\path.txt

Upvotes: 0

Cam Bruce
Cam Bruce

Reputation: 5689

use Path.Combine(Assembly.GetExecutingAssembly().Location, "Paths.txt");

Upvotes: 1

Related Questions