Koray Kırdinli

Yazılım ve İş Yaşamı Hakkında Paylaşımlar

Bat ile Scheduled Task Oluşturma

Merhabalar,

bat uzantılı dosyalar bildiğiniz gibi windows komutları çalıştırmamıza yarayan küçük programcıklardır. Bu örneğimizde işyerinde işime yarıyan bir scheduled task yani zamanlanmış görev  örneğini paylaşıyor olacağım.

Bir text dosyasına aşağıdaki scripti kopyalayıp uzantısını da .bat olarak kaydederseniz, dosyaya çift tıkladığınızda zamanlanmış görevi oluşturmuş olacaktır.

schtasks /create /tn “ServisAdı” /tr “c:\xxxxx.exe” /sc DAILY /st 23:00:00 /sd 01/01/2014 /ru KullaniciAdi /rp Sifre

Zamanlanmış Görevi doğru oluşturup oluşturmadığına Bilgisayarıma sağ tıklayıp Manage diyerek Task Scheduler kısmından bakabilirsiniz.

Untitled

 

 

Nisan 18, 2014 Posted by | Donanım, İpucu | , , | Yorum bırakın

C# Ftp sınıfı

Aşağıdaki sınıf ile c# da FTP upload, download, delete gibi işlemleri yapabilirsiniz.

public class FTP
    {
        private string host = null;
        private string user = null;
        private string pass = null;
        private FtpWebRequest ftpRequest = null;
        private FtpWebResponse ftpResponse = null;
        private Stream ftpStream = null;
        private int bufferSize = 2048;

        public FTP(string hostIP, string userName, string password)
        {
            host = hostIP; user = userName; pass = password;
        }

        public void Download(string remoteFile, string localFile)
        {
            try
            {
                ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
                ftpRequest.Credentials = new NetworkCredential(user, pass);
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.KeepAlive = true;
                ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
                ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
                ftpStream = ftpResponse.GetResponseStream();
                FileStream localFileStream = new FileStream(localFile, FileMode.Create);
                byte[] byteBuffer = new byte[bufferSize];
                int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
                try
                {
                    while (bytesRead > 0)
                    {
                        localFileStream.Write(byteBuffer, 0, bytesRead);
                        bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
                    }
                }
                catch (Exception ex) { Console.WriteLine(ex.ToString()); }
                localFileStream.Close();
                ftpStream.Close();
                ftpResponse.Close();
                ftpRequest = null;
            }
            catch (Exception ex)
            {
                LogManager.Log("FTP Download: ",ex);
            }
            return;
        }

        public void Upload(string remoteFile, string localFile)
        {
            try
            {
                ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
                ftpRequest.Credentials = new NetworkCredential(user, pass);
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.KeepAlive = true;
                ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
                ftpStream = ftpRequest.GetRequestStream();
                FileStream localFileStream = new FileStream(localFile, FileMode.Create);
                byte[] byteBuffer = new byte[bufferSize];
                int bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
                try
                {
                    while (bytesSent != 0)
                    {
                        ftpStream.Write(byteBuffer, 0, bytesSent);
                        bytesSent = localFileStream.Read(byteBuffer, 0, bufferSize);
                    }
                }
                catch (Exception ex) { Console.WriteLine(ex.ToString()); }
                localFileStream.Close();
                ftpStream.Close();
                ftpRequest = null;
            }
            catch (Exception ex)
            {
                LogManager.Log("FTP Upload: ", ex);
            }
            return;
        }

        public void Delete(string deleteFile)
        {
            try
            {
                ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + deleteFile);
                ftpRequest.Credentials = new NetworkCredential(user, pass);
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.KeepAlive = true;
                ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
                ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
                ftpResponse.Close();
                ftpRequest = null;
            }
            catch (Exception ex)
            {
                LogManager.Log("FTP Delete: ", ex);
            }
            return;
        }

        public void Rename(string currentFileNameAndPath, string newFileName)
        {
            try
            {
                ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + currentFileNameAndPath);
                ftpRequest.Credentials = new NetworkCredential(user, pass);
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.KeepAlive = true;
                ftpRequest.Method = WebRequestMethods.Ftp.Rename;
                ftpRequest.RenameTo = newFileName;
                ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
                ftpResponse.Close();
                ftpRequest = null;
            }
            catch (Exception ex)
            {
                LogManager.Log("FTP Rename: ", ex);
            }
            return;
        }

        public void CreateDirectory(string newDirectory)
        {
            try
            {
                ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + newDirectory);
                ftpRequest.Credentials = new NetworkCredential(user, pass);
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.KeepAlive = true;
                ftpRequest.Method = WebRequestMethods.Ftp.MakeDirectory;
                ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
                ftpResponse.Close();
                ftpRequest = null;
            }
            catch (Exception ex)
            {
                LogManager.Log("FTP CreateDirectory: ", ex);
            }
            return;
        }

        public string GetFileCreatedDateTime(string fileName)
        {
            try
            {
                ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + fileName);
                ftpRequest.Credentials = new NetworkCredential(user, pass);
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.KeepAlive = true;
                ftpRequest.Method = WebRequestMethods.Ftp.GetDateTimestamp;
                ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
                ftpStream = ftpResponse.GetResponseStream();
                StreamReader ftpReader = new StreamReader(ftpStream);
                string fileInfo = null;
                try
                {
                    fileInfo = ftpReader.ReadToEnd();
                }
                catch (Exception ex)
                {
                    LogManager.Log("FTP GetFileCreatedDateTime1: ", ex);
                }
                ftpReader.Close();
                ftpStream.Close();
                ftpResponse.Close();
                ftpRequest = null;
                return fileInfo;
            }
            catch (Exception ex)
            {
                LogManager.Log("FTP GetFileCreatedDateTime2: ", ex);
            }
            return "";
        }

        public string GetFileSize(string fileName)
        {
            try
            {
                ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + fileName);
                ftpRequest.Credentials = new NetworkCredential(user, pass);
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.KeepAlive = true;
                ftpRequest.Method = WebRequestMethods.Ftp.GetFileSize;
                ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
                ftpStream = ftpResponse.GetResponseStream();
                StreamReader ftpReader = new StreamReader(ftpStream);
                string fileInfo = null;
                try
                {
                    while (ftpReader.Peek() != -1)
                    {
                        fileInfo = ftpReader.ReadToEnd();
                    }
                }
                catch (Exception ex)
                {
                    LogManager.Log("FTP GetFileSize1: ", ex);
                }
                ftpReader.Close();
                ftpStream.Close();
                ftpResponse.Close();
                ftpRequest = null;
                return fileInfo;
            }
            catch (Exception ex)
            {
                LogManager.Log("FTP GetFileSize2: ", ex);
            }
            return "";
        }

        public string[] DirectoryListSimple(string directory)
        {
            try
            {
                ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + directory);
                ftpRequest.Credentials = new NetworkCredential(user, pass);
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.KeepAlive = true;
                ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
                ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
                ftpStream = ftpResponse.GetResponseStream();
                StreamReader ftpReader = new StreamReader(ftpStream);
                string directoryRaw = null;
                try
                {
                    while (ftpReader.Peek() != -1)
                    {
                        directoryRaw += ftpReader.ReadLine() + "|";
                    }
                }
                catch (Exception ex)
                {
                    LogManager.Log("FTP DirectoryListSimple1: ", ex);
                }
                ftpReader.Close();
                ftpStream.Close();
                ftpResponse.Close();
                ftpRequest = null;
                try
                {
                    string[] directoryList = directoryRaw.Split("|".ToCharArray()); return directoryList;
                }
                catch (Exception ex)
                {
                    LogManager.Log("FTP DirectoryListSimple2: ", ex);
                }
            }
            catch (Exception ex)
            {
                LogManager.Log("FTP DirectoryListSimple3: ", ex);
            }
            return new string[] { "" };
        }

        public string[] DirectoryListDetailed(string directory)
        {
            try
            {
                ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + directory);
                ftpRequest.Credentials = new NetworkCredential(user, pass);
                ftpRequest.UseBinary = true;
                ftpRequest.UsePassive = true;
                ftpRequest.KeepAlive = true;
                ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
                ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
                ftpStream = ftpResponse.GetResponseStream();
                StreamReader ftpReader = new StreamReader(ftpStream);
                string directoryRaw = null;
                try
                {
                    while (ftpReader.Peek() != -1)
                    {
                        directoryRaw += ftpReader.ReadLine() + "|";
                    }
                }
                catch (Exception ex)
                {
                    LogManager.Log("FTP DirectoryListDetailed1: ", ex);
                }
                /* Resource Cleanup */
                ftpReader.Close();
                ftpStream.Close();
                ftpResponse.Close();
                ftpRequest = null;
                /* Return the Directory Listing as a string Array by Parsing 'directoryRaw' with the Delimiter you Append (I use | in This Example) */
                try
                {
                    string[] directoryList = directoryRaw.Split("|".ToCharArray());
                    return directoryList;
                }
                catch (Exception ex)
                {
                    LogManager.Log("FTP DirectoryListDetailed2: ", ex);
                }
            }
            catch (Exception ex)
            {
                LogManager.Log("FTP DirectoryListDetailed3: ", ex);
            }
            return new string[] { "" };
        }
    }

Nisan 2, 2014 Posted by | C#, Yazılım | , , , , | 1 Yorum

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Bazı C# projelerinde unmanaged kodlar kullanmak durumunda kalabiliyoruz. Managed kod demek hafıza yönetimi(memory management) yonetilen demek oluyor.Oluşturduğumuz değişkenlerin yok edilmesi işini kullandığımız sistem(FrameWork, Programlama dili.) yapıyor demektir. Bir değişkeni tanımladığımızda bir süre kullanılmadığında bunu otomatik olarak GAC(Garbage Collector) tarafından hafızadan silinir. Bu yönetilen koda bir örnektir. “Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt” hatasını da unmanaged kodu debug modda çalıştırdığımızda almıştım. Biraz araştırdım ve bunu Visual Studio da basit bir ayar yaparak geçebileceğimi öğrendim. Tools menu -> Options -> Debugging -> General  menüsüne girerek “Suppress JIT optimization on module load” check’ini kaldırıyoruz.   suppress

Nisan 1, 2014 Posted by | C#, Yazılım | , , | Yorum bırakın