About xamarin.forms Android Create file with Write file (FTP)

Source: Internet
Author: User
Tags ftp file

Now read and write files in the original Android ecosystem should be a cinch. But how is xamarin.forms supposed to use it?

1 //gets the name of the file that contains the suffix2 stringStrName =Path.getfilename (strpath);3strpath ="ftp://"+ Builder. UserName +":"+ Builder. Password +"@"+ Builder. Host +strpath;4Builder. Allpath =strpath;5 //Global::android.os.environment.externalstoragedirectory.absolutepath: Get the root directory of Android6 //Environment.getfolderpath (Environment.SpecialFolder.ApplicationData):: Get Android Data Directory7 varPath =Global:: Android.OS.Environment.ExternalStorageDirectory.AbsolutePath +Environment.getfolderpath (Environment.SpecialFolder.ApplicationData);8 //Create a file9System.IO.Directory.CreateDirectory (path);

But I did not find my file on my phone for half a day. The original is no permission. How to add permission to it! There is a androidmanifest.xml file under the properties file in the Android project. In <application android:label= "Cardionnet2.android" ></application> under Add

1 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />2 <uses-permission android:name="Android.permission.WRITE_EXTERNAL_STORAGE  " />

These two sentences are good.

Then how to download files with FTP.

1FtpWebRequest reqftp =NULL;//FTP Request2FileStream Savestream =NULL;//ftp file Stream3Stream FtpStream =NULL;//FTP Transport Stream4FtpWebResponse response =NULL;//FTP Response5             Try6             {7                 varSSS =NewUri (builder. Path);8  9                 //create a file to saveTenSavestream =NewFileStream (Savepath, filemode.create); One                 //Download File Settings AReqftp = (ftpwebrequest) ftpwebrequest.create (NewUri (builder. Allpath));  -Reqftp.credentials =NewNetworkCredential (builder. UserName, builder. Password); -Reqftp.method =WebRequestMethods.Ftp.DownloadFile; theReqftp.usebinary =true; -                 //Start Request -Response =(FtpWebResponse) reqftp.getresponse (); -                 //Start Download +FtpStream =Response. GetResponseStream (); -                 //Save the download to a file stream +                 intBufferSize =65535; A                 intReadcount; at                 byte[] buffer =New byte[buffersize]; -Readcount = ftpstream.read (buffer,0, buffersize); -                  while(Readcount >0) -                 { -Savestream.write (Buffer,0, readcount); -Readcount = ftpstream.read (buffer,0, buffersize); in                 } -  } to             Catch(WebException webEx) +             { -                 ThrowwebEx; the             } *             Catch(Exception ex) $             {Panax Notoginseng                 Throwex; -             } the             finally +             { A                 //Freeing Resources the                 if(FtpStream! =NULL) Ftpstream.close (); +                 if(Savestream! =NULL) Savestream.close (); -                 if(Response! =NULL) Response. Close (); $}

The key to the FTP download file for Trend Xamarin and C # is how to build files on your phone.

About xamarin.forms Android Create file with Write file (FTP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.