0

I have a quiz that has a login feature but, when you change pc you must also change the drive the file is located e.g D drive, E drive etc...

Currently its set to F. Is there something i can add that will make it automatically search each drive for the file?

Here is my code

if (File.Exists(@"F:\C# Completed quiz\adam new\Mario quiz\bin/PlayerDetails.txt"))
{

    string[] lines = File.ReadAllLines(@"F:\C# Completed quiz\adam new\Mario quiz\bin/PlayerDetails.txt");
slugster
  • 49,403
  • 14
  • 95
  • 145
  • 3
    Wouldn't it be better to store the file in a commonly accessible location, such as `%appdata%` (or `Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)` in c#)? – spender Apr 14 '16 at 23:33

3 Answers3

1

I'd recommend you just put it in the AppData or MyDocuments folder:

string filename = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "C# Completed quiz","adam new","Mario quiz","bin","PlayerDetails.txt");
//or Environment.SpecialFolder.MyDocuments

if (File.Exists(filename))
{
    string[] lines = File.ReadAllLines(filename);
}
smead
  • 1,768
  • 15
  • 23
  • 2
    You need a bit of [`System.IO.Path.Combine`](https://msdn.microsoft.com/en-us/library/fyy7a5kt%28v=vs.110%29.aspx) instead of concatenating strings. – spender Apr 14 '16 at 23:36
0

Use Resource file to store your txt inside the application or just distribute it with your executable. To add resource file:

  1. Right click on your project and select Properties
  2. Go to Resources tab and create new file
  3. Click Add resource -> Add Existing File...
  4. Choose your text file and click Open

The file can now be accessed like string:

var lines = Properties.Resources.PlayerDetails;

If the file is in the same folder as your exe then you can access it in this way:

var lines = File.ReadAllLines("PlayerDetails.txt");

Edit: Note the comment below if you prefer to use this method.

Vladyslav
  • 786
  • 4
  • 19
  • Unless the app is installed in a location that needs elevated permissions for writing to (like, for instance, `Program Files`). Not a great idea. – spender Apr 14 '16 at 23:38
0

you need to enumerate the hard drives on the system and inspect them one at a time

https://msdn.microsoft.com/en-us/library/system.io.directory.getlogicaldrives(v=vs.110).aspx

shows how to enumerate the hard drives on the system

This answer uses what looks like a better way

How can I determine if a given drive letter is a local/mapped/usb drive?

Once you know a drive is a mounted drive the you should look at <drive>:/<your path>

Community
  • 1
  • 1
pm100
  • 48,078
  • 23
  • 82
  • 145