"Your time is limited, so don't waste it living someone else's life"

Category Archives: C#

Copy file in LAN network

The mechanism of copying files in a local network is the same when you query data from a database. We get 3 basic steps:

  • Map the network drive – as open connection
  • Copy file – as query data
  • Reject the above drive – as close connection

The first and the last steps are done with net.exe windows program while the second step works with the normal File.Copy functionality.

First step – Map the network
Read more of this post

Start process without console window

To start a process and get out it’s output stream, you can use ProcessStartInfo like:

var startInfo = new ProcessStartInfo("net.exe", "use \\")
                    UseShellExecute = false,
                    CreateNoWindow = true
var process = Process.Start(startInfo);

How to get relative path in C# (absolute)

As I introduced in the past post “Cách lấy đường dẫn tương đối“, we can get the relative path in Java. But, I could not use this way for such a case in Csharp. So, I introduce a new way to make Csharp can find the specified files as the same way you used in Java. That means you just only enter the path to file like a relative path.
For instance, you have the directory as following:
You now want to get an image in folder Images, and your execution file is in Debug directory. In fact, this is get the absolute path to the file you want. You create a method as bellow: Read more of this post

How to rotate a rectangle in WPF

When you want to rotate a rectangle in WPF.
You can use the RenderTranform attribute of Rectangle.
I give you an simple example on how to rotate a rectangle as following
First I have a rectangle object named rect. You initiate this object like:

Rectangle rect = new Rectangle();
rect.Fill = new SolidColorBrush(Colors.Blue);
rect.Width = 100;
rect.Height = 100;
Canvas.SetLeft(rect, 50);
Canvas.SetTop(rect, 50);

Read more of this post

How to draw something on canvas in C#

1. Create a canvas frame
In design screen of WPF, you create a canvas frame like the image below:
Don’d forget to name canvas frame to Name=”canvas”
Now, we practice to draw a line and a ellipse on the above canvas
2. Draw a line
The ways to draw shapes on canvas are similar to each other.

  • First, you have to define an instance of that shape.
  • Second, you initiate some obligation attributes for that object, for detail they are coordination, width, height, and color.
  • Final, you import that shape into canvas children list.

Let’s begin with a Line
Read more of this post

How to create a Timer in C#

In this post, I introduce how to create a timer for repeated execution with an validate on the screen. The case is when you create an function to draw and redraw something on canvas, and you want after an interval period of time, the function is executed again. My solution is using DispatcherTimer belongs to using System.Windows.Threading class.

//dont forgetto use: using System.Windows.Threading;

private DispatcherTimer timer;
private long runTume = 0;
//then you have 
timer = new DispatcherTimer();
timer.Interval = new TimeSpan(tick);
timer.Tick += new EventHandler(timer_Tick);
void timer_Tick(object sender, EventArgs e)
    //throw new NotImplementedException();
    runTime += tick / 10000000;

in this case, draw(runTime); function will draw on canvas screen.
Notice that in the timer.Interval = new TimeSpan(tick); the tick value has value of 100 nanosecond, so you should concern to adjust to fix your interval time.

Read and write a txt file in C#

In the case you want to read or write some short information from or into a text file, Csharp supports a simple way for you in System.IO.File. You create a text file in directory “C:\\config.txt” and then:
Read file
There are some lines in above file, and each line has its own meaning, so you need to read every line and write into a list or an array like this:

private void readFile()
    string[] lines = System.IO.File.ReadAllLines(@"C:\config.txt");
    //then you can convert every element of the array into the data you want

Write file
You have some information need to be stored in a text file, you can use the below function:

private void writeFile()
    string[] lines = { lstCube.Count.ToString(), numOfXRect.ToString(), count.ToString() };

Are they so simple,hihi, i just want to store in case i need.:D

%d bloggers like this: