Hi Everyone,

I need to write to a text file that is located on my website

Currently, I have the following code to let me read from a text file on my website

HttpWebRequest reqFP3 = (HttpWebRequest)HttpWebRequest.Create("http://jamesgeddes.com/tracknan/phone3.txt");
HttpWebResponse rspFP3 = (HttpWebResponse)reqFP3.GetResponse();
Stream st3 = rspFP3.GetResponseStream();
using (StreamReader sr3 = new StreamReader(st3))
                    {
                        WebTextPhone3 = sr3.ReadLine();
                    }

What do I need to do to write to that file instead?

Thanks everyone!

James

You should probably look at writing a web service. Create the service on your website, add a reference to it in your offline project (it will generate the proxy classes you need), and then you can interact with it.

Here's a sample web service implementation:

using System.Collections.Generic;
using System.IO;
using System.Web.Services;

namespace SampleWebService
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
        private string FileName
        {
            get
            {
                return @"C:\Temp\messages.txt";
            }
        }

        [WebMethod]
        public void PostMessage(string input)
        {
            using (StreamWriter sw = new StreamWriter(this.FileName, true))
            {
                sw.WriteLine(input);
                sw.Close();
            }
        }

        [WebMethod]
        public List<string> ListMessages()
        {
            List<string> messages = new List<string>();
            if (File.Exists(this.FileName))
            {
                using (StreamReader sr = new StreamReader(this.FileName))
                {
                    while (!sr.EndOfStream)
                    {
                        messages.Add(sr.ReadLine());
                    }
                }
            }

            return messages;
        }
    }
}

This exposes two web methods, one that accepts a string input, and one that returns a list of all previous messages. This is then consumed by a client that has established a reference to the service.

The simple client:

using System;
using System.Collections.Generic;

namespace SampleServiceClient
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceReference1.Service1SoapClient client = new ServiceReference1.Service1SoapClient();
            client.PostMessage(DateTime.Now.ToString());
            List<string> messages = client.ListMessages();
            foreach (string message in messages)
                Console.WriteLine(message);

            Console.Read();
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.