-1

I'm trying to write text to file on server from winform desktop application

 string path = "http://www.site.info/doc.txt";

To use path:

   System.Web.HttpContext.Current.Server.MapPath(path);

also I tried this way:

   System.Web.Hosting.HostingEnvironment.MapPath(path);

to write text into the text document:

using (StreamWriter _testData = new StreamWriter(Server.MapPath("~/doc.txt"), true))
{
    _testData.WriteLine("TEXT"); 
}

Seems like I'm doing something wrong,

name Server "does not exists in current context".

Not sure how to use Server.MapPath.

it is in References as System.Web not System.Web.dll, not sure, but it must be same, and in using as System.Web;

Also I am using System.Net; so maybe I can do it with WebClient.

nikorio
  • 671
  • 4
  • 16
  • 28
  • Not many contemporary web sites allow you direct write access to files on the web server. Much better to provide an API to perform the job server side. Consider looking into REST? –  Mar 14 '17 at 07:02
  • @MickyD Hello, I want write it only on my server – nikorio Mar 14 '17 at 12:26
  • Are you saying that both the WinForms app **and** the web server are the _same computer_?? –  Mar 14 '17 at 13:05
  • @MickyD no it is application on user computers, which writes to my server – nikorio Mar 14 '17 at 13:26
  • Well, I have no idea what you mean by _"I want write it only on my server"_ –  Mar 14 '17 at 13:38

2 Answers2

0

Why are you using Server.MapPath in a winform desktop application.

Download the file using something like this:

WebClient webClient = new WebClient();
var filearray = webClient.DownloadData(path);

and then write it to your local after modification (if needed) using

File.WriteAllBytes(savefilePath, filearray);

And then upload using webClient.UploadData(address, filearray).

Sadique
  • 22,572
  • 7
  • 65
  • 91
  • @MickyD see my edit. I mentioned OP should upload the file. – Sadique Mar 14 '17 at 06:24
  • @Sadiq Hello, this way allows me to read from file on server and write value to file on my computer, I do not need to download it to read. my question is how to properly write string from my desktop winform app to file to my server which already contains empty file. Or if this way is wrong, I need to load file with string to my server from my app, not sure, how to do it – nikorio Mar 14 '17 at 13:48
  • @nikorio - For that you will have to write Server-Side code. Make a Web API for that. – Sadique Mar 14 '17 at 13:49
0

You are trying to modify the file on server which the server wont allow as this could be misused and harm the server. You can update the file through the website hosting this text file.

The Server.Map path should be used in the website where you want to modify the file. If the file is asp.net web form website then you can make a aspx page that will modify file for you. If it is MVC then you will need a Action method in Controller to modify file for you.

If you want you own modified copy then you can download it and save it locally the Winform application as suggested by Sadiq. You can also upload the file by again your server side must allow this.

Adil
  • 146,340
  • 25
  • 209
  • 204