I have a xml file which is in a folder in the solution. I tried to access it using Server.MapPath. It was working fine in a aspx page of a different project. When I tried to access the file in my class library project, I am not supposed to use Server.MapPath. So I tried with HttpContext.Current.Server.MapPath. Problem is this class library project is calling from a separate WCF service project, so current server is WCF service project's server. So it ended up with error path is not valid.
This is what I tried- HttpContext.Current.Server.MapPath("./folder/conf.xml.config")
Any solution?
System.Web is already imported.
You can create a virtual-directory inside the web-services. This virtual-directory will point to Physical location of "folder/conf.xml.config".
Once this is done, you can access it using your existing code like below...
Server.MapPath("folder/conf.xml.config")
Server.MapPath works only with files that are inside the website and is used by specifying a relative location:
string configFile = Server.MapPath("~/App_Data/config.xml.config");
If you want to access a file from some other location you will have to manually provide the absolute path to it:
string configFile = #"c:\work\some_folder\config.xml.config";
Related
I am making a wcf service application how do I get the full path of a xml file? The file is on a visual studio project folder, is going to move to another computer and the path will be change.
Server.MapPath("/PathToYourFiles");
More info here https://msdn.microsoft.com/en-us/library/system.web.httpserverutility.mappath(v=vs.110).aspx
I removed using System.Web.dll;
and add the following:
string path = System.Web.Hosting.HostingEnvironment.MapPath("/fileName.xml");
Im developing a WCF web service with receives always and only XML.
So i need to validate that input XML using their XSD. The question is, can i save them inside web service? Locally i can access XSD files via relative path into IIS Express root folder which i created manually. I tried add the XSD files in VS project but i just cant a find them on runtime.
I'm using the Shemas like this: Image1 Link
IIS XSD'd Folder Path Workaround: Image2 Link
At the moment, its working fine, the problem will be when i try deploy the service somewhere on internet.
Thank you.
tl;dr: Can i send some XSD when deploying the webservice or its just impossible?
Files that belong to your solution should be physically part of it. Once that is the case you can use for instance HostingEnvironment.MapPath; or look at the answers to this question. Note that there is a possible issue with HostingEnvironment.MapPath when the WCF service is self hosted.
A possible solution is this method:
public static string MapPath(string path)
{
if (HttpContext.Current != null)
return HttpContext.Current.Server.MapPath(path);
return HostingEnvironment.MapPath(path);
}
The parameter path needs to be of the format "~/XSD/MyFile.xsd", with the folder "XSD" being located in the root of your WCF service.
NEVER create folders in c:\program files (x86)\iis express.
You can add the XSD as a resource, then load it from your assembly. Add the XSD to your project, and under the "Properties Explorer", set the "Build Action" to "Embedded Resource". You can then read the file with:
var schemaSet = new XmlSchemaSet();
schemaSet.Add("", XmlReader.Create(typeof(SomeClassInTheSameAssembly).Assembly
.GetManifestResourceStream("Full.Namespace.XsdName.xsd")));
See Working with Embedded Resources or Loading XmlSchema files out of Assembly Resources for more.
One way you can do it is by using an application setting in your config file that will hold a base file location such as:
<appSettings>
<add key="BaseDir" value="C:\your\folder\names" />
</appSettings>
Then in your program, when you need a file, you would do something like this:
string fileLocation = System.Configuration.ConfigurationManager.AppSettings["BaseDir"] +
#"\your\file\location\file.xsd";
To use System.Configuration.ConfigurationManager, you will need to add a reference to System.Configuration.
I am developing a HTML5 based WebApp being hosted in IIS7. This webapp sends requests to webservices being hosted in IIS7.
The service initialization looks up for a specific file e.g: "appfile.txt" as
FileStream stream = File.OpenRead("appfile.txt"); // opens file for reading.
This call when run as a console application looks up in the project\bin or output directory and able to locate the specified file.
But the same hosted in IIS7 looks up in "C:\windows\system32\inetsrv\appfile.txt".
Are there any configuration item having used in web.config locates the file from the Bin directory of the IIS7 application and not anywhere else?
Any help is much appreciated.
If the app file is in your web application folder, try using Server.MapPath to get The location of the file relative to the root of the web app:
File.OpenRead(Server.MapPath("~/appfile.txt"))
That should work. You probably need to set up the appfile.txt properties so it is copied to the output folder.
You can certainly create your own section in the web.config file to grab the file path that you want. It doesn't need to have been predefined. Then just use any XML reader you want.
Alternatively, you can make a .resx file very easily in Visual Studio and just populate the path there as a variable.
Thanks for all your valuable comments, I could resolve this issue myself using the following code snippets
string path = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
string[] labs = File.ReadAllLines(path + "/appfile.txt");
This bit of code allows me to read the file contents without any issues.
I am using a webservice to accept a URL of an already uploaded file
and want to copy this file (which is in one server with public access) to another server folder
issue is that when Server.Mappath is used, it is always referring to the web Service project location and not to the URL's location.
Is there anyway that I can copy the file using webservice with the URL only?
if the file is located in a different web site than the webservice, then you have to tell your webservice where are the files located. You could put a key in web.config for this location and then compose the filename:
Path.Combine(ConfigurationManager.AppSettings["FilesPath"],fileName);
Be aware that you have to have access rights to that folder.
I included a text file as output in a WCF set up project.
The text file is correctly located in the same folder with the dll, exe, and config file after the project is installed (C:\Program Files\KLTesting\KLTestApp).
However when the program tries to read it, it looks under "C:\Windows\system32", what's the correct way to find & read it?
I have
string a = Directory.GetCurrentDirectory();
a += "/R0303.txt";
string content = File.ReadAllText(a);
Thanks.
You should use AppDomain.CurrentDomain.BaseDirectory or AppDomain.CurrentDomain.SetupInformation.ApplicationBase instead, to get the Directory of your .exe file.
First you should not call Directory.GetCurrentDirectory() and concatenate with the file name. The service is running within a web server like IIS or some other type of container, so GetCurrentDirectory will not give you what you are thinking as you found out. (On a quick tangent, as a recommendation in the future if you want to do path combining you should use Path.Combine method as it will handle all the corner cases and be cross platform.)
There are a few ways to do this:
Assembly myAssembly = Assembly.GetAssembly(SomeTypeInAssm.GetType());
string fullFileName = Path.Combine(myAssembly.Location, "MyFile.txt");
The fullFileName should be what you are looking for. Make sure you use a type that is actually in an assembly located and referenced from the directory in question. However be aware because this file in your question in the Program Files area this is a secure area on Vista and higher OS's and you may not have access to do anything but read the file.
Also you could have the installer of the application place in the registry the install path. Then your service if on the same box can pull this information.
Also you could make sure the file you want to load is within the web server environment that is accessable to the WCF service in question.