xelement.load(“〜/app_data/file.xml”)パスの一部を見つけることができませんでした

StackOverflow https://stackoverflow.com/questions/3063614

質問

私はlinqtoxmlに新しいです。 xelement.load( "")メソッドを使用したい。しかし、コンパイラは私のファイルを見つけることができません。 XMLファイルの正しいパスを書くのを手伝ってくれますか?注意:app_codeでクラスを定義し、App_dataにあるメソッドのいずれかとXMLファイルでXMLファイルデータを使用したいと考えています。

settings = XElement.Load("App_Data/AppSettings.xml");

使えない Request.ApplicationPathPage.MapPath() また Server.MapPath() 私はクラスの継承フォームページクラスにいないため、私のファイルの物理的なパスを取得します。

簡単なエラーメッセージ:
パスの一部を見つけることができませんでした」C:プログラムファイル Microsoft Visual Studio 9.0 Common7 IDE app_data appsettings.xml'.

コンパイルされたパスは私のプロジェクトパスとは完全に異なります(g: myprojects asp.netプロジェクト vistacomputer website app_data appsettings.xml)

完全なエラーメッセージはこちらです:

System.IO.DirectoryNotFoundException was unhandled by user code
  Message="Could not find a part of the path 'C:\\Program Files\\Microsoft Visual Studio 9.0\\Common7\\IDE\\App_Data\\AppSettings.xml'."
  Source="mscorlib"
  StackTrace:
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
       at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials)
       at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
       at System.Xml.XmlReader.Create(String inputUri, XmlReaderSettings settings, XmlParserContext inputContext)
       at System.Xml.XmlReader.Create(String inputUri, XmlReaderSettings settings)
       at System.Xml.Linq.XElement.Load(String uri, LoadOptions options)
       at System.Xml.Linq.XElement.Load(String uri)
       at ProductActions.Add(Int32 catId, String title, String price, String website, String shortDesc, String fullDesc, Boolean active, Boolean editorPick, String fileName, Stream image) in g:\MyProjects\ASP.net Projects\VistaComputer\Website\App_Code\ProductActions.cs:line 67
       at CMS_Products_Operations.Button1_Click(Object sender, EventArgs e) in g:\MyProjects\ASP.net Projects\VistaComputer\Website\CMS\Products\Operations.aspx.cs:line 72
       at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
       at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
       at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 
役に立ちましたか?

解決

試してみることができます hostingenvironment.applicationphysicalpath 静的プロパティ(これがASP.NETアプリケーションで使用されていると仮定):

string filePath = Path.Combine(
    HostingEnvironment.ApplicationPhysicalPath, 
    @"App_Data\AppSettings.xml"
);

私は別のより良いアプローチは、ファイル名をパラメーターとして使用し、1日の終わりにアクセスできるWebformから呼び出される再利用可能な関数を書くことです。 Server.MapPath. 。これの利点は、この関数がASP.NETエンジンに依存しなくなり、ファイル名が異なる方法で計算される他のアプリケーションで再利用できることです。したがって、基本的に懸念を分離します。

  1. ファイル名の場所を計算します
  2. ファイル名を渡します。
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top