70
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

相対パスの取得方法

Last updated at Posted at 2025-12-12

1.はじめに

今回は、ファイルパスの取得方法についてまとめてみました。
なぜこのテーマにしたかというと、HTMLでファイル出力を行う際に、他のファイルを参照して画像を読み込んだPDFを出力する処理をしていたのですが、参照パスをフルパスで記載していたため、実機と検証機で毎回書き換える必要があり、非常に手間でした。
そこで、書き換え不要なファイルパスの取得方法を調べたので、備忘録として残しておきます。
※もし誤りがあればご指摘いただけると助かります。


2.Server.MapPathメソッド

このメソッドは仮想パス(Web アプリケーション内のパス)を物理パス(サーバー上の実際のファイルシステムのパス)に変換できます。
サーバーに対して十分な管理権限がないときなど、ウェブアプリケーションを実行する環境によっては物理パスが実行時まで不明の場合に使うことができます。

string physicalPath = Server.MapPath("~/images/photo.jpg");

3.System.Environment.CurrentDirectoryとSystem.IO.Directory.GetCurrentDirectory

現在のプロセスのカレントディレクトリ(現在のディレクトリ、作業ディレクトリ)を取得または設定する方法。
現在のプロセスのカレントディレクトリを取得、設定するには、Environment.CurrentDirectory プロパティを使います。
または、Directory クラスの GetCurrentDirectorySetCurrentDirectory メソッドを使っても同じことができます。
※例外処理で「呼び出し元に、該当するアクセス許可がありません」があることから、権限がないと引っ張ってこれないこともあるっぽいです。

// 現在のカレントディレクトリを取得
string currentDir = Environment.CurrentDirectory;

// または
string currentDir = Directory.GetCurrentDirectory();

// カレントディレクトリを変更
Directory.SetCurrentDirectory("C:\\NewFolder");

4.Environment.GetFolderPath

デスクトップ、お気に入り、スタートメニュー、システムディレクトリなどの特殊ディレクトリの絶対パスを取得する方法です。
このメソッドを使うことで、ユーザー環境に依存せずに特定のフォルダのパスを取得できます。

// デスクトップのパスを取得
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

// ドキュメントフォルダのパスを取得
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

// プログラムデータフォルダのパスを取得
string programDataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

5.AppDomain.CurrentDomain.BaseDirectory

自分自身のアプリケーションの実行ファイルのパスや、実行ファイルのあるフォルダのパスを取得する方法です。
アプリケーションの起動場所に依存したファイルアクセスを行う際に便利です。

// 実行ファイルのあるディレクトリを取得
string baseDir = AppDomain.CurrentDomain.BaseDirectory;

// 実行ファイルのパスを使ってファイルを読み込む例
string configPath = Path.Combine(baseDir, "config.json");

6. Bitmapメソッド

new Bitmap("path")bitmap.Save("path") のどちらかで画像の読み込みと保存をすることができます。

Bitmap bmp = new Bitmap("input.jpg");

と記載すると、ローカルフォルダの system フォルダ(これはそれぞれの環境に依存します)からのパスとして解釈されるため、相対パスを使用することができません。

7.最後に

今回は様々なファイルパスの取得方法について調べました。
私が相対パスの取得方法として使用したのはServer.MapPathメソッドのHttpContext.Current.Server.MapPath("~/")を使用して仮想パスを取得し、出力したいパスを文字結合させBitmapメソッドを使用して出力しました。
備忘録として記事を書かせてもらいましたが、ファイルの取得方法で困った際は参考にしていただければと思います。

参考:
・Server.MapPathメソッド
https://www.aspnet.keicode.com/aspnet/map-path.php
https://learn.microsoft.com/ja-jp/dotnet/api/system.web.httprequest.applicationpath?view=netframework-4.8.1
・System.Environment.CurrentDirectory:
・System.IO.Directory.GetCurrentDirectory
https://dobon.net/vb/dotnet/file/currentdirectory.html
https://learn.microsoft.com/ja-jp/dotnet/api/system.environment.currentdirectory?view=net-9.0
・Environment.GetFolderPath
https://dobon.net/vb/dotnet/file/getfolderpath.html
・AppDomain.CurrentDomain.BaseDirectory
・Application.StartupPath
・Assembly.GetExecutingAssembly().Location
https://dobon.net/vb/dotnet/vb6/apppath.html

70
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
70
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?