HOME BLOG

How to access Shared Directories on Operating Systems that support SMB protocol using SMBLibrary

Posted on: April 27th, 2025 by Olu No Comments

Hi Folks,

In this post we will briefly cover how to access files in shared directories on operating systems that support SMB protocol.

First, what’s SMB? SMB means Server Message Block. It is a communication protocol used to share files, printers, serial ports and miscellaneous cmomunications between nodes on a network.  SMB was originally developed at IBM and is widely supported in Windows mac OS and Linux.

In this post we will assume you’re programming in C#. Also, we will assume the

A good library for accessing shared drive is SMBLibrary https://github.com/TalAloni/SMBLibrary.

Checking a file exists

The procedure involves creating an instance of SMB2Client, connecting to the share server, logging in, creating a file store using TreeConnect method, and using it to look through the files on the shared directory to see if your desired file exists.

Here’s some sample code you can use for checking if a file exists in a given directory, using SMBLibrary.

string Fullpath = @"\\server\sharename\inner-path\inner-path2\";
var parts = Fullpath.Split('\\');
ShareName = parts[1]
PathPrefixInShare = $@"\\{parts[0]}\{ShareName}\";

Boolean exists(string directory, string file) {

    var result = false;
    var client = SMB2Client();
    string error = null;
    bool isConnected = client.Connect(IPAddress.Parse("sharehostname", SMBTransportType.DirectTCPTransport);

    if (isConnected) {
        NTStatus status = client.Login(String.Empty, "someusername", "somepassword")

        if (status == NTStatus.STATUS_SUCCESS) {
            ISMBFileStore fileStore = client.TreeConnect(ShareName, out status);

            object fileHandle;
            object directoryHandle;
            FileStatus fileStatus;

            if (fileStore is SMB2FileStore) {
                directory = directory.Replace(PathPrefixInShare, "");

                status = fileStore.CreateFile(out directoryHandle, out fileStatus, directory, AccessMask.GENERIC_READ, SMBLibrary.Fileattributes.Directory, ShareAccess.Read | ShareAccess.Write, CreateDisposition.FILE_OPEN, CreateOptions.FILE_DIRECTORY_FILE, null);

                if (status == NTStatus.STATUS_SUCCESS) {

                    List fileList;
                    status = fileStore.QueryDirectory(out fileList, directoryHandle, "*", FileInformationClass.FileDirectoryInformation);
                    status = fileStore.CloseFile(directoryHandle);

                    foreach(FileDirectoryInformation aFile in fileList) {
                        if (aFile.FileName == file) {
                            result = true;
                            break;
                        }
                    }
                }
                status = fileStore.Disconnect();
            } else {
                error = "There was an error accessing data";
            }
            client.Logoff();

        }
        client.Disconnect();
    }

    if (error != null) {
        throw new Exception(error);
    }
    return results;

}

 

Reading a file

The procedure involves creating an instance of SMB2Client, connecting to the share server, logging in, creating a file store using TreeConnect method, then get the inner filepath by stripping parent directories, create a file handle, by calling CreateFile method on the file store, then reading the data (bytes) from the file by calling ReadFile method on the file store.

And here is some sample code for fetching the data in a given file path within a shared drive using SMBLibrary

MemoryStream getTheFileStream(string filePath) {
    MemoryStream stream = new MemoryStream();

    var client = SMB2Client();
    string error = null;
    bool isConnected = client.Connect(IPAddress.Parse("someurl", SMBTransportType.DirectTCPTransport);

    if (isConnected) {
        NTStatus status = client.Login(String.Empty, "someuser", "somepassword")

        if (status == NTStatus.STATUS_SUCCESS) {
            ISMBFileStore fileStore = client.TreeConnect(ShareName, out status);

            object fileHandle;
            FileStatus fileStatus;

            if (fileStore is SMB2FileStore) {
                filePath = filePath.Replace(PathPrefixInShare, "");

                status = fileStore.CreateFile(out fileHandle, out fileStatus, filePath, AccessMask.GENERIC_READ | AccessMask.SYNCHRONIZE, SMBLibrary.Fileattributes.Normal, ShareAccess.Read, CreateDisposition.FILE_OPEN, CreateOptions.FILE_NON_DIRECTORY_FILE | CreateOptions.FILE_SYNCHRONOUS_IO_ALERT, null);

                if (status == NTStatus.STATUS_SUCCESS) {
                    byte[] data;
                    long bytesRead = 0;

                    while (true) {

                        status = fileStore.ReadFile(out data, fileHandle, bytesRead, (int)client.MaxReadSize);
                        if (status != NTStatus.STATUS_SUCCESS && status != NTStatus.STATUS_END_OF_FILE) {
                            throw new Exception("failed to read from file");

                        }

                        if (status == NTStatus.STATUS_END_OF_FILE || data.Length == 0) {
                            break;
                        }

                        bytesRead += data.Length;
                        stream.Write(data, 0, data.Length);
                    }

                }
                status = fileStore.CloseFile(fileHandle);
                status = fileStore.Disconnect();
            } else {
                error = "There was an error accessing data";
            }
            client.Logoff();

        }
        client.Disconnect();
    }

    if (error != null) {
        throw new Exception(error);
    }

    stream.Seek(0, SeekOrigin.Begin);
    return stream;

}

That’s all for now. Till next time, happy software development.

How to create objects with dynamic field names in C#

Posted on: December 18th, 2024 by Olu No Comments

Hi Folks,

In this post I’ll talk briefly about how to create objects with dynamic fields on the fly in a C# application.

First of all, why is this important?

An example is if you’re developing an API that returns some data where the fields are calculated at runtime.

This means you can’t anticipate what it would be before hand and create classes ahead of time.

In C# we use what is called ExpandoObject.

ExpandoObject represents an object whose members can be dynamically added or removed at runtime.

To create an ExpandoObject instance, you do something like

 

dynamic row = new ExpandoObject();

 

To set a field whose name you know at compile time, do something like

 

row.SomeField = somevalue

 

To set a field whose name you don’t know ahead of time, but whose name is stored in the value of another object, say fieldNameHolder, to some value stored in a variable, say fieldValueHolder, do something like

 

(IDictionary<string, object>row).Add(fieldNameHolder, fieldValueHolder);

 

That’s all for now. Till next time, happy software development.

 

References

ExpandoObject Class. Microsoft Learn. https://learn.microsoft.com/en-us/dotnet/api/system.dynamic.expandoobject?view=net-9.0M

Yinkos Tutorial Blog is launched

Posted on: November 10th, 2024 by Olu No Comments

Hi folks,

We will like to announce with great pleasure that the Yinkos Tutorial Blog is now launched.

Why have we launched the Yinkos Tutorial Blog? We launched it to provide a platform where we can publish educational articles from time to time to help students with the various subjects we tutor.

The blog is located at https://tutorial.yinkos.com/blog/.

You can access the blog by clicking Blog on the main menu of the Yinkos Tutorial website.

That’s all for now. Till next time, happy learning.

How to disable a test class or method in Java

Posted on: April 26th, 2024 by Olu No Comments

Hi folks,

In this brief post, we will talk about how to disable a test class or method in a Java-based project. Why would you want to do this? It could be because the test isn’t yet fully implemented. Another situation is if you have Continuous Integration in place where tests are run automatically during a build, but a certain test class isn’t yet ready for automatic execution.

To disable a test class or method, you can use the Disabled annotation from JUnit. E.g.

import org.junit.jupiter.api.Disabled
...

@Disabled
class YourTestClass {
...
}

That’s it. Now such a test would be ignored. That’s all for now. Till next time, happy software development.

 

References

Disabled (JUnit 5.0.0-M2 API). https://junit.org/junit5/docs/5.0.0-M2/api/org/junit/jupiter/api/Disabled.html

Yinkos Tutorial is launched

Posted on: February 28th, 2024 by Olu No Comments

Hi folks,

It’s with great pleasure that we announce the launch of our new service called Yinkos Tutorial.

Yinkos Tutorial provides students with quality one-on-one tutoring in a variety of subjects at secondary school level and beyond, including:

  • Math
  • Physics
  • English
  • Piano
  • Computer Programming

To access the Yinkos Tutorial website, click here.

If you would like tutoring from our expert tutors in any of the aforementioned subjects, click here to register with Yinkos Tutorial right away.

If you have any questions or comments, feel free to contact us.

How to generate hashtag symbol on a keyboard

Posted on: February 16th, 2024 by Olu No Comments

Hi folks,

in this post I go over a few ways to generate the hashtag symbol (#) on your keyboard.

On a Macintosh, the way to do this is to press Alt + 3.

If you find yourself working on a Windows virtual machine where that combination doesn’t work, alternate way to generate # is by using a combination of Alt and the 3 and 5 keys from your numeric keypad, i.e. you press and old Alt, then type 35 on the numeric keypad, then release Alt.

That’s all for now. Till next time happy computing.

References

How do you make a hashtag sign on a keyboard? – Quora

How to fix the issue where messages sent to email alias of Group doesn’t appear in inbox

Posted on: February 15th, 2024 by Olu No Comments

Hi folks,

In this post I will discuss how to fix an issue one may face when using multiple email address managed by Google Workspace. Specifically we’ll talk about how to fix the issue where messages sent to your email alias or to a Group you belong to does not appear in your Gmail inbox.

Example: John’s primary email address is john@solarmora.com. He also has the email alias salesman@solarmora.com. If John includes salesman@solarmora.com as a recipient of a message, the message isn’t sent to his inbox. However, if John sends a message to his primary account, john@solarmora.com, the message is sent to his inbox.

The cause of this problem is that Gmail, in an effort to prevent clutter, doesn’t deliver messages that you send to your own alias (or group you belong to) to your inbox.
You can find them in Sent Mail or All Mail. To receive these messages in your inbox, you need to set up the alias or Group as an alternate “Send mail as” address.

That’s all for now. Till next time, happy emailing.

References

Send emails from a different address or alias – Gmail Help.

Messages sent to email alias or Group aren’t in my inbox – Google Workspace Admin Help.

How to Grant Yinkos Hymns Manager the Microphone Permission on Safari

Posted on: September 4th, 2023 by Olu No Comments

Hi folks,

In this post, we cover how to grant the Microphone permission to Yinkos Hymns Manager (YHM) on iOS devices e.g. the iPhone. In order to record live songs with the Yinkso Hymns Manager on an iOS device, it is necessary to grant the website the Microphone permission.

If you find that you’re unable to use YHM to record live audio on the iPhone, then it could be because the Yinkos Hymns Manager website hasn’t been granted the Microphone permission. To grant this permission, follow these steps.

 

Open https://hymns-manager.yinkos.com in Safari.

Click on AA button on bottom left of browser tab.

 

 

 

Click on Website Settings.

 

 

 

Under Microphone, check if the text to the right says Allow. If not, click on Microphone, then click Allow. Then click on Done.

 

 

 

 

Now you can return to the app. you should be able to record songs live henceforth.

That’s all for now. Till next time, happy hymns management.

How to add entitlements to your libGDX iOS game

Posted on: August 23rd, 2023 by Olu No Comments

Hi folks,

In this post, we will go over how to add an entitlement (capability) to your libGDX iOS game. we assume the app uses robovm to provide the iOS support.

We will assume you want to add Game Center capability to your app.

First, open xcode for a sample project, optionally with the same bundle identifier as your game app

Add the Game Center capability within Xcode by following the instruction here.

Check your provisioning profile to make sure it has the Game Center entitlement

Verify that you can build your sample iOS app using your distribution provisioning profile

If you get an error that the provisioning profile doesn’t contain the Game Centre entitlement, then go to apple developer portal and check the profile there

If the profile has expiration field set to invalid, that means the profile is out of date. click edit button to see the profile details page, then click save. check the profile again to make sure it has a valid expiry date (in the future).

Close and re-open xcode and make sure it fetches the latest provisioning profile

Then check for the entitlements file within your Xcode project. it will be a file with file name ending with .entitlements

Copy the entitlements file into your libgdx ios project. you can call it something like Entitlements.plist.xml

Next you need to configure robovm to add that entitlement. to do this, open your robovm config file (ios/robovm.xml) file and add the following line within the config tag.

<iosEntitlementsPList>Entitlements.plist.xml</iosEntitlementsPList>

That’s it. you can now build your iOS app with robovm resting assured that it has the desired capability.

That’s all for now. Till next time, happy game development.

 

References

Configuration Reference · RoboVM User Guide. http://robovm.mobidevelop.com/docs/en/configuration.html.

How to manage hymn records with Yinkos Hymns Manager

Posted on: August 17th, 2023 by Olu No Comments

Hi folks,

In this article, we will cover how you can use Yinkos Hymns Manager (YHM) to manage record of hymns selected for your church services on specific dates.

In YHM, we call these Hymn Records. The advantages of using YHM to store hymn records are

1. Ease of use – is that it’s very easy to view all hymn records
2. Search – it’s very to search for particular hymn numbers and know what dates that hymn was selected

 

How to view all Hymn Records

Once logged in to YHM, you can access Hymn Records by clicking on Hymn Records on the home screen.

Alternatively, you can click on Hymns Management > Records on the top menu.

 

You will see a list of all hymn records grouped by date.

For each date, you will see all the hymn numbers chosen, including the position and hymn number (Hymn).

You can edit a hymn record by clicking the Edit icon.

To delete a hymn record, click the Delete icon. A confirmation dialog will be displayed.

If there is an audio recording for that hymn available, the Listen button will be active

You can click on the Listen button to view an audio player dialog and play the hymn. This can be useful when practicing songs.

If there is no audio recording, then the Listen button would be disabled.

 

Adding a new Hymn Record

You can add a new hymn record by clicking the Add button.

you can then fill out the details on the Hymn Record detail screen and click Save.

You can also create or update hymn records through the suggestions screen. Click here to find out how to use Hymn Suggestions.

 

How to search for hymn records

To search for a hymn record, enter the hymn number in the search bar and click the search button or press Enter.

You will see results showing all dates the hymn was chosen and the position it was chosen for.

To show all records, removing any search criteria, click Show all records

In this article we have covered how to manage hymn records using YHM, including how to create records, search for records, edit and delete records and listen to audio recordings of your hymns. Hope this has been helpful. Till next time, happy hymns management.