1

I wonder if it was possible to prevent or at least detect if my application is being used from a network drive instead of a local folder/drive.

I wish to inform my user that he has to copy the sources and run them locally because of heavy performance pitfalls.

Tom B.
  • 2,892
  • 3
  • 13
  • 34
  • Can you perfom a few of the critical actions and time them? This way, your app will warn the user even if he has a slow local disk and won't bother him if he has a really fast network. – Haukinger Feb 01 '16 at 13:44
  • Our company network IS slow, that is a constant, that's the reason :) – Tom B. Feb 01 '16 at 13:45
  • @Haukinger I think the issue with your approach is that in one scenario the user can easily do something about it (copy the files locally), but in the other they can't (e.g. ask IT to install an SSD - could take months!). However, your approach has merits in the general case :) – RB. Feb 01 '16 at 13:55
  • 2
    @TomB. Have you considered using ClickOnce deployment to resolve this - just publish the ClickOnce installer on the network share, rather than the app itself, so they *have* to install it locally. I've answered your question as asked below, but feel that this comment is the *correct* approach :) – RB. Feb 01 '16 at 13:56
  • I'm familiar with clickonce but it doesn't apply in this very specific scenario. It's for one-time batches only which are executed by 1 person. But they always forget to copy them from the network drive. So it's a failsafe :). And I'm wondering why this kind of question gets a downvote. It's clear and it's justified – Tom B. Feb 01 '16 at 14:00
  • 1
    @TomB. Not sure why the question got downvoted - seems clear and reasonable to me. Upvoted to balance it out :) I still think ClickOnce is the answer though - even for 1 user ;-) – RB. Feb 01 '16 at 14:02

2 Answers2

2

Get the path to where your executable has been executed from, get the root, then find out if it is a network drive.

var root = Path.GetPathRoot(Assembly.GetEntryAssembly().Location)
var driveInfo = new DriveInfo(root));
if (driveInfo.DriveType == DriveType.Network) {
    // Alert that this will be slow.
}

Note that you should read the question I linked (How can I get the application's path in a .NET console application?), as there is potentially a bit more to it than the code above, depending on your exact scenario.

Community
  • 1
  • 1
RB.
  • 36,301
  • 12
  • 91
  • 131
0

to prevent ArgumentException in case GetPathRoot returns \\Share\myshare

            DriveInfo driveInfo = null;

            try
            {
                driveInfo = new DriveInfo(Path.GetPathRoot(Assembly.GetEntryAssembly().Location));
            }
            catch (Exception)
            {
            }

            if (driveInfo == null || driveInfo.DriveType == DriveType.Network)
            {
                //not allowed
            }
GioviQ
  • 75
  • 1
  • 9