1

So I'm making a Tic Tac Toe application and have created a Text file linked to the program to hold the following information:

  • the name
  • the time took to win
  • the difficulty

I know the timer is redundant for a quick game like Tic Tac Toe but I'll use it in the future for other programs.

  • My question is how can I find the full path of the file while only knowing the name of the file?

I want to do this using the program so it can be transferred to any computer and still be able to access the file without the user having to input it.

The code I've tried is:

string file_name = Path.Combine(Environment.CurrentDirectory, "Tic Tac Toe\\HighScores.txt"); 

But this just looks in the Debug folder, where the file isn't located. The application is entirely a console application.

6
  • 2
    So you want to search whole hard disk? Commented Nov 19, 2015 at 16:09
  • Many files can have the same name, so you will have to know where to look. Commented Nov 19, 2015 at 16:12
  • Have you considered that the "CurrentDirectory" will be wherever the program is being run? Thus, if you copied the file to the root of a drive, then it may look there? Commented Nov 19, 2015 at 16:13
  • 1. Preferably I don't want to search the whole hard disk. 2. I was thinking using a partial directory e.g. Tic Tac Toe\Tic Tac Toe\HighScores.txt.3. I found the code on that and realised it when I ran it, I'm not too sure about moving the file around incase I break anything, and this program has took a hell of a lot of time as it is. Commented Nov 19, 2015 at 16:16
  • You, as the programmer, have to decide where your game should save (and thus expect to find) that file. For example, Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Tic Tac Toe", "HighScores.txt"). Commented Nov 19, 2015 at 16:21

2 Answers 2

1

Try to dedicate the file in a fixed sub directory:

\TicTacToe.exe \settings\settings.cfg

So the path is dependent of your executable file.

You'll fetch the directory by calling Directory.GetCurrentDirectory()

You can set a desired directory by setting Environment.CurrentDirectory

A common way to handle this case is the one described above.

Another would be to use user specifiy directories like the %appdata% path and create a dedicated directory there.

%appdata%\TicTacToe\settings.cfg

Everytime your application starts it should lookup the folder %appdata%\TicTacToe\

If it is present, your application has been executed with this user. If not, just create a new one, so we know it's the first run.

You can get the %appdata% path by calling

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) 

Example of what i would have done

private void setUp(){ string filename = "settings.cfg"; string dir = "TicTacToe"; string appdata =Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); string fullpath = Path.Combine(Path.Combine(appdata,dir),filename); //check if file exists, more accurate than just looking for the folder if(File.Exists(fullpath )){ //read the file and process its content }else{ Directory.CreateDirectory(Path.Combine(appdata,dir)); // will do nothing if directory exists, but then we have a bug: no file, but directory available using (FileStream fs = File.Create(fullpath)) { Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file."); // Add some information to the file. fs.Write(info, 0, info.Length); } } } 

Hope it helped.

Sign up to request clarification or add additional context in comments.

2 Comments

so what would be in the config file? the highscoring data?
All data that has to be saved: the name, the time took to win, the difficulty
1

Perhaps have a configuration file for your application and store the directory name in there.

An old example from MS, but should still be applicable...

How to store and retrieve custom information from an application configuration file by using Visual C#

2 Comments

I'm sorry... but how do I do that? I've had to teach myself C# and only been doing it for about 14 weeks
Thankfully configuration files is a feature Visual Studio provides lots of direct support for through its project settings. It generates a namespace you can use in your code, based on the project name.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.