Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path generated via subst-command not found by System.IO

Tags:

c#

.net

context:

  • System has one Harddisk with one drive ("C:\")
  • The needed path is located at F:\eventlogging

What I have done:

  • I executed the subst cmd in order to generate the F:\ drive (from C:\)

  • Added the directory "eventlogging"

The problem:

  • In the code, there are parts in which it writes to a file inside of F:\eventlogging
  • Exception gets thrown: System.IO.DirectoryNotFoundException: System.IO.DirectoryNotFoundException: Could not find a part of the path 'F:\eventlogging\'..

What I tried:

  • Entering the path in the explorer works (goes to path)
  • cd in cmd to this path works as well

Additional info:

  • Code can not be changed (common base for many projects)
like image 851
Best_Where_Gives Avatar asked Jan 29 '26 14:01

Best_Where_Gives


1 Answers

If you mapped the drive with an elevated prompt, it will be invisible under non-admin accounts.

You can either run your program as an adminstrator, or use a different method which is visible to both admin and non-admin accounts. This involves adding the following key to the registry (copy/paste the following line into a "temp.reg" file, and double-click it):

REGEDIT4 

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\DOS Devices] 
"F:"="\\??\\C:"

Additional benefit is that this approach is persistent across reboots.

Note: If you are planning to add the key manually using regedit.exe, do not escape the backslashes (i.e. replace each \\ with \ in the expression above), and there are no quotes in that case too (you would add a new String Value to the DOS Devices key, set name to F:, and data to \??\C:)

You also need to restart the system for this to take effect.

Also: I am aware you wrote that the code cannot be changed, but it should be emphasized nevertheless: using a config file instead of hard-coded paths would obviously be the right approach with any application (or even simpler, placing logs somewhere inside current users AppData\Local folder). Rebuilding this common base with a default set to f:\eventlogging wouldn't break other apps, but would allow you to have this flexibility. Same goes for all other hard-coded settings and magic numbers across your code. All logging frameworks use config files to configure their outputs.

like image 199
Groo Avatar answered Jan 31 '26 03:01

Groo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!