I have a situation where I need to access a network drive in C# but the drive letters may be different on each machine where my application will be running.
For example: On my development PC, the network drive letter looks like this M:\engineering\testerdata\configfile.txt
But on the production test stand machines, the drive letter might be different on each machine depending on how our IT department set them up. In those cases, there maybe some machines with G:\engineering\testerdata\configfile.txt and others with L:\engineering\testerdata\configfile.txt. Regardless, the data will be on a network drive under the folder \engineering\testerdata\configfile.txt.
I’m attempting to write a method that will access this network folder to read a configuration file. I don’t have to worry about credentials because they are already setup on each machine. Also, I don’t want to put this is a local working directory as I would have to manually change the setting on approximately 20 PC’s. Here is what my method looks like.
public string DatabaseSelector()
{
//This method determines which database to use when reading configuration data. If the file does not exist, default to AX.
string DataBaseToUse = "AX"; //This is the default database
string DatabasetoUsePath = "\\\\engineering\\testerdata\\configfile.txt";
StreamReader DBtoUseReader = new StreamReader(DatabasetoUsePath);
if (File.Exists(DatabasetoUsePath)) // if file exists, read file to determine which database to use.
{
DataBaseToUse = DBtoUseReader.ReadToEnd(); // Set to what's in the file. Will be AX or MACPAC
if (DataBaseToUse == "MACPAC")
return DataBaseToUse; //Done - using MACPAC
else
return "AX"; // regardless of anything else, use AX
}
return DataBaseToUse; // if file doesn't exist, us AX.
} // end DatabaseSelector
I always get an exception “An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
Additional information: The network name cannot be found.”
For the string DatabasetoUsePath I have also tried @"\\engineering\testerdata\configfile.txt"; And "\engineering\testerdata\configfile.txt"; Which will not even compile, I get the message “Unrecognized escape sequence”.
I thought this would be easy to implement considering I have done something very similar for reading a local file on the C: drive, but reading from the network drive is turning out to be more difficult than I thought! What am I doing wrong? Thanks in advance!
Take a look at the mapping and use the canonical file (I think it is called like that) name instead For instance I have a drive mapped to I, but it really is
\\DC1\applications\
normally I would adress this via I:\ You can get this information from the explorer, it really shows in my case that I:\ is really \DC1\applications
That would mean that if there is such a thing as I:\engineering you would address it as \DC1\applications\engineering
To view the mapping right click the start button and click open windows explorer. The mapping are displayed on the left under the node of computer. Assuming you have those mappings already.
See in this example the Y drive is mapped to
\\Storage\public
The C drive is a local disk, so is D, E is a DVD, F is also a drive, ...