我找到了一个可行的解决方案,使用 Barta Tamás 提到的文件传输协议。但是,我从 Michael Todd 那里了解到这并不安全,所以我不会在自己的应用程序中使用它,但也许它可以对其他人有所帮助。
我在这里找到了有关使用 FTP 上传文件的信息: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();
}
阅读问题的评论,如何更好地做到这一点,而不是使用 FTP。如果您的服务器上有重要文件,则不建议使用我的解决方案。