Tom B.
Tom B.

Reputation: 2962

Prevent application being runned from network drive

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.

Upvotes: 1

Views: 306

Answers (2)

GioviQ
GioviQ

Reputation: 95

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
            }

Upvotes: 0

RB.
RB.

Reputation: 37222

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.

Upvotes: 2

Related Questions