Introduction:
I am very happy to write an article about FTP server access from windows phone programming using c#.net, there are no direct APIs available for leveraging FTP services in a Windows phone app. From an enterprise perspective, this makes it rather difficult for employees to access files over their phones. Fortunately we have socket support for windows phone to communicate with a multitude of different services, making users feel more connected and available than ever.This article can explained about below topics:
1. What is FTP and Why?
2. How to Setup Ftp Server in Windows 8.0/8.1/10 OS system?
3. How to Connect FTP Server from WindowsPhone?
4. Upload files to Ftp Server
Requirements:
- This sample is targeted for windowsphone silverlight 8.1 OS,So make sure you’ve downloaded and installed the Windows Phone 8.1 SDK. For more information, see Get the SDK.
- I assumes that you’re going to test your app on the Windows Phone emulator. If you want to test your app on a phone, you have to take some additional steps. For more info, see Register your Windows Phone device for development.
- This post assumes you’re using Microsoft Visual Studio Express 2013 for Windows or Later.
Description:
1. What is FTP and Why?
- The File Transfer Protocol (FTP) is a standard network protocol used to transfer computer files from one host to another host over a TCP-based network, such as the Internet.
- FTP is built on a client-server architecture and uses separate control and data connections between the client and the server.
- FTP users may authenticate themselves with a clear-text sign-in protocol, normally in the form of a username and password, but can connect anonymously if the server is configured to allow it.
- For secure transmission that protects the username and password, and encrypts the content, FTP is often secured with SSL/TLS (FTPS). SSH File Transfer Protocol (SFTP) is sometimes also used instead, but is technologically different.
This section is not applicable for windows app developer, but it is better to learn understand the FTP communication by creating server in your machine.
2.1. So open Programs and Features in Control panel. Click on ‘Turn Windows features on or off’ as shown below.
If IIS was not installed earlier on particular Windows 8 or 8.1 computer, you need to install other features of IIS too ( as shown by arrow marks). See the below screenshot for the actual requirements to run FTP server on Windows 8/8.1 (All features which are ticked need to be installed).
Note: You must be restart your machine for above step to make changes in your .
2.2. After installation is completed, search and open ‘Internet Information Services (IIS) Manager’ in Administrator mode from start menu.
2.3. Give a name for FTP site and browse the local folder which you need to give access to others through server.(I already created a folder called 'FTPShare' on C drive before reaching this step.)
2.4. In next screen you need to select the local computer’s IP address from drop down box. I hope you have already set up static IP for the computer.
Under SSL option, select No SSL to make the connection without SSL certificate. In production environment for professional FTP server setup, you may need to enable SSL which requires a certificate.
Now its time to Set Up FTP Access Permission on Windows 8 or 8.1 or Windows 10
2.5. In next screen you can set the permission for users to access the FTP site. Here you need to decide how others will be accessing the FTP share and who will be having Read-only or Read & Write access.
Let’s assume this scenario, you want specific users to have read and write access, so obviously they must type an user name and password for it. Other users can access the FTP site without any username or password to view the content only, it’s called anonymous users access.
You will find several ways to do this, but here is the simple way which worked for me.
Firstly open start menu and type 'lusrmgr.msc', and create a user on Windows 8 or Windows 10 local computer (if you are not using active directory environment) .
and add the users who will be having read and write access on FTP site. In this example I have created ‘ftpusers’ group and added required users inside it.
2.6. Now, let’s continue the FTP site settings. In next screen to give the permission, select ‘Basic’ which will be prompting for user name and password, select Specified roles or groups option from drop down and type the correct group name. Set Read and Write permission for the group.
Press Finish to complete the setup.
Check the Firewall ! It should allow FTP traffic.
We are almost done and completed the all necessary steps. Now, you need to either disable the firewall or allow FTP inbound and outbound traffic on Windows 8 or 8.1 computer. I hope you can do it easily.
3. How to Connect FTP Server from WindowsPhone?
Step 1:
1. Open Microsoft Visual Studio Express 2013 for Windows (or) later.
2. Create new silverlight project using the "Blank App" template available under Visual C# -> Store Apps -> Windows Phone Apps. (for example project name :WindowsPhoneFTP)
Step 2:
Recently i made one binary file for FTP on windows phone programming, and temporarily now i can directly add ftp binary file to reference folder to access FTP server using windows phone 8.0 silverlight C#.
Step 3:
Now add below helper class for uploading files to FTP server.
- public class FtpClientClass
- {
- private static string m_Host = "Please ENTER your Server Name/IP";
- private static string FtpUserName = "Server UserName";
- private static string FtpPwd = "Server Password";
- private static string FtpPort = "21";
- private readonly NetworkCredential m_Credentials = new NetworkCredential(FtpUserName, FtpPwd);
- private const string PathToSet = "/";
- private const string FolderToCreate = "";//"WindowsPhone";
- public FtpClient CreateFtpClient()
- {
- var ftpClient = new FtpClient();
- ftpClient.Credentials = m_Credentials;
- ftpClient.HostName = new HostName(m_Host);
- ftpClient.ServiceName = FtpPort;
- return ftpClient;
- }
- //Upload file to Ftp Server
- public async Task<string> UploadFileAsync(string DataFields, string fileUsername)
- {
- try
- {
- if (fileUsername=="")
- {
- fileUsername = "NoUserName";
- }
- System.Diagnostics.Debug.WriteLine("-----------------------------------");
- System.Diagnostics.Debug.WriteLine("TestCreateFileAsync");
- System.Diagnostics.Debug.WriteLine("-----------------------------------");
- var ftpClient = CreateFtpClient();
- await ftpClient.ConnectAsync();
- await ftpClient.CreateDirectoryAsync(PathToSet + FolderToCreate);
- string testFilePath = PathToSet + fileUsername + ".txt";
- using (var stream = await ftpClient.OpenWriteAsync(testFilePath))
- {
- var bytes = Encoding.UTF8.GetBytes(DataFields);
- await stream.WriteAsync(bytes.AsBuffer());
- await stream.FlushAsync();
- }
- //await ftpClient.DisconnectAsync();
- return "Success";
- }
- catch(Exception ex)
- {
- return ex.Message;
- }
- }
- }
Now we can use above helper class like below:
And add below xaml code in MainPage.xaml
- <!--LayoutRoot is the root grid where all page content is placed-->
- <Grid x:Name="LayoutRoot" Background="White">
- <Grid VerticalAlignment="Center">
- <Grid.RowDefinitions>
- <RowDefinition Height="Auto"/>
- <RowDefinition Height="*"/>
- </Grid.RowDefinitions>
- <Button Name="BtnSubmit" VerticalAlignment="Center" Content="Upload to FTP" Background="#FF58E277" Click="BtnSubmit_Click"/>
- <TextBlock Name="TbckUploadStatus" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Bottom" Foreground="Black"/>
- </Grid>
- </Grid>
Output:
If you want to access SFTP server from windows phone silverlight, you can read it on next article from here.
FeedBack Note:
Please share your thoughts,what you think about this post,Is this post really helpful for you?I always welcome if you drop comments on this post and it would be impressive.
Follow me always at @Subramanyam_B
Hi,
ReplyDeleteI am getting below error
Metadata file .. Upload files to Ftp Server ..\WindowsPhoneFTP\Bin\Debug\WinPhoneFtp.FtpService.dll' could not be found
Hi bro, I download the library from "Code_Gupta.WPFTP" and fix the issue.
ReplyDeleteHello Sir, Good Day! Can you show me how you do it in step 2? thanks
ReplyDeleteCould you please download source code and find "WinPhoneFtp.FtpService.dll" inside Bin\Debug folder. Once you find, please add it manually to your project reference.
Delete