none
How to add resources that contain folder RRS feed

  • Question

  • Hi

    So i dont know what to do and i had a lot things to ask cause im very noob. Here i make my question list if anyone cn help me i appreciate it

    1. How to make my project to .exe file

    2. How i can add my resource folder, i need it inside a folder because i had a 100 files in different folder, i know i can add a resource from the cisual studio but it only work with file not folder. so how i can add my resource folder

    3. I do this to get my resource file

        string dataPath = @"C:\Users\vegay\source\repos\GisDestop\GisDestop\Resources\DataMaps\";

        but i realize that when i run it on other pc it will throw some error  because the path didnt exist. So how can i get my resource      file??

    Saturday, February 29, 2020 2:01 AM

All replies

  • I will start you off with an example were in this case the folder name will not exists first time through but will be created unless the user does not have rights to the folder.

    AppDomain.CurrentDomain.BaseDirectory represents the executable folder but you could replace it with 

    C:\Users\vegay\source\repos\GisDestop\GisDestop\Resources\DataMaps

    And since you are in a VB.NET forum I will give you that and C# since you posted C#

    VB

    Dim folderName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Level1", "Leve2")
    If Not Directory.Exists(folderName) Then
        Directory.CreateDirectory(folderName)
    End If

    C#

    var folderName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Level1", "Leve2");
    if (!Directory.Exists(folderName))
    {
       Directory.CreateDirectory(folderName);
    }

    Okay, for the next part, this time sticking with C# as it appears you are using C#. If you want to get say files from resources. File names can not have a . character and if it does a file named SomeFile.exe will be named SomeFile_exe.

    This code will get you files.

    Using statement

    using System.Reflection;

    public Dictionary<string, string> GetTextResources()
    {
    
        var resultDictionary = new Dictionary<string, string>();
        var properties = typeof(Resources)
            .GetProperties(
                BindingFlags.NonPublic | 
                BindingFlags.Instance | 
                BindingFlags.Static);
    
        var stringItems = (from pi in properties
            where pi.PropertyType == typeof(string)
            select pi).ToList();
    
        if (stringItems.Count <= 0) return resultDictionary;
    
        foreach (var propertyInfo in stringItems)
        {
            resultDictionary.Add(
                propertyInfo.Name, 
                Resources.ResourceManager.GetObject(propertyInfo.Name)?.ToString());
        }
    
        return resultDictionary;
    
    }

    Get names via key property and value contains the contents

    var results = GetTextResources();
    foreach (var entry in results)
    {
        Console.WriteLine($"File name: {entry.Key}");
    }

    Say the file is a bitmap or icon, see this project

    So the above is used when you have a great deal of resources and want to iterate them, otherwise you can access them one by one via

    Properties.Resources.Information1

    Where in this case Information1 is a resource.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange


    Saturday, February 29, 2020 3:19 AM
    Moderator
  • hello friend

    1- IT it is better to develop a setup file .Go to the file menu -> other project . Then you can add a setup file that will be contain all of your dependencies .

    2- In case of .Net project when you bulid the project .exe file is created to your debug folder from the project folder path

    3- exe file execution path will get from 

    var folderName = AppDomain.CurrentDomain.BaseDirectory;

    4- you can get your file path by adding the filename with the folderName with double slass 

    strint filepath = folderName + "//" + file name

    Saturday, February 29, 2020 10:46 AM
  •  

    Hi Noir3476,
    Based on your description, I have some suggestions you can refer to.
    1.You can use ClickOnce to publish you project by following the steps:
    Right click the project > Properties> Publish> Fill in each parameter
    2.To include a file to your publish, you can add the file to your project and set its Build Action to Content. 
    Right click your files> Properties> Build Action
    To make sure the file is included in publish, go to Project >Properties Publish Tab > Application Files Button and check if Publish Status of your file is Include (Auto).
    3.You can find the file by the following statement.

    var path = System.IO.Path.Combine(Application.StartupPath, "files");

    Here are some related documents you can refer to.
    [Find 'Application Files' directory after publish by ClickOnce]
    [How to: Publish a ClickOnce application using the Publish Wizard]
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 3, 2020 7:12 AM
  • Hi Noir3476,
    Has your problem been solved? If it is resolved, we suggest that you mark it as the answer. So it can help other people who have the same problem find a solution quickly. If not solved, what problem did you encounter?
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 4, 2020 9:39 AM