Copy folder structure in C#
C# Helper
by RodStephens
2y ago
Sometimes I need to copy a folder structure from one directory to another. For example, for my business I have directories representing years. Inside those directories I have standard folders for payroll, schedules, accounting, and others. With the new year, I needed to duplicate that folder structure. Any normal person would simply create the half dozen new folders and be done. Being a software developer, I needed to write a program to copy folder structure for me. Enter the name of the folder that contains the subfolders that you want to duplicate and click List Folders to see the folders t ..read more
Visit website
Unblock files received from email or the internet in C#
C# Helper
by RodStephens
2y ago
I do a lot of technical editing. When a project editor sends me files, Windows marks them as from an “untrusted” source. Then when I try to open the files in Word, it warns me and asks if I want to open the file anyway. This is kind of like taking candy from people. In general you shouldn’t take candy from stranger or trust files that you receive in emails. In this case, however, I know that the files are safe, so it’s just annoying. If you right-click one of these files in File Explorer and select Properties, you’ll see something like the following picture. You can see the warning at the b ..read more
Visit website
Find palindrome dates in C#
C# Helper
by RodStephens
2y ago
A palindrome date is a date that, in numeric format, reads the same forward and backward. For example, 12/1/21. To find the most dates, you can consider any formats including two-digit days and four-digit years, and you can ignore the separator characters. For example, 12/02/2021 is a palindrome because 12022021 is the same forward and backward. Obviously this depends on whether you use the m/d/y, d/m/y, or y/m/d format. December 2021 has a whopping 11 palindrome dates! (In the m/d/y format.) When you click the Go button, the program uses the following code to search for palindrome dates. pr ..read more
Visit website
Use regular expressions to rename files in a directory hierarchy in C#
C# Helper
by RodStephens
2y ago
This example extends the example Use regular expressions to rename files within a date range and that match a pattern in C# to let you rename files that match a pattern and that were modified within a date range. See the previous example for most of the details. This post describes the changes from that version. The earlier versions of this program only searched a single directory. This version only makes one small change to let you search recursively through a directory hierarchy. When it lists the files that it will update, this version of the program uses the following code. // Get the f ..read more
Visit website
Copy a C# project
C# Helper
by RodStephens
2y ago
Sometimes you might want to copy a C# project so you can save the current version or so you can modify it for another purpose. Unfortunately, when you copy a C# project, the copied program does not automatically update the project’s name. For example, you could have the howto_copy_project project in the directory my_new_project. The project, solution, namespace, and other values inside the project will keep their old name. If you open the new and original projects at the same time, it’s easy to become confused about which version is which. To copy a C# project, copy its directory and all of ..read more
Visit website
Remove all event handlers from an event in C#
C# Helper
by RodStephens
2y ago
I found this technique on this Microsoft forum. When you click the program’s Add button, the following code installs several event handlers. private void btnAdd_Click(object sender, EventArgs e) { btnClickMe.Click += btnClickMe_Click; picCanvas.Click += picCanvas_Click; picCanvas.MouseClick += picCanvas_MouseClick; picCanvas.MouseDown += picCanvas_MouseDown; picCanvas.MouseMove += picCanvas_MouseMove; picCanvas.MouseUp += picCanvas_MouseUp; txtOutput.AppendText("Added event handlers\r\n"); } Each of the event handlers adds a message to the output text box. For e ..read more
Visit website
Display a colored battery status in C#
C# Helper
by RodStephens
2y ago
This is a minor update to the example Display battery status in a friendly way in C#. That example periodically checks the battery’s charge. It then draws textual and graphical indicators of the charge and plugged/unplugged status. That example was only for demonstration, so it drew the graphical status in four different ways. Lately I’ve been breaking in a new laptop battery, so I wanted a way to easily keep track of the battery status. This example draws the graphical status in only one way instead of four. It’s also smaller and changes the colors of the display depending on the status. For ..read more
Visit website
How to tabulate ranked voting in C#
C# Helper
by RodStephens
3y ago
New York City’s recent election used a ranked voting ballot. This example shows how you might find the winner in a ranked voting election. What Is Ranked Voting? In ranked voting (aka ranked-choice voting or preferential voting), each voter ranks the candidates into first, second, third, and other choices. To decide the winner, you repeat these steps: For each voter, find the highest ranked candidate that is still in the running and add one to that candidate’s tally. If a candidate has more than 50% of the total votes, then that candidate is the winner. If no candidate has more than 50% of ..read more
Visit website
Select rectangular areas in an image in WPF and C#
C# Helper
by RodStephens
3y ago
This is another fine example of WPF’s unofficial slogan: Twice as flexible and only five times as hard. Practically everything about this example is harder than it is in Windows forms: drawing the rectangle, selecting the area, saving the results into a file, building a menu item with a shortcut, arranging the controls, and even simply displaying the original image. I’m going to gloss over some of those topics and focus mostly on the two issues that are most central to the example: how to select rectangular areas and how to save the result into a file. Download the example program to see add ..read more
Visit website
Use VBA code to add and remove a watermark on all pages in a Word document
C# Helper
by RodStephens
3y ago
This post shows one way that you can add and remove a watermark in a Word document. To add a watermark in this way, you add a “building block” to the header of each of the document’s sections. If headers are marked so the first page is different or if odd and even pages have different watermarks, then you need to add the watermark to each separately. The following code adds a watermark to each page. Sub AddWatermarks() Dim doc As Document Dim sec As Section Dim hdr As HeaderFooter Dim rng As Range Dim strBBPath As String ' Building block names. Const confidential_1 As String = "CONFIDENTIA ..read more
Visit website

Follow C# Helper on FeedSpot

Continue with Google
Continue with Apple
OR