Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write file to project folder on any computer

Tags:

c#

io

file-io

I'm working on a project for a class. What I have to do is export parsed instructions to a file. Microsoft has this example which explains how to write to a file:

// Compose a string that consists of three lines.
string lines = "First line.\r\nSecond line.\r\nThird line.";

// Write the string to a file.
System.IO.StreamWriter file = new System.IO.StreamWriter("c:\\test.txt");
file.WriteLine(lines);

file.Close();

I'm fine with that part, but is there a way to write the file to the current project's environment/location? I'd like to do that instead of hard coding a specific path (i.e. "C:\\test.txt").

like image 257
blutuu Avatar asked Oct 12 '13 02:10

blutuu


People also ask

What is path to project?

The key to the project structure is the path used to identify the files in a project—the project path. The project path is used within scripts to describe the location of project resources. This project path is based on the project root and is self-contained, which allows for portability.


1 Answers

Yes, just use a relative path. If you use @".\test.txt" ( btw the @ just says I'm doing a string literal, it removes the need for the escape character so you could also do ".\\test.txt" and it would write to the same place) it will write the file to the current working directory which in most cases is the folder containing your program.

like image 102
evanmcdonnal Avatar answered Sep 30 '22 22:09

evanmcdonnal