Sunday, 27 May 2018

How to Open aspx page to new tab using C#



ScriptManager.RegisterStartupScript(this, this.GetType(), "OpenWindow","window.open('YourPage.aspx','_blank');", true);

Friday, 25 May 2018

How to run exe file using C#

1. Open Visual Studio and create a new website
2. Add namespace using System.Diagnostics;
3. On Button Click event write below code

         var Path = "C:\\Notepad.exe";

         Process.Start(Path);

4. Run Code.

Thursday, 24 May 2018

Upload file on FTP using C#


1. Open Visual Studio and create a new website

2. Add namespaces like using System.IO; and using System.Net;
3. On Button Click event write below code

string filename = Path.GetFileName(localFile);

                string ftpPath = ftpUrl;
                FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpPath);
                ftp.Credentials = new NetworkCredential(Username, Password);

ftp.UsePassive = true;

                ftp.KeepAlive = true;
                ftp.UseBinary = true;
                ftp.Method = WebRequestMethods.Ftp.UploadFile;
             
                FileStream fs = File.OpenRead(localFile);
                byte[] buffer = new byte[fs.Length];
                fs.Read(buffer, 0, buffer.Length);
                fs.Close();

                Stream ftpstream = ftp.GetRequestStream();

                ftpstream.Write(buffer, 0, buffer.Length);
                ftpstream.Close();

4.Run project and upload file.

Friday, 18 May 2018

How to Deploy ASP. Net Websites on IIS



Step 1: From Visual Studio, publish your Web application.
Step 2: From RUN -> inetmgr -> OK
Step 3: Add Application Pool for your Website Select Version V4 for Application Pool. And Select Pipeline Mode to Integrated
Step 4: From Sites ->Right click -> Add Website 

Step 5: Configure the Website
Give your Site name
Select Application Pool you created
Set Physical Path of your website
Set Bindings and Port of Website
Set Host Name and then Click OK



Thursday, 17 May 2018

Download File from SFTP using C#

1. Open Visual studio and create a new website
2. Right Click to your solution and go to Manage Nuget Packages
3. Search for SSH.NET and install it
4. Include namespaces like using Renci.SshNet; and using System.IO;
5. On your Button Click Event write below code

          string port = SFTP Port;

          string host = SFTP Host;
          string username = SFTP Username;
          string password = SFTP Password;
          string remoteDirectory = SFTP Folder from where the file will be downloaded;

          string localDirectory = Local Directory Path;

                if (!File.Exists(Path.GetDirectoryName(localDirectory)))
                    Directory.CreateDirectory(Path.GetDirectoryName(localDirectory));

                using (SftpClient sftp = new SftpClient(host, Convert.ToInt32(port), username, password))

                {
                    sftp.Connect();
                    var files = sftp.ListDirectory(remoteDirectory);
                    foreach (var file in files)
                    {
                        string remoteFileName = file.Name;
                        if (file.Name.Contains("."))
                        {
                            using (Stream file1 = File.OpenWrite(localDirectory + "\\" + remoteFileName))
                            {
                                sftp.DownloadFile(remoteDirectory + "\\" + remoteFileName, file1);
                            }
                        }
                    }
                }
6. Run Your Project

Upload file on SFTP using C#

1. Open Visual studio and create a new website
2. Right Click to your solution and go to Manage Nuget Packages
3. Search for SSH.NET and install it
4. Include namespaces like using Renci.SshNet; and using System.IO;
5. On your Button Click Event write below code

          string port = SFTP Port;

          string host = SFTP Host;
          string username = SFTP Username;
          string password = SFTP Password;
          string destinationFolder = SFTP Folder where the file will be uploaded;

          using (SftpClient client = new SftpClient(host, Convert.ToInt32(port), username, password))

                   {
                        client.Connect();
                        client.ChangeDirectory(destinationFolder);
                        using (FileStream fs = new FileStream(path, FileMode.Open))
                            {
                                    client.BufferSize = 4 * 1024;
                                    client.UploadFile(fs, Path.GetFileName(path));
                            }
                    }
6. Run Your Project

How to Create Proxy Class from wsdl.exe

1.  Download wsdl.exe
2.  Place it on your local computer
3.  Open Command Prompt
4.  Run command
     C:\Project>wsdl.exe [Path to your WSDL file]
5.  You will see the .cs file in the C:\Project Folder

SQL server tricky questions