예제 #1
0
        /// <summary>
        /// Map relative path to app-domain base directory on both servers, background processes, standalone programs and more.
        /// </summary>
        /// <param name="path"></param>
        /// <param name="context"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string MapPathToBaseDir(string path, ICurrentDirContext context = null, string defaultValue = null)
        {
            if (path == null)
            path = defaultValue;
              if (path == null)
            return null;

              if (context == null)
            context = GetCurrentDirContext();

              path = path.Trim();

              if (IsAbsolutePath(path))
            return path;

              if (!path.Contains("~"))
              {
            if (path.Length > 1 && !(path[0] == '/' || path[0] == '\\'))
              path = "~/" + path;
            else
              path = "~" + path;
            // Unfortunately this does not work: path = Path.Combine("~", path);
              }

              path = context.MapPath(path);
              // Avoid troublesome double-slashes
              path = path.Replace("\\/", "\\").Replace("\\\\", "\\");
              return path;
        }
예제 #2
0
        /// <summary>
        /// Map relative path to app-domain base directory on both servers, background processes, standalone programs and more.
        /// </summary>
        /// <param name="path"></param>
        /// <param name="context"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static string MapPathToBaseDir(string path, ICurrentDirContext context = null, string defaultValue = null)
        {
            if (path == null)
            {
                path = defaultValue;
            }
            if (path == null)
            {
                return(null);
            }

            if (context == null)
            {
                context = GetCurrentDirContext();
            }

            path = path.Trim();

            if (IsAbsolutePath(path))
            {
                return(path);
            }

            if (!path.Contains("~"))
            {
                if (path.Length > 1 && !(path[0] == '/' || path[0] == '\\'))
                {
                    path = "~/" + path;
                }
                else
                {
                    path = "~" + path;
                }
                // Unfortunately this does not work: path = Path.Combine("~", path);
            }

            path = context.MapPath(path);
            // Avoid troublesome double-slashes
            path = path.Replace("\\/", "\\").Replace("\\\\", "\\");
            return(path);
        }