1

私のアプリは、それが実行されるディレクトリのサブディレクトリでファイルを開き、サブディレクトリが呼び出され、sampleファイルが含まれます:

  • example.raf(例の拡張、重要ではない)
  • background.gif

example.rafへの相対パスが含まれておりbackground.gif(この場合、ファイル名のみがファイルが raf と同じディレクトリにあるため)、RAF を開くと、アプリケーションは を読み取って表示しますbackground.gif

RAF ファイルの読み込みに使用OpenFileDialogすると、すべて問題なく、画像が正しく読み込まれます。ファイルを開くダイアログが何らかの方法で現在の作業ディレクトリに変更されることは知っていますが、ファイルを開くダイアログを呼び出さずにこれを再作成することはできませんでした

残念ながら、このようなファイル形式へのパスを指定せずに、コードから直接raf 読み取りメソッドを呼び出す場合OpenFileDialog

LoadRAF("sample\\example.raf");

この場合、問題が発生しました。アプリは、 RAFファイルとイメージを含むサブディレクトリからではなく、ExecutablePathからイメージをロードしようとします。もちろん、これは正常な動作ですが、この場合は非常に望ましくありません。アプリで相対パスと絶対パスの両方を処理する必要があるため、これを解決するにはどうすればよいですか、ExecutablePath を変更する方法、 または少なくとも.OpenFileDialog

4

3 に答える 3

3

私のプロジェクトZipSolution(http://zipsolution.codeplex.com/)の次のコードは、.netで相対パスを解決および作成する方法を示しています。

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ZipSolution
{
    internal static class RelativePathDiscovery
    {
        /// <summary>
        /// Produces relative path when possible to go from baseLocation to targetLocation
        /// </summary>
        /// <param name="baseLocation">The root folder</param>
        /// <param name="targetLocation">The target folder</param>
        /// <returns>The relative path relative to baseLocation</returns>
        /// <exception cref="ArgumentNullException">base or target locations are null or empty</exception>
        public static string ProduceRelativePath(string baseLocation, string targetLocation)
        {
            if (string.IsNullOrEmpty(baseLocation))
            {
                throw new ArgumentNullException("baseLocation");
            }

            if (string.IsNullOrEmpty(targetLocation))
            {
                throw new ArgumentNullException("targetLocation");
            }

            if (!Path.IsPathRooted(baseLocation))
            {
                return baseLocation;
            }

            if (!Path.IsPathRooted(targetLocation))
            {
                return targetLocation;
            }

            if (string.Compare(Path.GetPathRoot(baseLocation), Path.GetPathRoot(targetLocation), true) != 0)
            {
                return targetLocation;
            }

            if (string.Compare(baseLocation, targetLocation, true) == 0)
            {
                return ".";
            }

            string resultPath = ".";

            if (!targetLocation.EndsWith(@"\"))
            {
                targetLocation = targetLocation + @"\";
            }

            if (baseLocation.EndsWith(@"\"))
            {
                baseLocation = baseLocation.Substring(0, baseLocation.Length - 1);
            }

            while (!targetLocation.StartsWith(baseLocation + @"\", StringComparison.OrdinalIgnoreCase))
            {
                resultPath = resultPath + @"\..";
                baseLocation = Path.GetDirectoryName(baseLocation);

                if (baseLocation.EndsWith(@"\"))
                {
                    baseLocation = baseLocation.Substring(0, baseLocation.Length - 1);
                }
            }

            resultPath = resultPath + targetLocation.Substring(baseLocation.Length);

            // preprocess .\ case
            return resultPath.Substring(2, resultPath.Length - 3);
        }

        /// <summary>
        /// Resolves the relative pathes
        /// </summary>
        /// <param name="relativePath">Relative path</param>
        /// <param name="basePath">base path for discovering</param>
        /// <returns>Resolved path</returns>
        public static string ResolveRelativePath(string relativePath, string basePath)
        {
            if (string.IsNullOrEmpty(basePath))
            {
                throw new ArgumentNullException("basePath");
            }

            if (string.IsNullOrEmpty(relativePath))
            {
                throw new ArgumentNullException("relativePath");
            }

            var result = basePath;

            if (Path.IsPathRooted(relativePath))
            {
                return relativePath;
            }

            if (relativePath.EndsWith(@"\"))
            {
                relativePath = relativePath.Substring(0, relativePath.Length - 1);
            }

            if (relativePath == ".")
            {
                return basePath;
            }

            if (relativePath.StartsWith(@".\"))
            {
                relativePath = relativePath.Substring(2);
            }

            relativePath = relativePath.Replace(@"\.\", @"\");
            if (!relativePath.EndsWith(@"\"))
            {
                relativePath = relativePath + @"\";
            }

            while (!string.IsNullOrEmpty(relativePath))
            {
                int lengthOfOperation = relativePath.IndexOf(@"\") + 1;
                var operation = relativePath.Substring(0, lengthOfOperation - 1);
                relativePath = relativePath.Remove(0, lengthOfOperation);

                if (operation == @"..")
                {
                    result = Path.GetDirectoryName(result);
                }
                else
                {
                    result = Path.Combine(result, operation);
                }
            }

            return result;
        }
    }
}
于 2009-11-17T09:54:33.820 に答える
1

OpenFileDialogは、舞台裏で絶対的な道を吐き出している。

rafファイルの場所がわかっている場合は、次のようにすることができます。

string parentPath = Directory.GetParent(rafFilePath);
string imagePath = Path.Combine(parentPath, imageFileNameFromRaf);

imagePathには、rafファイルに含まれている画像名とrafファイルがあったディレクトリから派生した画像への絶対パスが含まれるようになります。

于 2008-10-02T21:19:57.730 に答える
0

相対パスから読み取る前に、 Environment.CurrentDirectory を使用して、現在のディレクトリを実行可能ファイルを含むディレクトリに変更することができます。または、代わりに、相対パス (Path.IsPathRooted) がある場合は、ルート ディレクトリを相対パスと結合 (Path.Combine) して絶対パスにすることができます。

于 2009-11-17T10:03:43.407 に答える