How to merge external files within WinForms exe - c#

I'm designing a script generator using winforms. Scope is to generate few update/Insert queries. I've template of update/insert queries within my project in a folder in format of .text.
text = File.ReadAllText(#"\Visual Studio 2013\Projects\MigrationScript\MigrationScript\Scripts\Schema_OWNER.SYS_PARAMS.txt");
text = text.Replace(Constants.LOWER_VER, lowerversion)
.Replace(Constants.CURRENT_VER, currentversion);
System.IO.Directory.CreateDirectory(string.Format(Constants.DIRECTORY_CCB_SEED_OWNER, releaseVal));
File.WriteAllText(string.Format(Constants.DIRECTORY_CCBOWNER_SYS_PARAMS, releaseVal), text);
It works like charm in my machine. But when i extract the .exe and run in another machine, i'm getting error like System.IO.DirectoryNotFoundException: Could not find a part of the path
How to include external files within the project into my .exe, so that i could run in any machine?? Believe i explained my issue. If not please revert me.

System.IO.DirectoryNotFoundException: Could not find a part of the path clearly states that path is not accessible from the system where your .exe is placed and being run. Make Sure Whatever path you have given should be accessible from the System's where your .exe is supposed to be executed.

Related

How do I link a specific file in c#?

I have a program that can edit a certain text file. Currently, I am only running it in Visual Studios. When I am referring to the text file, the file path looks something like this:
#"C:\myProjectFolder\someTextFile.txt"
For now, I can use this path and it works flawlessly, but after I deploy it, the program would only work on my computer because this path is specific to my computer.
In HTML for example, if I was linking a CSS file, it would be possible to do \stylesheets\style.css instead of C:\myWebsite\stylesheets\style.css
How can I achieve something similar in C#
Use System.Windows.Forms.Application.StartupPath and Application.executablePathfor getting current application path.
if file is in current directory of .Exe file full path is not required and just name of file is enough. you also can point to current path in some situations by ".\\"

FileNotFoundException is occur while running Wpf application on 32bit machine

I created setup file using Inno setup.I have exe,dll and one xml file in my setup.
When I install on 64 bit machine it works fine means it take xml file from directory where exe is present.
But When I install same setup on 32 bit machine it take dll path but while accessing xml file it takes path of desktop where shortcut of exe is present and showing FileNotFoundException.
Thanks
Your application is most likely not specifying pathnames on the files it is trying to open, so it is expecting to find them in the current directory. Inno Setup by default does not set the "Start In" field on shortcuts its creates; this causes Windows to pick a directory itself, which usually won't be the directory containing your application.
In virtually all cases, this is something that should be corrected at the application level. Properly designed GUI applications should not expect to be started from a particular directory; they should always specify full pathnames on files they open. In Delphi or C++Builder, for example, it's possible to get the full pathname of the directory containing the application EXE by calling: ExtractFilePath(ParamStr(0)). To get the full path of a file named "File.txt" in the application directory, use: ExtractFilePath(ParamStr(0)) + 'File.txt'.
To get path of working directory of application while loading xml file in code.
string WorkingDir=System.AppDomain.CurrentDomain.BaseDirectory
XDocument temp_xdocument= XDocument.Load(WorkingDir+"file.xml");
It works for me.

Application will not launch from command line full path, but will after CDing to directory

I am attempting to get a .Net C# application to run from a Registry Run key (at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run, where other string values live and launch things just fine). But for some inexplicable reason, the path to my app doesn't launch my app. So I decided to run the command manually from a command prompt, just to see if that was the problem. It was. So now I'm really perplexed:
This is a .Net 4.0 C# application which I've compiled in Release mode. The application lives at:
C:\Program Files (x86)\MyCompany\MyProduct\MyProduct.exe
I can double-click on the application and it runs properly. I can also open a CMD window and do the following:
cd "C:\Program Files (x86)\MyCompany\MyProduct\"
MyProduct.exe
And the application launches just fine. HOWEVER, if I try this:
"C:\Program Files (x86)\MyCompany\MyProduct\MyProduct.exe"
The application does not launch. (!) SO obviously the Registry key isn't going to work either.
Is there some kind of additional step which must be taken to run a .Net application from its full path?
Apparently your app depends on the "Current Directory". For opening some sort of file.
The best thing to do is to find that dependency and fix it with an absolute path.
When that's not possible the second-best option would be to change the Current folder to that of the running .EXE as soon as possible. That mean you should execute this line as soon as possible:
Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
But that will only work if you manage to execute that before the faulty code. Could be hard when it's for instance in a static constructor.
Sounds like you've got a relative file reference to something in the working directory. Is there a file that you're trying to load in your app? Make sure you're using the right file paths.

XNA calling an imported exe file in project

I wish to call another exe while running my own project.
string appRoot =Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
For this I get this path : C:\Users\Jeff TungMbp\Documents\Visual Studio 2010\Projects\menuSystemTutorial\menuSystemTutorial\ menuSystemTutorial\bin\x86\Debug\xxxxx.exe
This is not the bath I wish to access.
I wish to access this path : C:\Users\Jeff TungMbp\Documents\Visual Studio 2010\Projects\menuSystemTutorial\menuSystemTutorial\ menuSystemTutorial\xxxxxx.exe
I don't want to do hard code like System.Process(#"C:\xxxxxxx") .
The reason I wish to access the path is because I've imported the .exe file into my project, the path I wish to access is the .exe exact location.
Any way to solve this problem? Or is there any other ways to make an .exe file attach with my project after publish it as setup file?
Thanks.
Well, I don't believe there is a way you can point directly to your menuSystemTutorial\xxxxxx.exe path without custom situations.
Since GetExecutingAssembly() method gets the assembly that contains the code that is currently executing, and Visual Studio creates your program exe under bin\x86\Debug folder, seems to me there is no way to do it.
But if can get the path you want from the original one, you can use string.Replace() method like;
string appRoot = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location).Replace(#"bin\x86\Debug\", "");

Working Directory in Visual Studio C# file

What exactly is Working Directory in the properties of Visual Studio C# project.
I have see a project where I right click and go to Properties and then I go to Debug tab, it shows me Working Directory where Author of the code has specified folder in local machine. I want to know what does that working directory means and what kinds of file will it store in that directory.
Thank you in advance.
Every process has a current Working Directory which is where all relative paths will be formed from. If not specified, this directory is the directory in which the active application started.
You can check which directory is set by calling:
System.IO.Directory.GetCurrentDirectory();
As mentioned above in a comment by #0xA3 this setting has no effect to your deployed product, it is is only for debugging.
The working directory of a project (and any Windows program in general) is the default place in which a program is looking up it's files. For example: My program has working directory C:\dir and tries to open test.txt, it would look in C:\dir\test.txt.
So every opened file will be opened relative to the working folder.
I think it will store nothing there, unless you add/write code in your application which explicitly creates files, either explicitly in the application's working directory, or implicitly specifying only a filename without specifying a directory at all.

Categories