C# 将文件从Windows Mobile设备传输到…任何位置
我似乎找不到解决这个问题的办法。我正试图让WindowsMobile6上的CompactFramework应用程序能够将本地文件系统上的文件移动到另一个系统 以下是我知道的解决方案:C# 将文件从Windows Mobile设备传输到…任何位置,c#,.net,windows,windows-mobile,compact-framework,C#,.net,Windows,Windows Mobile,Compact Framework,我似乎找不到解决这个问题的办法。我正试图让WindowsMobile6上的CompactFramework应用程序能够将本地文件系统上的文件移动到另一个系统 以下是我知道的解决方案: FTP-这是大多数的问题 这些API使用起来非常昂贵 HTTP PUT-据我所知,我不能将匿名PUT与IIS7一起使用,这就是系统正在运行的web服务器。(一个极端的解决方法是使用不同的web服务器放置文件,并让其他系统将其传输到IIS系统) Windows共享-我需要对共享进行身份验证,但我还没有看到通过Win
- FTP-这是大多数的问题 这些API使用起来非常昂贵
- HTTP PUT-据我所知,我不能将匿名PUT与IIS7一起使用,这就是系统正在运行的web服务器。(一个极端的解决方法是使用不同的web服务器放置文件,并让其他系统将其传输到IIS系统)
- Windows共享-我需要对共享进行身份验证,但我还没有看到通过Windows mobile传递此身份验证的方法
- FTP:定义“太贵”。你是指性能、字节开销还是美元成本
- HTTP:IIS7当然支持托管或访问。你可以很容易地使用这两种方法来上传数据
- Windows共享只需要您这样做,但并不十分复杂
如果你能把你是怎么做到的,我会+1这个。你是如何在Mobile6上运行PHP的?我只是编写了一个PHP脚本,就像你编写表单处理程序一样。我通过HTTP请求将数据作为字节传递给PHP脚本,其中一个字段是filedata作为字节。唯一的缺点是POST字段的大小限制,这是由web服务器设置的。IIRC Apache默认为20MB。如果这仍然太模糊,我可以挖掘代码-让我知道!我对PHP一无所知。如果这是C#或者甚至是C,我就能猜出来。我已经有了PHP食谱,但其中的代码对我来说仍然是巫毒。用一些示例代码更新了我的答案。我之前的评论是我在切换到这个方法之前尝试的老方法。我一次传递的数据较少,但对我来说似乎是可靠的。
/*
* Here's the short+sweet about how I'm doing this
* 1) Copy the file from mobile device to web server by querying PHP script with paramaters for each line
* 2) PHP script checks 1) If we got the whole data file 2) If this is a duplicate data file
* 3) If it is a duplicate, or we didn't get the whole thing, it goes away. The mobile
* device will hang on to it's data file in the first case (if it's duplicate it deletes it)
* to be tried again later
* 4) The server will then process the data files using a scheduled task/cron job at an appropriate time
*/
private void process_attempts()
{
Uri CheckUrl = new Uri("http://path/to/php/script?action=check");
WebRequest checkReq = WebRequest.Create(CheckUrl);
try
{
WebResponse CheckResp = checkReq.GetResponse();
CheckResp.Close();
}
catch
{
MessageBox.Show("Error! Connection not available. Please make sure you are online.");
this.Invoke(new Close(closeme));
}
StreamReader dataReader = File.OpenText(datafile);
String line = null;
line = dataReader.ReadLine();
while (line != null)
{
Uri Url = new Uri("http://path/to/php/script?action=process&line=" + line);
WebRequest WebReq = WebRequest.Create(Url);
try
{
WebResponse Resp = WebReq.GetResponse();
Resp.Close();
}
catch
{
MessageBox.Show("Error! Connection not available. Please make sure you are online.");
this.Invoke(new Close(closeme));
return;
}
try
{
process_bar.Invoke(new SetInt(SetBarValue), new object[] { processed });
}
catch { }
process_num.Invoke(new SetString(SetNumValue), new object[] { processed + "/" + attempts });
processed++;
line = dataReader.ReadLine();
}
dataReader.Close();
Uri Url2 = new Uri("http://path/to/php/script?action=finalize&lines=" + attempts);
Boolean finalized = false;
WebRequest WebReq2 = WebRequest.Create(Url2);
try
{
WebResponse Resp = WebReq2.GetResponse();
Resp.Close();
finalized = true;
}
catch
{
MessageBox.Show("Error! Connection not available. Please make sure you are online.");
this.Invoke(new Close(closeme));
finalized = false;
}
MessageBox.Show("Done!");
this.Invoke(new Close(closeme));
}
<?php
//Get the GET'd values from the C#
//The current line being processed
$line = $_GET['line'];
//Which action we are doing
$action = $_GET['action'];
//# of lines in the source file
$totalLines = $_GET['lines'];
//If we are processing the line, open the data file, and append this new line and a newline.
if($action == "process"){
$dataFile = "tempdata/SOME_KIND_OF_UNIQUE_FILENAME.dat";
//open the file
$fh = fopen($dataFile, 'a');
//Write the line, and a newline to the file
fwrite($fh, $line."\r\n");
//Close the file
fclose($fh);
//Exit the script
exit();
}
//If we are done processing the original file from the C# application, make sure the number of lines in the new file matches that in the
//file we are transferring. An expansion of this could be to compare some kind of hash function value of both files...
if($action == "finalize"){
$dataFile = "tempdata/SOME_KIND_OF_UNIQUE_FILENAME.dat";
//Count the number of lines in the new file
$lines = count(file($dataFile));
//If the new file and the old file have the same number of lines...
if($lines == $totalLines){
//File has the matching number of lines, good enough for me over TCP.
//We should move or rename this file.
}else{
//File does NOT have the same number of lines as the source file.
}
exit();
}
if($action == "check"){
//If a file with this unique file name already exists, delete it.
$dataFile = "tempdata/SOME_KIND_OF_UNIQUE_FILENAME.dat";
if(file_exists($dataFile)){
unlink($dataFile);
}
}
?>