Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用c将文件上传到metro应用程序中的ftp服务器#_C#_Windows Store Apps - Fatal编程技术网

C# 如何使用c将文件上传到metro应用程序中的ftp服务器#

C# 如何使用c将文件上传到metro应用程序中的ftp服务器#,c#,windows-store-apps,C#,Windows Store Apps,我想用C#将文件上传到metro应用程序中的FTP服务器。我试过这个,但没用 public static async Task<bool> UpLoad(string localsubfolders, string ftpURIInfo, string Username, string Password, string filename) { bool Successful = false; try {

我想用C#将文件上传到metro应用程序中的FTP服务器。我试过这个,但没用

public static async Task<bool> UpLoad(string localsubfolders, string ftpURIInfo, string Username, string Password, string filename)
    {


        bool Successful = false;
        try
        {


            BackgroundUploader uploader = new BackgroundUploader();

            StorageFolder storageFolder =  Windows.Storage.ApplicationData.Current.LocalFolder;
            StorageFile sampleFile =  await storageFolder.GetFileAsync("wp_ss_20150313_0001.png");



            Uri urlWithCredential;
            bool Success = Uri.TryCreate(ftpURIInfo + "" + filename, UriKind.Absolute, out urlWithCredential);

            if (!string.IsNullOrEmpty(Username.Trim()) &&
               !string.IsNullOrEmpty(Password.Trim()))
            {
                urlWithCredential = new Uri(urlWithCredential.ToString().ToLower().Replace(@"ftp://",
                    string.Format(@"ftp://{0}:{1}@",
                    Username,
                    Password)));
            }

            UploadFile(urlWithCredential, sampleFile);

        }
        catch (Exception)
        {
            throw;
        }
        return Successful;
    }



    public static async Task UploadFile(Uri destination, StorageFile targetFile)
    {
        var request = WebRequest.Create(destination);
        request.Credentials = Credentials;
        request.Method = "STOR";
        try
        {
            using (var requestStream = (await request.GetRequestStreamAsync()))
            using (var stream = await targetFile.OpenStreamForReadAsync())
            {
                stream.CopyTo(requestStream);
            }
        }
        catch { }
    }
我在这里创建的目的是将小的.png文件上传到服务器,但它不起作用

public static async Task<bool> UpLoad(string localsubfolders, string ftpURIInfo, string Username, string Password, string filename)
    {


        bool Successful = false;
        try
        {


            BackgroundUploader uploader = new BackgroundUploader();

            StorageFolder storageFolder =  Windows.Storage.ApplicationData.Current.LocalFolder;
            StorageFile sampleFile =  await storageFolder.GetFileAsync("wp_ss_20150313_0001.png");



            Uri urlWithCredential;
            bool Success = Uri.TryCreate(ftpURIInfo + "" + filename, UriKind.Absolute, out urlWithCredential);

            if (!string.IsNullOrEmpty(Username.Trim()) &&
               !string.IsNullOrEmpty(Password.Trim()))
            {
                urlWithCredential = new Uri(urlWithCredential.ToString().ToLower().Replace(@"ftp://",
                    string.Format(@"ftp://{0}:{1}@",
                    Username,
                    Password)));
            }

            UploadFile(urlWithCredential, sampleFile);

        }
        catch (Exception)
        {
            throw;
        }
        return Successful;
    }



    public static async Task UploadFile(Uri destination, StorageFile targetFile)
    {
        var request = WebRequest.Create(destination);
        request.Credentials = Credentials;
        request.Method = "STOR";
        try
        {
            using (var requestStream = (await request.GetRequestStreamAsync()))
            using (var stream = await targetFile.OpenStreamForReadAsync())
            {
                stream.CopyTo(requestStream);
            }
        }
        catch { }
    }
公共静态异步任务上载(字符串localsubfolders、字符串ftpURIInfo、字符串用户名、字符串密码、字符串文件名)
{
bool成功=错误;
尝试
{
BackgroundUploader uploader=新的BackgroundUploader();
StorageFolder StorageFolder=Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile sampleFile=等待storageFolder.GetFileAsync(“wp_ss_20150313_0001.png”);
Uri urlWithCredential;
bool Success=Uri.TryCreate(ftpURIInfo+“”+文件名,UriKind.Absolute,out urlWithCredential);
如果(!string.IsNullOrEmpty(Username.Trim())&&
!string.IsNullOrEmpty(Password.Trim()))
{
urlWithCredential=新Uri(urlWithCredential.ToString().ToLower().Replace(@“ftp://”),
string.Format(@“ftp://{0}:{1}@),
用户名,
密码),;
}
上传文件(urlWithCredential、sampleFile);
}
捕获(例外)
{
投
}
成功回归;
}
公共静态异步任务上载文件(Uri目标、StorageFile目标文件)
{
var request=WebRequest.Create(目的地);
请求.凭据=凭据;
request.Method=“STOR”;
尝试
{
使用(var requestStream=(wait request.GetRequestStreamAsync())
使用(var stream=await targetFile.OpenStreamForReadAsync())
{
CopyTo(requestStream);
}
}
捕获{}
}

我找到了这个问题的解决方案

以下代码工作正常

public static async Task<bool> SmallUpload1(string ftpURIInfo, string filename, string username, string password)
    {

        string serverUrl;
        Uri serverUri = null;
        //StorageFolder localFolderArea;
        NetworkCredential credential;
        bool Successful = false;

        try
        {

            StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
            StorageFile targetFile = await storageFolder.GetFileAsync("wp_ss_20150313_0001.png");





            Uri.TryCreate(ftpURIInfo, UriKind.Absolute, out serverUri);
            serverUrl = serverUri.ToString();
            credential = new System.Net.NetworkCredential(username.Trim(),
                password.Trim());

            WebRequest request = WebRequest.Create(serverUrl + "/" + filename);
            request.Credentials = credential;

            request.Proxy = WebRequest.DefaultWebProxy;

            //STOR is for ftp: // POST is for http:// web sites
            request.Method = "STOR";

            byte[] buffer = Encoding.UTF8.GetBytes(UploadLine);




            using (Stream requestStream = await request.GetRequestStreamAsync())
            {
                using (var stream = await targetFile.OpenStreamForReadAsync())
                {
                    stream.CopyTo(requestStream);
                }
            }

            Successful = true;

        }
        catch (Exception)
        {

            throw;
        }
        return Successful;

    } 
公共静态异步任务SmallUpload1(字符串ftpURIInfo、字符串文件名、字符串用户名、字符串密码)
{
字符串serverUrl;
uriserveruri=null;
//StorageFolderLocalFolderArea;
网络证书;
bool成功=错误;
尝试
{
StorageFolder StorageFolder=Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile targetFile=await storageFolder.GetFileAsync(“wp_ss_20150313_0001.png”);
TryCreate(ftpURIInfo、UriKind.Absolute、out-serverUri);
serverUrl=serverUri.ToString();
凭证=新系统.Net.NetworkCredential(username.Trim(),
password.Trim());
WebRequest=WebRequest.Create(serverUrl+“/”+文件名);
请求。凭证=凭证;
request.Proxy=WebRequest.DefaultWebProxy;
//STOR代表ftp://POST代表http://网站
request.Method=“STOR”;
byte[]buffer=Encoding.UTF8.GetBytes(上传线);
使用(Stream requestStream=await request.GetRequestStreamAsync())
{
使用(var stream=await targetFile.OpenStreamForReadAsync())
{
CopyTo(requestStream);
}
}
成功=正确;
}
捕获(例外)
{
投
}
成功回归;
} 

请指定
它不工作