c# FileLock – If you need to lock a file for temporary purpose.

Hi Everyone ,

FileLock is vey simple tool that locking defined file in given path. This tool can be used acting as AV programs , troublesoothing or reproduce error when a file is locked scenarios .
You can download binaries or source from :

http://spstools.codeplex.com/releases/view/80978

FileLock.cs
  1. class Program
  2.  {
  3.      static void Main(string[] args)
  4.      {
  5.          if (args.Length != 1)
  6.          {
  7.              Console.WriteLine(“FileLock v1.0”);
  8.              Console.WriteLine(“Usage: FileLock <path>”);
  9.          }
  10.          else
  11.          {
  12.              Console.WriteLine(args[0] + ” is Locked” + Environment.NewLine + “Press any key to relase!”);
  13.              using (FileStream fs = new FileStream(args[0], FileMode.Open, FileAccess.Write, FileShare.Write))
  14.              {
  15.                  fs.Lock(0, fs.Length);
  16.                  Console.ReadKey();
  17.                  fs.Unlock(0, fs.Length);
  18.              }
  19.          }
  20.      }
  21.  }
Advertisements

c# Multiple WebRequest and WebResponse timeout problems.

Hi Everyone ,

In this tips and tricks article i wanna talk about when we use Multiple WebRequest object for interacting some urls getting timeout errors even if remote server not busy.
this is usually caused by not dispose some open WebResponse objects.You can also use “using” statement for disposing unnecessery objects .

For good example :

WebRequest wrGETURL;
wrGETURL = WebRequest.Create(sURL);
// wrGETURL.Proxy = myProxy;

durationTime = DateTime.Now;

using
(HttpWebResponse response = (HttpWebResponse)wrGETURL.GetResponse())
{
status = response.StatusCode.ToString();
TimeSpan ts = DateTime.Now – durationTime;
duration = ts.TotalMilliseconds.ToString();
}

Getting installed powershell version

Ok. you have powershell but you dont know what is the version of it ?
Just write “$host.version” than press enter 🙂

Optional parameters for functions in c#

C# (before .net 4.0) does not support optional method arguments. However, there may be times when you are using components that were created in a language that supports optional arguments, such as legacy COM components or components created with Microsoft Visual Basic .NET.

class Example {    public void Test()     {        object missing = Type.Missing;       object urlblog = "http://blog.bugrapostaci.com";        VBTestService yourObject = new VBTestService();         // first parameter is url and other 3 are optional in definition of OptionalParameterFuction coded VB        yourObject.OptionalParameterFuction(ref urlblog, ref missing, ref missing, ref missing, ref missing);     } }

Happy Codding…

c# getting day name for specific culture

Here is a small example for getting day name of a date by specific culture:

 

DateTime startDate = DateTime.Now;

DateTime endDate = DateTime.Now.AddDays(2);

 

CultureInfo c = new CultureInfo("tr-TR");

string startDayName = c.DateTimeFormat.DayNames[(int)startDate.DayOfWeek];

string endDayName = c.DateTimeFormat.DayNames[(int)endDate.DayOfWeek];

 

 

Happy Tips.

The given expression is never of the provided warning.

When you try to getting type of Generic variable “T” you get this warning.Here is the sample :

class CauseWarning {     public void make<T>(T value)     {     if (typeof(T) is Test)         Console.Write("Something");     } } public class Test { }

Here is the solution:


class CauseWarning {     public void make<T>(T value)     {     if (typeof(T).IsAssignableFrom(typeof( Test)))         Console.Write("Something");     } } public class Test { }

Type.IsAssignableFrom Method Determines whether an instance of the current Type can be assigned from an instance of the specified Type.true if the c parameter and the current Type represent the same type, or if the current Type is in the inheritance hierarchy of c, or if the current Type is an interface that c supports. false if none of these conditions are the case, or if c is a null reference.

which process lock my dll

Sometimes you want to know whats happinging at the background or wonder which app or process using specific dll. Or may be like me when try to copy a dll getting error “DLL is used by another process” . So what now ?

Process Explorer is one of great tool for this.

Download:

http://download.sysinternals.com/Files/ProcessExplorer.zip