locked
Building exe and reading from txt file RRS feed

  • Question

  • Hi,

    I'm new here and I'm creating my first exe from a windows application.

    I've created a windows application that is reading data from a txt file that I've copied to the project folder.

    The application is running fine in Visual Studio.

    After I build the application, I've got my exe-file from the Debug folder and I copied that exe to my desktop.

    Then when I run that exe from my desktop, that txt file couldn't be found. (It must be possible to run that exe on different pc's and different locations)

    So here are my questions:

    - where in my projectfolder do I store that txt file

    - how can I make sure that the txt file is included in the exe

    - and what would be the path to that txt file

    I already did a lot of testing with different paths, but with no luck,

    I've got the feeling that the txt file is not copied to the exe

    I've put the txt file in the rootfolder of the project and changed the properties "Build Action" to "Content" and "Copy to Output Directory" to "Copy Always".

    When I build, I can see that the txt file is copied to the Debug folder.

    Can someone help me with this?

    Thanks in advance!

    Ilse

    • Moved by Xavier Xie-MSFT Thursday, February 4, 2016 7:17 AM move to correct forum
    Wednesday, February 3, 2016 2:36 PM

Answers

  • Hi,

    - where in my projectfolder do I store that txt file

    - how can I make sure that the txt file is included in the exe

    - and what would be the path to that txt file

    In C++ you can use GetModuleFileName function to dynamically get the current executable path and copy the text file to the corresponding path. Or when you deploy the application, please make sure the text file is deployed on the same path with exe. Then you can read the file from the exe path no matter where you put the exe.

    The code below is how to use GetModuleFileName to get the real path of exe.

    #include "stdafx.h"
    #include "stdio.h"
    #include "string.h"
    #include "Windows.h"
    
    
    void TestGetExePath(void)
    
    {
    
             //
    
             const int nBufSize = 512;
    
             TCHAR chBuf[nBufSize];
    		 wchar_t wstring[nBufSize];
    
             ZeroMemory(chBuf, nBufSize);
    
    
    
            
    
            if (GetModuleFileName(NULL, chBuf, nBufSize))
    
             {            
    
                 
    
             wprintf(L"%s\n",chBuf);
    
    
    
             }
    
    
     }
    
    int main()
    {
    	TestGetExePath();
    	system("pause");
        return 0;
    }
    

    May


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Baron Bi Thursday, March 3, 2016 2:44 AM
    • Marked as answer by Fred Bao Tuesday, March 8, 2016 5:56 AM
    Thursday, February 4, 2016 8:25 AM

All replies

  • Hi,

    In my own experience, when I have an image (say called imagefile.png) placed in a folder (say called Assets), I'll use

    BitmapImage bitmap = new BitmapImage();

    bitmap.UriSource = new Uri("ms-appx:///Assets/imagefile.png", UriKind.Absolute);

    Sometimes, in my previous apps, I do have some text files that I want to distribute with my application. I that case, I'll have a text file (called myDatafile.txt) and stored in a folder (called Data). When I want to read the file, I'll use

    StorageFolder myStore = Windows.ApplicationModel.Package.Current.InstalledLocation;
    StorageFolder myFolder = await myStore.GetFolderAsync("Data");
    StorageFile myData = await myFolder.GetFileAsync("myDatafile.txt");
    string tmpContent = await FileIO.ReadTextAsync(myData);

    myStore object is the actual folder where my application (exe) is stored and myFolder is the sub-folder of where my application (exe) is stored.

    Actual data for an app will be different, the locations should be in LocalFolder, TemporaryFolder or RoamingFolder and can be accessed using Windows.Storage.ApplicationData namespace, eg.

    StorageFolder tempStore  = ApplicationData.Current.TemporaryFolder;
    StorageFolder dataStore  = ApplicationData.Current.LocalFolder;
    StorageFolder roamStore  = ApplicationData.Current.RoamingFolder;

    Normally, my database will be in dataStore, sometimes when I generate a thumbnail and not keeping the thumbnail, I'll store them in tempStore. I recall (not entirely sure) that tempStore content is also accessed by Windows and the content in this folder might be cleared by Windows during housekeeping. I'm yet to use roamStore. I think roamStore can be used to share data between same app installed in other devices.

    I'm not sure about moving the application (exe) and running on different PC. Via Visual Studio, I think its pretty obvious, just compile/build to run on the same development PC. I also have a tablet and a notebook for testing. I use Visual Studio Remote Debugging Tool (check the link) for that. The remote debugging tool allows me to build and copy the application to a remote PC or tablet for testing. Just like when you build phone app directly to the USB connected phone.   

    ** I hope more experienced developers can correct my post.


    I'm at the advance level of my experience. In life that is, based on my age. Still learning the changing ways of programming. I do what I can to give a helping hand but sometimes due to my limited knowledge and understanding the issues, by trying to help I do make things worse and more confusing that the current problem. Please accept my apologies.



    Thursday, February 4, 2016 5:30 AM
  • Hi,

    - where in my projectfolder do I store that txt file

    - how can I make sure that the txt file is included in the exe

    - and what would be the path to that txt file

    In C++ you can use GetModuleFileName function to dynamically get the current executable path and copy the text file to the corresponding path. Or when you deploy the application, please make sure the text file is deployed on the same path with exe. Then you can read the file from the exe path no matter where you put the exe.

    The code below is how to use GetModuleFileName to get the real path of exe.

    #include "stdafx.h"
    #include "stdio.h"
    #include "string.h"
    #include "Windows.h"
    
    
    void TestGetExePath(void)
    
    {
    
             //
    
             const int nBufSize = 512;
    
             TCHAR chBuf[nBufSize];
    		 wchar_t wstring[nBufSize];
    
             ZeroMemory(chBuf, nBufSize);
    
    
    
            
    
            if (GetModuleFileName(NULL, chBuf, nBufSize))
    
             {            
    
                 
    
             wprintf(L"%s\n",chBuf);
    
    
    
             }
    
    
     }
    
    int main()
    {
    	TestGetExePath();
    	system("pause");
        return 0;
    }
    

    May


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Proposed as answer by Baron Bi Thursday, March 3, 2016 2:44 AM
    • Marked as answer by Fred Bao Tuesday, March 8, 2016 5:56 AM
    Thursday, February 4, 2016 8:25 AM