【问题标题】:Xamarin.iOS: Saving files to windows shared folderXamarin.iOS:将文件保存到 Windows 共享文件夹
【发布时间】:2015-12-22 07:02:19
【问题描述】:

我有一个使用 Xamarin 编写的 iOS 应用程序。有没有办法将一些文件(在我的情况下为图像)保存到 Windows PC 上的共享文件夹中? iOS设备和PC在同一个WiFi网络中,所以我可以通过IP地址访问PC机。但是如何将文件保存到该共享文件夹? 如果可能的话,我将不胜感激一些代码示例。

【问题讨论】:

    标签: ios xamarin xamarin.ios


    【解决方案1】:

    你试试SharpCifs.Std?
    NuGet 包是here.

    像这样在 Xamarin.iOS 上驱动 SMB。

    var file = new SmbFile("smb://UserName:Password@ServerName/ShareName/Folder/NewFileName.txt"));
    file.CreateNewFile();
    var writeStream = file.GetOutputStream();
    writeStream.Write(Encoding.UTF8.GetBytes("Hello!"));
    

    【讨论】:

      【解决方案2】:

      在我的 Xamarin 应用程序(在 iPad Pro 12.9 上)中,我通过在我的 Windows10 机器上激活 FTP 来解决这个问题(请参阅 Google 如何执行此操作),然后通过 FTP 将图像上传到 Windows 文件夹:

      private void UploadImage(UIImage image, string fileName)
      {
          try
          {
              string ftpPath = "ftp://192.168.47.79/"; // IP of my Windows10 pc
              string ftpTargetFilePath = $"{ftpPath}{fileName}";
              var credentials = new NetworkCredential("ftpuser", "ftp");
      
              FtpWebRequest request = (FtpWebRequest) WebRequest.Create(ftpTargetFilePath);
              request.Credentials = credentials;
              request.Method = WebRequestMethods.Ftp.UploadFile;
              request.UsePassive = false; // in my case I needed passive mode to be deactivated
              
              using (Stream imageStream = image.AsPNG().AsStream())
              {
                  using (Stream ftpStream = request.GetRequestStream())
                  {
                      imageStream.CopyTo(ftpStream);
                  }
              }
          }
          catch (Exception ex)
          {
              Debug.WriteLine($"Uploading Image: Failed! ({ex.Message})");
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-11-06
        • 1970-01-01
        • 2013-07-05
        • 2012-12-25
        • 1970-01-01
        • 1970-01-01
        • 2018-10-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多