c# - ASP.NET Relative Paths in Referenced Libraries -


I have an asp.net website in which I am loading some validation rules from an XML file. This XML file name, without any path information, is difficult coded in a library. (I know that the hard-coded name is not good, but for this example go with it).

When I run the website, the ASP.NET XML file is coded in the source path, where the name is hard in the C # file, it's completely crappy for me. , Because I do not know how at runtime, we are also considering a source path as a possibility of resolving an ineligible file name.

  // config class, in C: \ temp \ Project.Core \ Config.cs public static string validation RulesFile {get {return m_validationRulesFile; }} Private static string m_validationRulesFile = "validation_rules.xml"; // file name using m_validationRules.LoadRulesFromXml (Config.ValidationRulesFile, "call");  

The path we are looking at is an exception that is similar to Config.cs:

  Exception Description: System.IO.FileNotFoundException: File 'C : \ Temp \ Project.Core \ validation_rules.xml 'Do not search'  

Can anyone explain this to me? I already know how you usually want to handle the path in ASP.net, please do not reply with solution. I really want to understand this, because it really makes me wonder, and it's going to bother me not to make any end.

UPDATE

Here is the relevant code for LoadRulesFromXml

  public void LoadRulesFromXml (string in_xmlFileName, string in_type) {XmlDocument xmlDoc = New XmlDocument () ; XmlDoc.Load (in_xmlFileName); ...  

UPDATE2

It seems that the Cassini web server gets its current directory determined by VS, and in fact it is set on the path to my library project is. I'm not sure how VS Determines which project to use for the path, but at least it tells what is happening. Thanks Joe

If you do not provide a path, then the file access will normally use the current task directory As a default, this is probably your web application directory in ASP.NET.

This is usually not a good idea to rely on the current working directory, so you can use Path.Combine to specify a different default directory, e.g. Relative to an AppDomain.CurrentDomain.BaseDirectory, which is also a web application directory for an ASP.NET application.

You must explicitly add the path to the name of the file that opens. You can also try to find the current working directory.

When Cassini moves from Visual Studio, the current directory is inherited from the working directory of Visual Studio, whatever happens: It seems that your case is.

Ie:

  public void LoadRulesFromXml (string in_xmlFileName, string in_type) {// to see what's happening on the debug. WrightLine ("Current Directory" + System. Environmental Current Directory); XmlDocument xmlDoc = New XmlDocument (); // use a clear path xmlDoc.Load (System.IO.Path.Combine (AppDomain.CurrentDomain.BaseDirectory, in_xmlFileName)); ...  

Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -