ParameterInvalid() static private method

static private ParameterInvalid ( string parameter ) : ArgumentException
parameter string
return System.ArgumentException
Ejemplo n.º 1
0
        internal static string ConvertFileNameToUrl(string fileName)
        {
            string prefix;

            if (IsAbsoluteLocalPhysicalPath(fileName))
            {
                prefix = FileUrlLocal;
            }
            else
            {
                if (IsAbsoluteUncPhysicalPath(fileName))
                {
                    prefix = FileUrlUnc;
                }
                else
                {
                    // We should never get here, but if we do we are likely to have
                    // serious security problems, so throw an exception rather than simply
                    // asserting.
                    throw ExceptionUtil.ParameterInvalid(nameof(fileName));
                }
            }

            string newFileName = prefix + fileName.Replace('\\', '/');

            return(newFileName);
        }
Ejemplo n.º 2
0
 public ConfigurationPermission(PermissionState state)
 {
     switch (state)
     {
     case PermissionState.None:
     case PermissionState.Unrestricted:
         this._permissionState = state;
         return;
     }
     throw ExceptionUtil.ParameterInvalid("state");
 }
Ejemplo n.º 3
0
        private PermissionState _permissionState; // Unrestricted or None

        //
        // Creates a new instance of ConfigurationPermission
        // that passes all demands or that fails all demands.
        //
        public ConfigurationPermission(PermissionState state)
        {
            // validate state parameter
            switch (state)
            {
            case PermissionState.Unrestricted:
            case PermissionState.None:
                _permissionState = state;
                break;

            default:
                throw ExceptionUtil.ParameterInvalid("state");
            }
        }
Ejemplo n.º 4
0
        //
        // Compares two ConfigurationPermission instances
        //
        public override bool IsSubsetOf(IPermission target)
        {
            if (target == null)
            {
                return(_permissionState == PermissionState.None);
            }

            if (target.GetType() != typeof(ConfigurationPermission))
            {
                throw ExceptionUtil.ParameterInvalid("target");
            }

            ConfigurationPermission other = (ConfigurationPermission)target;

            return(_permissionState == PermissionState.None || other._permissionState == PermissionState.Unrestricted);
        }
Ejemplo n.º 5
0
        public override IPermission Intersect(IPermission target)
        {
            if (target == null)
            {
                return(null);
            }
            if (target.GetType() != typeof(ConfigurationPermission))
            {
                throw ExceptionUtil.ParameterInvalid("target");
            }
            if (this._permissionState == PermissionState.None)
            {
                return(new ConfigurationPermission(PermissionState.None));
            }
            ConfigurationPermission permission = (ConfigurationPermission)target;

            return(new ConfigurationPermission(permission._permissionState));
        }
Ejemplo n.º 6
0
        public override IPermission Union(IPermission target)
        {
            if (target == null)
            {
                return(this.Copy());
            }
            if (target.GetType() != typeof(ConfigurationPermission))
            {
                throw ExceptionUtil.ParameterInvalid("target");
            }
            if (this._permissionState == PermissionState.Unrestricted)
            {
                return(new ConfigurationPermission(PermissionState.Unrestricted));
            }
            ConfigurationPermission permission = (ConfigurationPermission)target;

            return(new ConfigurationPermission(permission._permissionState));
        }
Ejemplo n.º 7
0
        internal static string ConvertFileNameToUrl(string fileName)
        {
            string str;

            if (IsAbsoluteLocalPhysicalPath(fileName))
            {
                str = "file:///";
            }
            else
            {
                if (!IsAbsoluteUNCPhysicalPath(fileName))
                {
                    throw ExceptionUtil.ParameterInvalid("filename");
                }
                str = "file:";
            }
            return(str + fileName.Replace('\\', '/'));
        }
Ejemplo n.º 8
0
        public override IPermission Intersect(IPermission target)
        {
            if (target == null)
            {
                return(null);
            }

            if (target.GetType() != typeof(ConfigurationPermission))
            {
                throw ExceptionUtil.ParameterInvalid("target");
            }

            // Create an None permission if either this or other is None
            if (_permissionState == PermissionState.None)
            {
                return(new ConfigurationPermission(PermissionState.None));
            }
            ConfigurationPermission other = (ConfigurationPermission)target;

            return(new ConfigurationPermission(other._permissionState));
        }
Ejemplo n.º 9
0
        public override IPermission Union(IPermission target)
        {
            if (target == null)
            {
                return(Copy());
            }

            if (target.GetType() != typeof(ConfigurationPermission))
            {
                throw ExceptionUtil.ParameterInvalid("target");
            }

            // Create an Unrestricted permission if either this or other is unrestricted
            if (_permissionState == PermissionState.Unrestricted)
            {
                return(new ConfigurationPermission(PermissionState.Unrestricted));
            }
            ConfigurationPermission other = (ConfigurationPermission)target;

            return(new ConfigurationPermission(other._permissionState));
        }
        private ClientConfigPaths(string exePath, bool includeUserConfig)
        {
            this._includesUserConfig = includeUserConfig;
            Assembly exeAssembly         = null;
            string   codeBase            = null;
            string   applicationFilename = null;

            if (exePath == null)
            {
                AppDomainSetup setupInformation = AppDomain.CurrentDomain.SetupInformation;
                this._applicationConfigUri = setupInformation.ConfigurationFile;
                exeAssembly = Assembly.GetEntryAssembly();
                if (exeAssembly != null)
                {
                    this._hasEntryAssembly = true;
                    codeBase = exeAssembly.CodeBase;
                    bool flag = false;
                    if (StringUtil.StartsWithIgnoreCase(codeBase, "file:///"))
                    {
                        flag     = true;
                        codeBase = codeBase.Substring("file:///".Length);
                    }
                    else if (StringUtil.StartsWithIgnoreCase(codeBase, "file://"))
                    {
                        flag     = true;
                        codeBase = codeBase.Substring("file:".Length);
                    }
                    if (flag)
                    {
                        codeBase            = codeBase.Replace('/', '\\');
                        applicationFilename = codeBase;
                    }
                    else
                    {
                        codeBase = exeAssembly.EscapedCodeBase;
                    }
                }
                else
                {
                    StringBuilder buffer = new StringBuilder(260);
                    Microsoft.Win32.UnsafeNativeMethods.GetModuleFileName(new HandleRef(null, IntPtr.Zero), buffer, buffer.Capacity);
                    codeBase            = Path.GetFullPath(buffer.ToString());
                    applicationFilename = codeBase;
                }
            }
            else
            {
                codeBase = Path.GetFullPath(exePath);
                if (!FileUtil.FileExists(codeBase, false))
                {
                    throw ExceptionUtil.ParameterInvalid("exePath");
                }
                applicationFilename = codeBase;
            }
            if (this._applicationConfigUri == null)
            {
                this._applicationConfigUri = codeBase + ".config";
            }
            this._applicationUri = codeBase;
            if ((exePath == null) && this._includesUserConfig)
            {
                bool isHttp = StringUtil.StartsWithIgnoreCase(this._applicationConfigUri, "http://");
                this.SetNamesAndVersion(applicationFilename, exeAssembly, isHttp);
                if (this.IsClickOnceDeployed(AppDomain.CurrentDomain))
                {
                    string data = AppDomain.CurrentDomain.GetData("DataDirectory") as string;
                    string str4 = this.Validate(this._productVersion, false);
                    if (Path.IsPathRooted(data))
                    {
                        this._localConfigDirectory = this.CombineIfValid(data, str4);
                        this._localConfigFilename  = this.CombineIfValid(this._localConfigDirectory, "user.config");
                    }
                }
                else if (!isHttp)
                {
                    string str5 = this.Validate(this._companyName, true);
                    string str6 = this.Validate(AppDomain.CurrentDomain.FriendlyName, true);
                    string str7 = !string.IsNullOrEmpty(this._applicationUri) ? this._applicationUri.ToLower(CultureInfo.InvariantCulture) : null;
                    string str8 = !string.IsNullOrEmpty(str6) ? str6 : this.Validate(this._productName, true);
                    string typeAndHashSuffix = this.GetTypeAndHashSuffix(AppDomain.CurrentDomain, str7);
                    string str10             = (!string.IsNullOrEmpty(str8) && !string.IsNullOrEmpty(typeAndHashSuffix)) ? (str8 + typeAndHashSuffix) : null;
                    string str11             = this.Validate(this._productVersion, false);
                    string str12             = this.CombineIfValid(this.CombineIfValid(str5, str10), str11);
                    string folderPath        = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                    if (Path.IsPathRooted(folderPath))
                    {
                        this._roamingConfigDirectory = this.CombineIfValid(folderPath, str12);
                        this._roamingConfigFilename  = this.CombineIfValid(this._roamingConfigDirectory, "user.config");
                    }
                    string path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                    if (Path.IsPathRooted(path))
                    {
                        this._localConfigDirectory = this.CombineIfValid(path, str12);
                        this._localConfigFilename  = this.CombineIfValid(this._localConfigDirectory, "user.config");
                    }
                }
            }
        }
Ejemplo n.º 11
0
        internal static Configuration OpenExeConfiguration(ConfigurationFileMap fileMap, bool isMachine,
                                                           ConfigurationUserLevel userLevel, string exePath)
        {
            // validate userLevel argument
            switch (userLevel)
            {
            case ConfigurationUserLevel.None:
            case ConfigurationUserLevel.PerUserRoaming:
            case ConfigurationUserLevel.PerUserRoamingAndLocal:
                break;

            default:
                throw ExceptionUtil.ParameterInvalid(nameof(userLevel));
            }

            // validate fileMap arguments
            if (fileMap != null)
            {
                if (string.IsNullOrEmpty(fileMap.MachineConfigFilename))
                {
                    throw ExceptionUtil.ParameterNullOrEmpty(nameof(fileMap) + "." + nameof(fileMap.MachineConfigFilename));
                }

                ExeConfigurationFileMap exeFileMap = fileMap as ExeConfigurationFileMap;
                if (exeFileMap != null)
                {
                    switch (userLevel)
                    {
                    case ConfigurationUserLevel.None:
                        if (string.IsNullOrEmpty(exeFileMap.ExeConfigFilename))
                        {
                            throw ExceptionUtil.ParameterNullOrEmpty(nameof(fileMap) + "." + nameof(exeFileMap.ExeConfigFilename));
                        }
                        break;

                    case ConfigurationUserLevel.PerUserRoaming:
                        if (string.IsNullOrEmpty(exeFileMap.RoamingUserConfigFilename))
                        {
                            throw ExceptionUtil.ParameterNullOrEmpty(nameof(fileMap) + "." + nameof(exeFileMap.RoamingUserConfigFilename));
                        }
                        goto case ConfigurationUserLevel.None;

                    case ConfigurationUserLevel.PerUserRoamingAndLocal:
                        if (string.IsNullOrEmpty(exeFileMap.LocalUserConfigFilename))
                        {
                            throw ExceptionUtil.ParameterNullOrEmpty(nameof(fileMap) + "." + nameof(exeFileMap.LocalUserConfigFilename));
                        }
                        goto case ConfigurationUserLevel.PerUserRoaming;
                    }
                }
            }

            string configPath = null;

            if (isMachine)
            {
                configPath = MachineConfigPath;
            }
            else
            {
                switch (userLevel)
                {
                case ConfigurationUserLevel.None:
                    configPath = ExeConfigPath;
                    break;

                case ConfigurationUserLevel.PerUserRoaming:
                    configPath = RoamingUserConfigPath;
                    break;

                case ConfigurationUserLevel.PerUserRoamingAndLocal:
                    configPath = LocalUserConfigPath;
                    break;
                }
            }

            Configuration configuration = new Configuration(null, typeof(ClientConfigurationHost), fileMap, exePath, configPath);

            return(configuration);
        }
Ejemplo n.º 12
0
        private ClientConfigPaths(string exePath, bool includeUserConfig)
        {
            _includesUserConfig = includeUserConfig;

            Assembly exeAssembly  = null;
            bool     isSingleFile = false;

            if (exePath != null)
            {
                // Exe path was specified, use it
                ApplicationUri = Path.GetFullPath(exePath);
                if (!File.Exists(ApplicationUri))
                {
                    throw ExceptionUtil.ParameterInvalid(nameof(exePath));
                }
            }
            else
            {
                // Exe path wasn't specified, get it from the entry assembly
                exeAssembly = Assembly.GetEntryAssembly();

                // in case of SingleFile deployment, Assembly.Location is empty.
                if (exeAssembly?.Location.Length == 0)
                {
                    isSingleFile     = true;
                    HasEntryAssembly = true;
                }

                if (exeAssembly != null && !isSingleFile)
                {
                    HasEntryAssembly = true;

                    // The original .NET Framework code tried to get the local path without using Uri.
                    // If we ever find a need to do this again be careful with the logic. "file:///" is
                    // used for local paths and "file://" for UNCs. Simply removing the prefix will make
                    // local paths relative on Unix (e.g. "file:///home" will become "home" instead of
                    // "/home").
                    string configBasePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, exeAssembly.ManifestModule.Name);
                    Uri    uri            = new Uri(configBasePath);

                    Debug.Assert(uri.IsFile);
                    ApplicationUri = uri.LocalPath;
                }
                else
                {
                    try
                    {
                        // An EntryAssembly may not be found when running from a custom host.
                        // Try to find the native entry point.
                        using (Process currentProcess = Process.GetCurrentProcess())
                        {
                            ApplicationUri = currentProcess.MainModule?.FileName;
                        }
                    }
                    catch (PlatformNotSupportedException)
                    {
                        ApplicationUri = string.Empty;
                    }
                }
            }

            if (!string.IsNullOrEmpty(ApplicationUri))
            {
                string applicationPath = ApplicationUri;
                if (isSingleFile)
                {
                    // on Unix, we want to first append '.dll' extension and on Windows change '.exe' to '.dll'
                    // eventually, in ApplicationConfigUri we will get '{applicationName}.dll.config'
                    applicationPath = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ?
                                      Path.ChangeExtension(ApplicationUri, ".dll") : ApplicationUri + ".dll";
                }

                ApplicationConfigUri = applicationPath + ConfigExtension;
            }

            // In the case when exePath was explicitly supplied, we will not be able to
            // construct user.config paths, so quit here.
            if (exePath != null)
            {
                return;
            }

            // Skip expensive initialization of user config file information if requested.
            if (!_includesUserConfig)
            {
                return;
            }

            bool isHttp = StringUtil.StartsWithOrdinalIgnoreCase(ApplicationConfigUri, HttpUri);

            SetNamesAndVersion(exeAssembly, isHttp);
            if (isHttp)
            {
                return;
            }

            // Create a directory suffix for local and roaming config of three parts:

            // (1) Company name
            string part1 = Validate(_companyName, limitSize: true);

            // (2) Domain or product name & an application uri hash
            string namePrefix = Validate(AppDomain.CurrentDomain.FriendlyName, limitSize: true);

            if (string.IsNullOrEmpty(namePrefix))
            {
                namePrefix = Validate(ProductName, limitSize: true);
            }
            string applicationUriLower = !string.IsNullOrEmpty(ApplicationUri)
                ? ApplicationUri.ToLowerInvariant()
                : null;
            string hashSuffix = GetTypeAndHashSuffix(applicationUriLower, isSingleFile);
            string part2      = !string.IsNullOrEmpty(namePrefix) && !string.IsNullOrEmpty(hashSuffix)
                ? namePrefix + hashSuffix
                : null;

            // (3) The product version
            string part3 = Validate(ProductVersion, limitSize: false);

            string dirSuffix = CombineIfValid(CombineIfValid(part1, part2), part3);

            string roamingFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

            if (Path.IsPathRooted(roamingFolderPath))
            {
                RoamingConfigDirectory = CombineIfValid(roamingFolderPath, dirSuffix);
                RoamingConfigFilename  = CombineIfValid(RoamingConfigDirectory, UserConfigFilename);
            }

            string localFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

            if (Path.IsPathRooted(localFolderPath))
            {
                LocalConfigDirectory = CombineIfValid(localFolderPath, dirSuffix);
                LocalConfigFilename  = CombineIfValid(LocalConfigDirectory, UserConfigFilename);
            }
        }
        internal static System.Configuration.Configuration OpenExeConfiguration(ConfigurationFileMap fileMap, bool isMachine, ConfigurationUserLevel userLevel, string exePath)
        {
            ExeConfigurationFileMap map;
            string str;
            ConfigurationUserLevel level = userLevel;

            if (((level != ConfigurationUserLevel.None) && (level != ConfigurationUserLevel.PerUserRoaming)) && (level != ConfigurationUserLevel.PerUserRoamingAndLocal))
            {
                throw ExceptionUtil.ParameterInvalid("userLevel");
            }
            if (fileMap != null)
            {
                if (string.IsNullOrEmpty(fileMap.MachineConfigFilename))
                {
                    throw ExceptionUtil.ParameterNullOrEmpty("fileMap.MachineConfigFilename");
                }
                map = fileMap as ExeConfigurationFileMap;
                if (map != null)
                {
                    switch (userLevel)
                    {
                    case ConfigurationUserLevel.None:
                        goto Label_0059;

                    case ConfigurationUserLevel.PerUserRoaming:
                        goto Label_0071;

                    case ConfigurationUserLevel.PerUserRoamingAndLocal:
                        if (string.IsNullOrEmpty(map.LocalUserConfigFilename))
                        {
                            throw ExceptionUtil.ParameterNullOrEmpty("fileMap.LocalUserConfigFilename");
                        }
                        goto Label_0071;
                    }
                }
            }
            goto Label_00A1;
Label_0059:
            if (!string.IsNullOrEmpty(map.ExeConfigFilename))
            {
                goto Label_00A1;
            }
            throw ExceptionUtil.ParameterNullOrEmpty("fileMap.ExeConfigFilename");
Label_0071:
            if (!string.IsNullOrEmpty(map.RoamingUserConfigFilename))
            {
                goto Label_0059;
            }
            throw ExceptionUtil.ParameterNullOrEmpty("fileMap.RoamingUserConfigFilename");
Label_00A1:
            str = null;
            if (isMachine)
            {
                str = "MACHINE";
            }
            else
            {
                switch (userLevel)
                {
                case ConfigurationUserLevel.None:
                    str = "MACHINE/EXE";
                    break;

                case ConfigurationUserLevel.PerUserRoaming:
                    str = "MACHINE/EXE/ROAMING_USER";
                    break;

                case ConfigurationUserLevel.PerUserRoamingAndLocal:
                    str = "MACHINE/EXE/ROAMING_USER/LOCAL_USER";
                    break;
                }
            }
            return(new System.Configuration.Configuration(null, typeof(ClientConfigurationHost), new object[] { fileMap, exePath, str }));
        }
Ejemplo n.º 14
0
        private ClientConfigPaths(string exePath, bool includeUserConfig)
        {
            _includesUserConfig = includeUserConfig;

            Assembly exeAssembly         = null;
            string   applicationFilename = null;

            if (exePath != null)
            {
                // Exe path was specified, use it
                ApplicationUri = Path.GetFullPath(exePath);
                if (!File.Exists(ApplicationUri))
                {
                    throw ExceptionUtil.ParameterInvalid(nameof(exePath));
                }

                applicationFilename = ApplicationUri;
            }
            else
            {
                // Exe path wasn't specified, get it from the entry assembly
                exeAssembly = Assembly.GetEntryAssembly();

                if (exeAssembly == null)
                {
                    throw new PlatformNotSupportedException();
                }

                HasEntryAssembly = true;

                // The original .NET Framework code tried to get the local path without using Uri.
                // If we ever find a need to do this again be careful with the logic. "file:///" is
                // used for local paths and "file://" for UNCs. Simply removing the prefix will make
                // local paths relative on Unix (e.g. "file:///home" will become "home" instead of
                // "/home").
                string configBasePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, exeAssembly.ManifestModule.Name);
                Uri    uri            = new Uri(configBasePath);

                if (uri.IsFile)
                {
                    ApplicationUri      = uri.LocalPath;
                    applicationFilename = uri.LocalPath;
                }
                else
                {
                    ApplicationUri = Uri.EscapeDataString(configBasePath);
                }
            }

            ApplicationConfigUri = ApplicationUri + ConfigExtension;

            // In the case when exePath was explicitly supplied, we will not be able to
            // construct user.config paths, so quit here.
            if (exePath != null)
            {
                return;
            }

            // Skip expensive initialization of user config file information if requested.
            if (!_includesUserConfig)
            {
                return;
            }

            bool isHttp = StringUtil.StartsWithOrdinalIgnoreCase(ApplicationConfigUri, HttpUri);

            SetNamesAndVersion(applicationFilename, exeAssembly, isHttp);
            if (isHttp)
            {
                return;
            }

            // Create a directory suffix for local and roaming config of three parts:

            // (1) Company name
            string part1 = Validate(_companyName, limitSize: true);

            // (2) Domain or product name & an application urit hash
            string namePrefix = Validate(AppDomain.CurrentDomain.FriendlyName, limitSize: true);

            if (string.IsNullOrEmpty(namePrefix))
            {
                namePrefix = Validate(ProductName, limitSize: true);
            }
            string applicationUriLower = !string.IsNullOrEmpty(ApplicationUri)
                ? ApplicationUri.ToLowerInvariant()
                : null;
            string hashSuffix = GetTypeAndHashSuffix(applicationUriLower);
            string part2      = !string.IsNullOrEmpty(namePrefix) && !string.IsNullOrEmpty(hashSuffix)
                ? namePrefix + hashSuffix
                : null;

            // (3) The product version
            string part3 = Validate(ProductVersion, limitSize: false);

            string dirSuffix = CombineIfValid(CombineIfValid(part1, part2), part3);

            string roamingFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

            if (Path.IsPathRooted(roamingFolderPath))
            {
                RoamingConfigDirectory = CombineIfValid(roamingFolderPath, dirSuffix);
                RoamingConfigFilename  = CombineIfValid(RoamingConfigDirectory, UserConfigFilename);
            }

            string localFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

            if (Path.IsPathRooted(localFolderPath))
            {
                LocalConfigDirectory = CombineIfValid(localFolderPath, dirSuffix);
                LocalConfigFilename  = CombineIfValid(LocalConfigDirectory, UserConfigFilename);
            }
        }
Ejemplo n.º 15
0
        private ClientConfigPaths(string exePath, bool includeUserConfig)
        {
            _includesUserConfig = includeUserConfig;

            Assembly exeAssembly         = null;
            string   applicationUri      = null;
            string   applicationFilename = null;

            // get the assembly and applicationUri for the file
            if (exePath == null)
            {
                // First check if a configuration file has been set for this app domain. If so, we will use that.
                // The CLR would already have normalized this, so no further processing necessary.
                AppDomain      domain = AppDomain.CurrentDomain;
                AppDomainSetup setup  = domain.SetupInformation;
                _applicationConfigUri = setup.ConfigurationFile;

                // Now figure out the application path.
                exeAssembly = Assembly.GetEntryAssembly();
                if (exeAssembly != null)
                {
                    _hasEntryAssembly = true;
                    applicationUri    = exeAssembly.CodeBase;

                    bool isFile = false;

                    // If it is a local file URI, convert it to its filename, without invoking Uri class.
                    // example: "file:///C:/WINNT/Microsoft.NET/Framework/v2.0.x86fre/csc.exe"
                    if (StringUtil.StartsWithIgnoreCase(applicationUri, FILE_URI_LOCAL))
                    {
                        isFile         = true;
                        applicationUri = applicationUri.Substring(FILE_URI_LOCAL.Length);
                    }
                    // If it is a UNC file URI, convert it to its filename, without invoking Uri class.
                    // example: "file://server/share/csc.exe"
                    else if (StringUtil.StartsWithIgnoreCase(applicationUri, FILE_URI_UNC))
                    {
                        isFile         = true;
                        applicationUri = applicationUri.Substring(FILE_URI.Length);
                    }

                    if (isFile)
                    {
                        applicationUri      = applicationUri.Replace('/', '\\');
                        applicationFilename = applicationUri;
                    }
                    else
                    {
                        applicationUri = exeAssembly.EscapedCodeBase;
                    }
                }
                else
                {
                    StringBuilder sb = new StringBuilder(MAX_PATH);
                    UnsafeNativeMethods.GetModuleFileName(new HandleRef(null, IntPtr.Zero), sb, sb.Capacity);
                    applicationUri      = Path.GetFullPath(sb.ToString());
                    applicationFilename = applicationUri;
                }
            }
            else
            {
                applicationUri = Path.GetFullPath(exePath);
                if (!FileUtil.FileExists(applicationUri, false))
                {
                    throw ExceptionUtil.ParameterInvalid("exePath");
                }

                applicationFilename = applicationUri;
            }

            // Fallback if we haven't set the app config file path yet.
            if (_applicationConfigUri == null)
            {
                _applicationConfigUri = applicationUri + ConfigExtension;
            }

            // Set application path
            _applicationUri = applicationUri;

            // In the case when exePath was explicitly supplied, we will not be able to
            // construct user.config paths, so quit here.
            if (exePath != null)
            {
                return;
            }

            // Skip expensive initialization of user config file information if requested.
            if (!_includesUserConfig)
            {
                return;
            }

            bool isHttp = StringUtil.StartsWithIgnoreCase(_applicationConfigUri, HTTP_URI);

            SetNamesAndVersion(applicationFilename, exeAssembly, isHttp);

            // Check if this is a clickonce deployed application. If so, point the user config
            // files to the clickonce data directory.
            if (this.IsClickOnceDeployed(AppDomain.CurrentDomain))
            {
                string dataPath      = AppDomain.CurrentDomain.GetData(ClickOnceDataDirectory) as string;
                string versionSuffix = Validate(_productVersion, false);

                // NOTE: No roaming config for clickonce - not supported.
                if (Path.IsPathRooted(dataPath))
                {
                    _localConfigDirectory = CombineIfValid(dataPath, versionSuffix);
                    _localConfigFilename  = CombineIfValid(_localConfigDirectory, UserConfigFilename);
                }
            }
            else if (!isHttp)
            {
                // If we get the config from http, we do not have a roaming or local config directory,
                // as it cannot be edited by the app in those cases because it does not have Full Trust.

                // suffix for user config paths

                string part1 = Validate(_companyName, true);

                string validAppDomainName  = Validate(AppDomain.CurrentDomain.FriendlyName, true);
                string applicationUriLower = !String.IsNullOrEmpty(_applicationUri) ? _applicationUri.ToLower(CultureInfo.InvariantCulture) : null;
                string namePrefix          = !String.IsNullOrEmpty(validAppDomainName) ? validAppDomainName : Validate(_productName, true);
                string hashSuffix          = GetTypeAndHashSuffix(AppDomain.CurrentDomain, applicationUriLower);

                string part2 = (!String.IsNullOrEmpty(namePrefix) && !String.IsNullOrEmpty(hashSuffix)) ? namePrefix + hashSuffix : null;

                string part3 = Validate(_productVersion, false);

                string dirSuffix = CombineIfValid(CombineIfValid(part1, part2), part3);

                string roamingFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                if (Path.IsPathRooted(roamingFolderPath))
                {
                    _roamingConfigDirectory = CombineIfValid(roamingFolderPath, dirSuffix);
                    _roamingConfigFilename  = CombineIfValid(_roamingConfigDirectory, UserConfigFilename);
                }

                string localFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                if (Path.IsPathRooted(localFolderPath))
                {
                    _localConfigDirectory = CombineIfValid(localFolderPath, dirSuffix);
                    _localConfigFilename  = CombineIfValid(_localConfigDirectory, UserConfigFilename);
                }
            }
        }
Ejemplo n.º 16
0
        private ClientConfigPaths(string exePath, bool includeUserConfig)
        {
            _includesUserConfig = includeUserConfig;

            Assembly exeAssembly = null;
            string   applicationUri;
            string   applicationFilename = null;

            // get the assembly and applicationUri for the file
            if (exePath == null)
            {
                // Now figure out the application path.
                exeAssembly = Assembly.GetEntryAssembly();

                if (exeAssembly == null)
                {
                    throw new PlatformNotSupportedException();
                }

                HasEntryAssembly = true;
                applicationUri   = exeAssembly.CodeBase;

                bool isFile = false;

                if (StringUtil.StartsWithOrdinalIgnoreCase(applicationUri, FileUriLocal))
                {
                    // If it is a local file URI, convert it to its filename, without invoking Uri class.
                    // example: "file:///C:/WINNT/Microsoft.NET/Framework/v2.0.x86fre/csc.exe"
                    isFile         = true;
                    applicationUri = applicationUri.Substring(FileUriLocal.Length);
                }
                else
                {
                    // If it is a UNC file URI, convert it to its filename, without invoking Uri class.
                    // example: "file://server/share/csc.exe"
                    if (StringUtil.StartsWithOrdinalIgnoreCase(applicationUri, FileUriUnc))
                    {
                        isFile         = true;
                        applicationUri = applicationUri.Substring(FileUri.Length);
                    }
                }

                if (isFile)
                {
                    applicationUri      = applicationUri.Replace('/', '\\');
                    applicationFilename = applicationUri;
                }
                else
                {
                    applicationUri = exeAssembly.EscapedCodeBase;
                }
            }
            else
            {
                applicationUri = Path.GetFullPath(exePath);
                if (!File.Exists(applicationUri))
                {
                    throw ExceptionUtil.ParameterInvalid(nameof(exePath));
                }

                applicationFilename = applicationUri;
            }

            // Fallback if we haven't set the app config file path yet.
            if (ApplicationConfigUri == null)
            {
                ApplicationConfigUri = applicationUri + ConfigExtension;
            }

            // Set application path
            ApplicationUri = applicationUri;

            // In the case when exePath was explicitly supplied, we will not be able to
            // construct user.config paths, so quit here.
            if (exePath != null)
            {
                return;
            }

            // Skip expensive initialization of user config file information if requested.
            if (!_includesUserConfig)
            {
                return;
            }

            bool isHttp = StringUtil.StartsWithOrdinalIgnoreCase(ApplicationConfigUri, HttpUri);

            SetNamesAndVersion(applicationFilename, exeAssembly, isHttp);
            if (isHttp)
            {
                return;
            }

            string part1 = Validate(_companyName, limitSize: true);
            string validAppDomainName  = Validate(AppDomain.CurrentDomain.FriendlyName, limitSize: true);
            string applicationUriLower = !string.IsNullOrEmpty(ApplicationUri)
                ? ApplicationUri.ToLower(CultureInfo.InvariantCulture)
                : null;
            string namePrefix = !string.IsNullOrEmpty(validAppDomainName)
                ? validAppDomainName
                : Validate(ProductName, limitSize: true);
            string hashSuffix = GetTypeAndHashSuffix(applicationUriLower);

            string part2 = !string.IsNullOrEmpty(namePrefix) && !string.IsNullOrEmpty(hashSuffix)
                ? namePrefix + hashSuffix
                : null;

            string part3 = Validate(ProductVersion, limitSize: false);

            string dirSuffix = CombineIfValid(CombineIfValid(part1, part2), part3);

            string roamingFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

            if (Path.IsPathRooted(roamingFolderPath))
            {
                RoamingConfigDirectory = CombineIfValid(roamingFolderPath, dirSuffix);
                RoamingConfigFilename  = CombineIfValid(RoamingConfigDirectory, UserConfigFilename);
            }

            string localFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);

            if (Path.IsPathRooted(localFolderPath))
            {
                LocalConfigDirectory = CombineIfValid(localFolderPath, dirSuffix);
                LocalConfigFilename  = CombineIfValid(LocalConfigDirectory, UserConfigFilename);
            }
        }