防止从网络驱动器运行应用程序

本文关键字:运行 应用程序 驱动器 网络 | 更新日期: 2023-09-27 18:32:48

我想知道是否可以阻止或至少检测我的应用程序是否从网络驱动器而不是本地文件夹/驱动器使用。

我希望通知我的用户,由于严重的性能缺陷,他必须复制源代码并在本地运行它们。

防止从网络驱动器运行应用程序

获取可执行文件的执行路径,获取根目录,然后确定它是否是网络驱动器。

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

请注意,您应该阅读我链接的问题(如何在 .NET 控制台应用程序中获取应用程序的路径?),因为根据您的确切方案,它可能比上面的代码更多。

以防止 ArgumentException 在 GetPathRoot 返回 ''''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
            }