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)