Visual C++ GetCurrentDirectory() Not Working Properly During Startup - c++-cli

The program works fine, with an issue.
GetCurrentDirectory();
When this is used during start-up it shows the directory as "C:\Documents and Settings\Administrator" whatever be the actual application directory.
If use close the application and run it again it gives the proper directory.
How do you solve this problem without having to write the present directory in a separate file and then reading it.

I would use GetModuleFileName() and then search for the last "\" ,split the string check with the GetCurrentDirectory() , If it does not match I would set the split string as the current directory using SetcurrentDirectory()

Related

Is it possible to have .net console application that embed another executable file?

I have a single command line windows executable that has many options built into this exe file.
Eg:
(It can take screenshot)
ToolGo.exe printscreen c:\temp\filename.jpg yyyymmdd
(It can show up)
ToolGo.exe showIP machineA
I want to write another command line application, possibly in .net , where it can embed/build a wrapper around this ToolGo.exe file into my application without the user be able to use the ToolGo.exe, and also users can only access one function of this main exe file.
In the example I want this other tool to access only the print screen function in this new exe file.
The new application will have this:
Tool2go.exe printscreen c:\temp\filename.jpg yyyymmdd
But if someone types the following, it will not work:
Tool2go.exe showIP machineA
Or
ToolGo.exe showIP machineA
Any ideas how I can write this code to do this in a .net command line application?
This is a multi-part question, so I'll just give the main part of the issue as the answer with suggestions on handling the rest.
You can embed a .exe into your program by clicking on Properties and navigating the the Resources section, and adding that .exe to it.
After that, it's just a matter of extracting it locally so you can pass your commands to it, and handle it's responses. (I'm not really aware of any way to do so w/out first extracting the. exe; the .exe itself needs to run somehow after all).
To extract the embedded .exe, you do this:
' Extract the MyProgram resource (i.e. your .exe)
Dim b() As Byte = My.Resources.MyProgram
' Write it to the user's Temp folder
File.WriteAllBytes(Environment.ExpandEnvironmentVariables("%TEMP%\MyProgram.exe"), b)
By extracting it to the user's Temp folder, you can pass it your commands, and since it's 'out of sight' the user probably won't even know it's there to directly use it themselves, unless they're a bit more advanced and visit their Temp folder often. You can slightly help to avoid this, but extracting the .exe when your program starts, and then deleting it when it exits, so it only exists on the user's system while your program is running.
As far as what the user can and cannot type in order to pass to the program, you can simply handle the filtering with your program; since your program is the one passing the commands to the .exe, just don't pass any commands that you don't allowed, and pass the ones you do want allowed.

Visual Studio 2010 Setup does not create added file

My problem should be plain and simple to solve, but google is not helping me today.
I need to read/write a configuration file (config.xml) and, as i see so much problems with permissions with special folders, i decided for myDocuments.
Now, from File system (Setup), I added a custom special folder (myDocuments)
added a subfolder (g1OKweb) inside myDocuments
added the file (config.xml) inside g1OKweb
What I expect, reading around, is that during the installation g1OKweb should be created if not existing or older, and the same for config.xml, but it isn't.
Does someone have any clue?
Thanks in advance
Use Directory.CreateDirectory to create the directory before attempting to access the file. This will automatically create all parts of the path that do not yet exist. If the full path already exists, it will do nothing.
When opening the file, use a FileStream constructor overload that allows you to specify FileMode.OpenOrCreate. This will succeed regardless of whether the file already exists or not.
When you have opened the file, check to see if it is empty before parsing it. If it is empty, insert your XML root element first.

vb.net, How to get the rest of a directory name, when it changes?

I have a program that writes a file to a directory and it works fine. But the directory is part of a game, and every time they update, the path changes, for ex.
C:\Riot Games\League of Legends\RADS\solutions\lol_game_client_sln\releases\0.0.0.229\deploy\DATA\menu\hud
But when they update, it might change to something like:
C:\Riot Games\League of Legends\RADS\solutions\lol_game_client_sln\releases\0.0.0.314\deploy\DATA\menu\hud
Note the 0.0.0.299 changed to 0.0.0.314. I want the program to still be usable after updates. So is there anyway for it to finish the rest of the path past the releases folder automatically?
You will have to create a Windows Service that uses FileSystemWatcher. It is capable of automatic detection of file/directory changes and raising event that you can use.
dim LatestVersion as String = ..............Your Version Number....................
"C:\Riot Games\League of Legends\RADS\solutions\lol_game_client_sln\releases\" & LatestVersion & "\deploy\DATA\menu\hud"
Find all of the directories within "Releases" and find the latest version (numerical order). Then user that in your file path.
.GetDirectories finds all directories within a folder.

Read from App_Data using Json (.NET)

We got a homework in college, to make a Local web-site using MVC and VB, that will read from a Json file (that is places in the App_Data folder) some seed data and populate the database.
The problem is, I'm not sure how to make Json read from the file in App_data, without assigning a full address to it.
What happens, pretty much, is I give it an address:
File.ReadFile("~/App_Data/emails.json") 'Also tried "App_Data/emails.json"
After that, I pass this path (as filename) to a StreamReader:
Using fileRead As New IO.StreamReader(filename)
Json read stuff
And instead of reading this address as the address of the Project ("C:\Users\BlueLight\Desktop\Codes\VBA\NMCAss2\App_Data\emails.json") it throws an Exception, that it
"could not find a part of the path 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\App_Data\emails.json'." So, it searches in a different place.
Can I redirect this path to my App_Data, without using the full path? Or, maybe there is a problem somewhere in my logic?
Thanks in advance.
Update: I tried reversing "/" with "\" like "App_Data\filename.json" and "'~\App_Data\filename.json" it still didn't work.
Apparently, the best way to fix it is using
System.Web.HttpContext.Current.Server.MapPath("~/App_Data/namemails.json")
Works perfectly.

Vb.net compact framework and Saving XML (app.config)

I am writing my first windows ce app. I am using an xml file (app.Config) to store state data... basically user preferences on the last location (connection string) that the person was using before closing the app. I am having trouble understanding what is happening on deploy. It looks like it is copying my xml file to the debug folder on deploy. My problem is, when I save the xml file it is not saving in my project. Is there another folder in which the emulator resides that contains all of the state data and possibly the file that I am writing out? I am saving the doc to the same filepath as I am reading in but it does not actually save or throw error or anything.
Any help is appreciated!
Thanks
I can think of three possible issues that might cause this behaviour:
The app.config gets renamed at compile/deploy time to MyFirstApp.exe.config. Make sure you are writing back to that filename and not app.config.
app.config normally lives in your source folder and at compile time is copied to the bin\Debug directory. For .NETCF project the MyFirstApp.exe.config is then deployed to the emulator or the device.
Make sure you are writing to the correct directory, this code snippet might help:
string appDir = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
You are doing everything correct but app.config replaces your changes in MyFirstApp.exe.config everytime your rebuild-all or deploy.
You have to make sure you're reading and writing to the correct place. I was able to get something similar working using this post.