Copy a file from one server folder to another folder using webservice - c#

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.

Related

System IO exception even though file is added in Visual studio C#

I have a project into which I added a JSON file, but when I try to read the file in code I get this error.
Could not find file 'C:\\Program Files (x86)\\IIS Express\\client_secret.json'.
"ExceptionType": "System.IO.FileNotFoundException"
I have the file added in like this.
I can access it when I copy the full path and then read it.
its Asp.NEt or WEBAPI, i.e. its Web application you can access path by using Server.MapPath method.
Example : Server.MapPath("~/script/data.txt")//this locate file in your script folder on sever
for the folder under than website you need to do like this
Server.MapPath(~/client._secret.json) //here ~ sign means relative path from root
Server.MapPath method gives you physical path of your file on server machine.

Cannot download ".config" files from host via http

I've published my ClickOnce application to my website via FTP.
I've got 404 error (not found) when run "app.application". In log file there is a line that says cannot find "app.exe.config" file.
I noticed that I can access to this file via FTP and online file manager of website, but when I try to use HTTP, this error is shown.
I've created new files with different names on website (all of them have .config or .cfg extension), but result is the same: I cannot access them via HTTP (through address bar of browser).
I am using a windows web hosting server and it has a "web.config" file in the root folder of website.
Should I do any change in server settings or I must rename my .config file?
any help will be appreciated.

Cannot use Server.MapPath to access a external file

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";

C# + File.OpenRead(path_to_file)

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.

Were would the appropriate location to store files in a website? Like a temp file?

I have a MVC application that downloads a file and then has to unzip it and use the data.
Where would the correct location be to store this file?
If I were using a windows Forms application I would store it in the bin and use the following to get the path. I'm trying to do the same for a web application?
String path = Application.StartupPath;
Create a folder in the root of your project and call it 'Temp' or something similar.
Store the downloaded files in it.
I think the best place would be to create a folder outside the root folder of the application so that this folder won't be accessible by website users.
To access this folder you have mainly two options:
create a virtual directory on the IIS level and access it.
Use AspEnableParentPaths, here is an example: http://msdn.microsoft.com/en-us/library/ms524697%28v=vs.90%29.aspx

Categories

Resources