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);
The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server.
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.
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.
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);
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]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With