1

On my local computer I created the network drive D:/ that points to \\MyComputerName\D.

On my local disk C:/ I created a folder with the name D and configured a network share with permission to everyone on that share.

Then I start Windows PowerShell ISE. My working directory is C:\Users\username

After that I create a folder with Powershell within this network drive such as

  [System.IO.Directory]::CreateDirectory("D:/Foo/Bar")

the directory Foo with his subfolder Bar will be created successfully.

Then I remove the Foo folder within D:/ and start Visual Studio 2019 with Admin Permissions.

Then I create a simple C#.NET Console Application such as

class Program
{
    static void Main(string[] args)
    {
        System.IO.Directory.CreateDirectory(@"D:/Foo/Bar");

    }
}

If I run the Console Application the directory will not be created and I get System.IO.DirectoryNotFoundException: 'Could not find a part of the path 'D:\Foo\Bar'.'

What should I do to create successfully the folder with my simple console application?

kbisang
  • 558
  • 4
  • 13

2 Answers2

1

Fundamentally, if you run a process with elevation (as administrator), it will not see the drive mappings you've established while running non-elevated.

You can verify this as follows:

# Establish a (temporary) mapping; making it persistent makes no difference.
# Note: This assumes that the admin shares are turned on.
net use T: "\\$(hostname)\c$\Users"

# Start a new PowerShell process *elevated* (as admin)
# You'll see that it can't find drive T:
Start-Process -Verb Runas powershell '-noprofile -c Get-ChildItem T:; pause'

# Clean up
net use T: /del

The simplest workaround is to use the UNC path directly to create your folder.

If you don't want to hard-code that into your application or you don't want to re-create the mapping for your elevated incarnation beforehand, you'll have to find some other way to communicate the drive-mapping information from your non-elevated incarnation to your elevated one.

If all code that needs to see the mapped drive is run via PowerShell, a solution is to place net use calls in the $PROFILE file.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • 1
    Thank you very much for your explanation. I started a command prompt _with_ admin permissions and deleted my network drive ```D``` first with ```net use D: /del```. After that I created the network drive ```D``` again with ```net use D: "\\%COMPUTERNAME%\D``` and then my simple console application above creates successfully the folder ```Foo``` with his subfolder ```Bar``` in network drive ```D```! :-) – kbisang Jan 23 '20 at 14:46
  • I have the same problem. when i run my application on debug it create om folder network ok. but when i deloy it to IIS it had error : Could not find a part of the path Do you know why? – Hùngkk Dec 13 '21 at 07:11
0

You could start by checking if you can reach the D drive by using Directory.exists(string path). If you can't find it, you will not be able to create a directory. Maybe permissions are missing somewhere.

  • Thanks for your hint. Of course this would be a reasonable check to not run into the ```System.IO.DirectoryNotFoundException```. But what should I do to write into the network drive in my example above. I just compared the permissions in the security tab of the ```C``` drive with the permissions in the security tab of my network drive ```D```. I saw that the Groups ```Authenticated Users``` and ```Users``` and the user ```SYSTEM``` missed. I added them on the security tab of network drive ```D``` and gave them all full access. But the problem is still the same as described in my question. – kbisang Jan 23 '20 at 14:17
  • Also I added the user ```Everyone``` to the security tab on network drive ```D``` without success. – kbisang Jan 23 '20 at 14:19
  • But try it, that way you will know if you actually have access to the D drive from your application. If it doesn't work then something is wrong somewhere else. – Jonathan Mathieu Jan 23 '20 at 14:21