Ejemplo n.º 1
0
 private static String GetAbsolutePath(String path, String paramName,
                                       out MessageListenerException exception)
 {
     exception = null;
     try
     {
         path = Path.GetFullPath(path);
     }
     catch (ArgumentException ex)
     {
         exception = new MessageListenerException("Parameter '" + paramName + "' is invalid.", ex);
     }
     catch (System.Security.SecurityException ex)
     {
         exception = new MessageListenerException("No permission to get absolute path.", ex);
     }
     catch (NotSupportedException ex)
     {
         exception = new MessageListenerException("Path format in parameter '" + paramName
                                                  + "' is not supported.", ex);
     }
     catch (PathTooLongException ex)
     {
         exception = new MessageListenerException("Path in parameter '" + paramName
                                                  + "' is too long.", ex);
     }
     return(path);
 }
Ejemplo n.º 2
0
        public static void SetConfigPath(String path, String baseDir)
        {
            bool isAbsolute = false;

            try
            {
                isAbsolute = Path.IsPathRooted(path);
            }
            catch (ArgumentException ex)
            {
                throw new MessageListenerException("Parameter 'path' is invalid.", ex);
            }
            if (isAbsolute || //To add volume/drive to the rooted path if necessary.
                String.IsNullOrEmpty(baseDir))    //If baseDir is null, path relative to the current working dir
            {
                MessageListenerException ex = null;
                path = GetAbsolutePath(path, "path", out ex);
                if (ex != null)
                {
                    throw ex;
                }
                ConfigPath = path;
                return;
            }

            isAbsolute = false;
            try
            {
                isAbsolute = Path.IsPathRooted(baseDir);
            }
            catch (ArgumentException ex)
            {
                throw new MessageListenerException("Parameter 'baseDir' is invalid.", ex);
            }
            if (!isAbsolute)
            {
                throw new MessageListenerException("Parameter 'baseDir' must be absolute.");
            }
            MessageListenerException exc = null;

            baseDir = GetAbsolutePath(baseDir, "baseDir", out exc); //To add volume/drive to the path if necessary
            if (exc != null)
            {
                throw exc;
            }
            baseDir = baseDir.Replace('\\', '/');
            if (!baseDir.EndsWith("/"))
            {
                baseDir += "/";
            }
            Uri baseUri = new Uri(baseDir.StartsWith("/") ? "file:" + baseDir : "file:///" + baseDir);

            path = path.Replace('\\', '/');
            Uri uri;

            try
            {
                uri = new Uri(baseUri, path);
            }
            catch (Exception ex)
            {
                throw new MessageListenerException("Combination 'baseDir' and 'path' yields invalid path.", ex);
            }
            ConfigPath = uri.AbsolutePath;
        }