Coding

All things computer programming related...

Get full path of executing assembly

- Posted in Coding by

C# code to get the full path of the executing assembly:

<script src="https://gist.github.com/kyrathasoft/825c2c277bdaed4d300880415f72ab5b.js"></script>

then there's also this:

<script src="https://gist.github.com/kyrathasoft/4f4f1d6639c6faff0b2e4c77e60ede87.js"></script>

using System;
using System.Reflection; //required for Assembly namespace
using System.IO; //required for Path namespace

namespace HelloWorld {
class Hello {

    static void Main(string[] args){
        Console.WriteLine("n Now executing: {0}", ExecutingAssemblyPath());
        Console.Write("n Press any key to exit... ");
        Console.ReadKey();
        Console.WriteLine();
    }

    public static string ExecutingAssemblyPath(){
    string path = AppDomain.CurrentDomain.BaseDirectory;
    string fname = Assembly.GetEntryAssembly().GetName().Name;
    path += fname;
    if(File.Exists(fname + ".exe")){ path += ".exe"; } else {path += ".dll"; }
        return path;
    }    

  }
}

I'm very interested in Twine

- Posted in Coding by

I've been interested in Twine 2.0 since it emerged onto the IF scene in 2009. But I only recently (August 2019) revisited it and began to experiment with it in earnest. As I nail down concepts, achieve insights, and come up with snippets of script that work, I'm collecting them here.

Some credit goes to Chapel and the code and examples he's made available on the Twine lab under this license.

I keep a copy of the Windows version 2.3.3 of Twine here. It's an 84.8 Mb download.

If you want a zero-install Twine engine, with some additional story formats and an extractible Web-to-Exe that can turn your Twine stories into stand-alone applications, get this. It's an 85.8 Mb download. You can put it wherever you want on your hard drive (excepting the Program Files directory) and pin a shortcut to the Twine executable to your Start Menu or Desktop.

If you experiment with Twine and decide you want to spend some extended time using it, you may want to peruse Chapel's custom macros. You can put together a custom download of the macros you like here.

Get top level subdirectories from a given directory

- Posted in Coding by

Here is a program demonstrating how to get the names of the top-level subdirectories in a given directory:

String manipulation in C#

- Posted in Coding by

Here is a method that, if passed a proper noun, such as a person's name or a street address, will return a properly formatted version of that string:

And here is an example of using method ProperlyCapitalizeName in a program:

Bytes to characters and vice versa

- Posted in Coding by

It just so happens that any character we might use in a string has a numerical encoding. The alphabet ranges from 65-90 for capital letters A through Z, and from 97-122 for lowercase letters a-z. When we want to work with text encoding, we need to add the following namespace to the top of our source code file:

using System.Text;

The numerical representation of our English alphabet takes the form of unique numbers for each upper and lowercase letter, and these are stored in a variable type called byte. A byte is an 8-bit integer that can hold values from 0 to 255.

If we have a string variable named sChar, we can obtain its numerical encoding as follows, if we have referenced the System.Text namespace:

byte[] bt = Encoding.Default.GetBytes(sChar);

The reverse is encoding bytes to characters. If we have an array of bytes, we can convert each element’s value to its corresponding character:

byte[] bytes = {40, 80, 20};
char result;
foreach(byte number in bytes)
{
  result = Convert.ToChar(number);
  Console.WriteLine(“{0} converts to {1}.”, number, result);
}

Full source code listing:

download sourcecode

Recursive file counting

- Posted in Coding by
using System;
using System.Reflection;
using System.IO;

namespace GetFilesRecursively {
class Program {

    static void Main(){
        int cnt = 0;
        long bites = 0;
        string p = string.Empty;
        foreach (string file in System.IO.Directory.GetFiles(
            GetExecutingDirectoryName(), "*",SearchOption.AllDirectories))
        {
            cnt++;
            FileInfo fi = new FileInfo(file);
            bites += fi.Length;
            //do something with file
            p = String.Format("{0:n0}", (int)bites);
        }   
        Console.Write(" Total files: {0}, ", cnt.ToString());
        Console.WriteLine("totaling {0} bytes.", p);
    }

    /* found following method here: 
    https://www.red-gate.com/simple-talk/blogs/c-getting-
    the-directory-of-a-running-executable/ */
    public static string GetExecutingDirectoryName()
    {
        var location = new Uri(Assembly.GetEntryAssembly().GetName().CodeBase);
        return new FileInfo(location.AbsolutePath).Directory.FullName;
    }       
  }
}

A php script to count Flatpress blog entries

- Posted in Coding by

I wrote a custom page in PHP to count the number of blog entries I have made, on a Flatpress self-hosted blog.

I've updated the script to work on my 2023 HTMLy blog, but the original script was for Flatpress.

We went tonight for revival meeting. Richie Coomer, a former middle school teacher of mine, was the preacher. Followed that upon getting home with three episodes of Money Heist.

Zip a passed directory using c-sharp

- Posted in Coding by

If passed a valid subdirectory, this 60-line C# console program will zip it to .zip file that is located in same directory as executing code.

Encrypt to HTML

- Posted in Coding by

I can't remember who put me onto this nifty javascript utility. It was someone on the donationcoder forum, but it's been a long time ago and cursory search didn't turn up the relevant post.

Anyway, encrypt-to-HTML is a real jewel written by Steve Clay, a Floridian web dev. You supply it plain text, either by typing or pasting into a textbox, or else from loading it from a file, and it produces an encrypted HTML file containing your protected content. When you open the produced HTML file in a browser, you're provided with a password textbox. You must enter the correct password and then your plain text will be decrypted and shown to you.

You can download this utility here. It's a 12.6 Kb download.

This small download is downloadable here on my website.

Copy contents from one folder to another

- Posted in Coding by

Introducing the CopyTo method, which faithfully copies one directory's contents to another directory. The method listing is on Github here.

And here is a sample program demonstrating its usage.

Page 4 of 7