Monday, June 25, 2012

VB^ Code for File OPerations

Public Function DeleteFile(strPath As String) As String
On Error GoTo Error
If FSO.FileExists(strPath) Then
FSO.DeleteFile (strPath)
End If
Error:
MsgBox ("DeleteFile : " & Err.Description)
End Function
Public Function MoveFile(ByVal strSrcPath As String, ByVal strDestPath As String) As String
On Error GoTo Error
If FSO.FileExists(strSrcPath) Then
FSO.MoveFile strSrcPath, strDestPath

End If
Error:
MsgBox ("MoveFile : " & Err.Description)
End Function
Public Function ReadFile(ByVal strFilePath As String) As String
On Error GoTo Error
Dim strOutput As String
If FSO.FileExists(strFilePath) = False Then
Exit Function
End If
Dim nFileNum As Integer, sText As String, sNextLine As String, lLineCount As Long
nFileNum = FreeFile
Open strFilePath For Input As nFileNum
lLineCount = 1
' Read the contents of the file
Do While Not EOF(nFileNum)
   Line Input #nFileNum, sNextLine
   'do something with it
   'add line numbers to it, in this case!
   sNextLine = sNextLine & vbCrLf
   sText = sText & sNextLine
Loop
strOutput = sText

Error:
MsgBox ("ReadFile : " & Err.Description & strOutput)
End Function
Public Function WriteFile(strFilePath As String, strToWriteFilePath As String) As String
On Error GoTo Error
Dim strOutput As String
If fso.FileExists(strFilePath) = False Then
Exit Function
End If
Dim nFileNum As Integer, sText As String, sNextLine As String, lLineCount As Long
nFileNum = FreeFile
Open strFilePath For Input As nFileNum
lLineCount = 1
' Read the contents of the file
Do While Not EOF(nFileNum)
   Line Input #nFileNum, sNextLine
  
   sNextLine = sNextLine & vbCrLf
   sText = sText & sNextLine
Loop
strOutput = sText
Dim newFIle
Set fso = CreateObject("Scripting.FileSystemObject")
 Set newFIle = fso.CreateTextFile(strToWriteFilePath, True, False)
newFIle.Write strOutput


Error:
MsgBox ("WriteFile : " & Err.Description & strOutput)
End Function

Sunday, June 10, 2012

File Upload using FtpWebRequest in .Net C#

Namespace used:
  1. using System.Net;   
  2. using System.IO;  

Sample Code:
  1. public void ftpfile(string ftpfilepath, string inputfilepath)   
  2. {   
  3.     string ftphost = "127.0.0.1";   
  4.     //here correct hostname or IP of the ftp server to be given   
  5.   
  6.     string ftpfullpath = "ftp://" + ftphost + ftpfilepath;   
  7.     FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);   
  8.     ftp.Credentials = new NetworkCredential("userid""password");   
  9.     //userid and password for the ftp server to given   
  10.   
  11.     ftp.KeepAlive = true;   
  12.     ftp.UseBinary = true;   
  13.     ftp.Method = WebRequestMethods.Ftp.UploadFile;   
  14.     FileStream fs = File.OpenRead(inputfilepath);   
  15.     byte[] buffer = new byte[fs.Length];   
  16.     fs.Read(buffer, 0, buffer.Length);   
  17.     fs.Close();   
  18.     Stream ftpstream = ftp.GetRequestStream();   
  19.     ftpstream.Write(buffer, 0, buffer.Length);   
  20.     ftpstream.Close();   
  21. }  

Function can be used as
  1. ftpfile(@"/testfolder/testfile.xml", @"c:\testfile.xml");  

Reference :http://blog.logiclabz.com/c/ftp-file-upload-using-ftpwebrequest-in-net-c.aspx


Code to check Directory exists in ftp or not

private void btnCheck_Click(object sender, EventArgs e)
        {
            bool result = FtpDirectoryExists("ftp://micro123/First", "anonymous", "anonymous");
            MessageBox.Show("Folder"+result);
        }
        public bool FtpDirectoryExists(string directoryPath, string ftpUser, string ftpPassword)
        {
            bool IsExists = true;
            try
            {
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(directoryPath);
                request.Credentials = new NetworkCredential(ftpUser, ftpPassword);
                request.Method = WebRequestMethods.Ftp.PrintWorkingDirectory;

                FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                IsExists = false;
            }
            return IsExists;
        }