Zerato
Zerato

Reputation: 703

Write to web text file

I am programming in Microsoft Visual C# 2010 Express. I have a text file in a folder on my web server containing one character: '0'. When I start my C# Application I want to read the number from my text file, increase it by 1, and then save the new number instead.

I've browsed the web but I can't find a good answer. All I get is questions and answers about writing/reading from local text files.

So basically, I want to write some text to a text file which is not on my computer but here: http://mywebsite.xxx/something/something/myfile.txt

Is this possible?

Upvotes: 0

Views: 2168

Answers (2)

Zerato
Zerato

Reputation: 703

I found a working solution, using File Transport Protocol as Barta Tamás mentioned. However, I learned from Michael Todd that this is not safe, so I will not use it in my own application, but maybe it can be helpful to someone else.

I found information about uploading files using FTP here: http://msdn.microsoft.com/en-us/library/ms229715.aspx

    void CheckNumberOfUses()
    {
        // Get the objects used to communicate with the server.
        FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create("ftp://mywebsite.xx/public_html/something1/something2/myfile.txt");
        HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create("http://mywebsite.xx/something1/something2/myfile.txt");

        StringBuilder sb = new StringBuilder();
        byte[] buf = new byte[8192];
        HttpWebResponse response = (HttpWebResponse)httpRequest.GetResponse();
        Stream resStream = response.GetResponseStream();
        string tempString = null;
        int count = resStream.Read(buf, 0, buf.Length);
        if (count != 0)
        {
            tempString = Encoding.ASCII.GetString(buf, 0, count);
            int numberOfUses = int.Parse(tempString) + 1;
            sb.Append(numberOfUses);
        }

        ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;

        // This example assumes the FTP site uses anonymous logon.
        ftpRequest.Credentials = new NetworkCredential("login", "password");

        // Copy the contents of the file to the request stream.
        byte[] fileContents = Encoding.UTF8.GetBytes(sb.ToString());
        ftpRequest.ContentLength = fileContents.Length;
        Stream requestStream = ftpRequest.GetRequestStream();
        requestStream.Write(fileContents, 0, fileContents.Length);
        requestStream.Close();
        FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
        ftpResponse.Close();
    }    

Read in the comments of the question how this can be done better, not using FTP. My solution is not suggested if you have important files on your server.

Upvotes: 0

Kevin DeVoe
Kevin DeVoe

Reputation: 602

You may have to adjust the path directory, but this works:

    string path = Path.GetDirectoryName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile) + "\\something\\myfile.txt";
    string previousNumber = System.IO.File.ReadAllText(path);
    int newNumber;
    if (int.TryParse(previousNumber, out newNumber))
    {
        newNumber++;
        using (FileStream fs = File.Create(path, 1024))
        {
            Byte[] info = new UTF8Encoding(true).GetBytes(newNumber.ToString());
            fs.Write(info, 0, info.Length);
        }
    }

Upvotes: 3

Related Questions