62335

how to set path to a file in ASP C# properly?

Question:

working on a website here, in ASP and C#.

my problem is, when I try to read a file, it finds the wrong directory. here are the code im using:

addtext.Text = new WebClient().DownloadString("readme.htm");

error look like this:

Could not find file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\readme.htm'.

how do I guide the path to the same as my default.aspx ??

Answer1:

<a href="http://msdn.microsoft.com/en-us/library/ms178116%28v=vs.100%29.aspx" rel="nofollow">This page</a> has pretty much everything you might need to know about resolving paths in ASP .Net

Answer2:

I suggest you to use app keys for saving the address of the folder which contains your files. You can build a hierarchy for different type of files.

<add key="WebResources" value="~/Assets/WebResources/" /> <add key="ImageRoot" value="Images\Web" />

and then access them like this and concatenate them with further folder of hierarchy .

string WebResources = ConfigurationManager.AppSettings["WebResources"]; string dir = System.Web.HttpContext.Current.Server.MapPath(WebResources);

Recommend

  • How can I determine if process is 32 or 64Bit from a handle?
  • How to create a custom Powershell operator?
  • Regex for URL rewrite with optional query string parameters
  • Extending the Django 1.11 User Model
  • What is corresponding c++ data type to SQL numeric(18,0) data type?
  • Other than Linq to SQL does anything else consume INotifyPropertyChanging?
  • Less Conflicting Session Manager for Zope 2
  • powershell Get-Counter -ComputerName parameter on Windows 7
  • Xamarin MonoAndroid Azure mobile service InsertAsync
  • Switch to popup in python using selenium
  • How to pass a value from ASP.NET MVC controller to ASP.NET webforms control inside MVC View?
  • Private IP address in reserved subnet range
  • How To Customize ASP.NET Chart Databound To SqlDataSource
  • Ubuntu and bcrypt
  • Memory error in python- how to use more memory
  • Differences in dis-assembled C code of GCC and Borland?
  • Does Mobilefirst provide a provision to access web services directly?
  • x64 applications using gdi+: what are the consequences on performance?
  • Algorithm for a smudge tool?
  • Installing Hadoop, Java Exception about illegal characters at index 7?
  • Different response to non-authenticated users and AJAX calls
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Spray.io: When (not) to use non-blocking route handling?
  • Modifying destination and filename of gulp-svg-sprite
  • GridView Sorting works once only
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • How do I rollback to a specific git commit
  • Is there a mandatory requirement to switch app.yaml?
  • Benchmarking RAM performance - UWP and C#
  • Hits per day in Google Big Query
  • Angular 2 constructor injection vs direct access
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Linking SubReports Without LinkChild/LinkMaster
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Is there any way to bind data to data.frame by some index?
  • Checking variable from a different class in C#
  • Busy indicator not showing up in wpf window [duplicate]
  • How can i traverse a binary tree from right to left in java?