Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get server.Mappath in console application c#

How to use server.MapPath in the console application using c#. I tried the below code but I am not getting my pdf file. Kindly suggest in this.

 string[] pdf_file = System.IO.Directory.GetFiles(@"C:\Folder\" + fileName);
like image 439
harathi viru Avatar asked Mar 21 '17 11:03

harathi viru


People also ask

What is Server MapPath in C#?

The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server.

What is Server MapPath returns?

If the path starts with either a forward slash(/) or backward slash(\) the MapPath Method returns a path as if the path is a full virtual path. If the path doesn't start with a slash, the MapPath Method returns a path relative to a directory of the . asp file being processed.

Is a physical path but a virtual path was expected in asp net?

Generally this physical and virtual path problem occurred whenever we refer “Server. MapPath” value multiple times while using folder path in applications. To solve this e:is a physical path but a virtual path was expected we need to use Server.


2 Answers

XmlDocument xmlDoc = new XmlDocument();
string path = Directory.GetCurrentDirectory() + "//XMLFile1.xml";
xmlDoc.Load(path);
XmlReader xmlReader = new XmlNodeReader(xmlDoc);
DataSet ds = new DataSet();
ds.ReadXml(xmlReader);
like image 63
Inam Abbas Avatar answered Oct 06 '22 20:10

Inam Abbas


Actually we need to locate application directory which contain folder along with bin folder, and our executable file is present in bin/debug. So we can get the executable file path using Environment.CurrentDirectory and from that we can extract our path as below,

string filepath = Environment.CurrentDirectory;
Console.WriteLine(filepath.Split(new String[] {"bin"}, StringSplitOptions.None)[0]);
like image 25
BornToCode Avatar answered Oct 06 '22 20:10

BornToCode