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\", "");
Related
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.
My exact file path is as follows. This .txt file is not supposed to be deployed to bin/debug
string str = File.ReadAllText(#"C:\development\slnfolder\projfolder\myfile.txt");
How can I write the code so that I do not have to hard code full path to get to the file
I am trying to avoid hard coding path in the above line of code as follows:
string file = #"myfile.txt";
string str = Path.GetFullPath(file);
but the str ends up being as follows and is not able to find the file.
C:\development\slnfolder\projfolder\bin\debug\myfile.txt
You can include myfile.txt in your Visual Studio solution and go to its properties and set the Build action to Copy always (or Copy if newer if you want to avoid copying the file if it didn't change since the previous build...).
This way you're going to have the whole file in the target directory (i.e. bin\debug).
That's where it should map, because that's where your executable is running from. I'd highly suggest you ensure that the text file is moved to the bin/debug folder (there's a VS option to copy it down in properties) rather than trying to read two levels up. It will be much easier once you end up deploying your app instead of running it from visual studio.
If you're using Visual studio, than add the txt file to your project
right click on properties
set build action to none
and set copy to output directory to copy if newer
this will ensure that the txt file is always in the same folder as your executable
To avoid hard-coding something, you should:
"Soft-code" it (i.e. make it part of your product's configuration). You can use Configuration Settings APIs for that.
Take it as a parameter on the command line (read the directory location from one of the args passed to the Main method), or
Make a convention as to where it should be located, for example, in the data directory, which is a subdirectory of your current running directory (read from #"..\data\myfile.txt").
You can always define a combination of these methods, for example, use the "by convention" location when the configuration / command line option has not been specified.
broken to bare-bones scene:
I have a program in c# that calls a .exe inside cmd(using process.start), passing some required arguments.
What i'm trying to do: Include the exe into the project so that i don't have to call cmd.
Any idea?
If you just want to include so you don't have to ship two files then just include it into the project as "embedded resource" (see project item options) and then you can call ResourceManager.GetStream and write it to file and call Process.Start.
If you want integrate the functions of that exe so that the exe is not needed anymore (no Process.Start) then you need the source code...
EDIT:
the "write to file" is not necessary if the exe is .NET - then you can directly load it from the resource stream as Assembly/AppDomin and execute it.
You can add an exe as an embedded resource (just right click on a folder in the Solution explorer, Add Existing Item, then get properties on it and set it to be Embedded Resource). However, you may not be able to easily execute it in place - you'll need to save it to disk and then execute it (which doesn't solve your stated problem of having to ShellExecute the .exe file, but does solve the problem of having to ship more than one file to the end user).
If you have the source code, then you'll be able to repackage the exe as a dll, or integrate it directly into your program code.
If the exe is a .NET assembly, you could use ILMerge to merge the exe into your main assembly. You can then invoke the code in the exe directly.
I need some help with paths please!
Basically I have a project with the following folder structure:
Project (root directory which contains the .sln file etc.)
Project/MyProj (contains the code)
Project/MyProjTest (the test folder)
Project/TestResults
Now with this Project I need to have a common folder where I can stick a bunch of files for use with the Application without having to copy the files to multiple locations etc. What is the best way to do this? Ideally I would like to have the folder as Project/ResourcesFolder, so that both the Code folder and Test folder can access it. Now if this is the case how do I call this folder from within C#? I've tried Application.StartupPath, Environment.GetCurrentDirectory but they both just return the CURRENT folder which is not what I want.
Thanks in advance.
You can add a solution folder to your solution and place common files in it.
You'll have to copy the files, you'll want your program to operate the same way after it is deployed. The simplest way to do so is by adding them to your project. In the Properties window, set Build Action = None, Copy to Output Directory = Copy if Newer. The latter setting ensures that you don't waste time copying the files over and over again.
This ensures that the files will be present in the same directory as your EXE. Both when you debug and after you deploy it. Simply use Application.StartupPath to locate them. Creating the Setup project for the app is now very simple as well.
Note that if the files are small you really want to embed them as resources.
.. goes one directory up. That is, from Project/MyProjTest you could access Project/MyProj via ../MyProj.
Use Server.MapPath("~") to get to the root folder of your application. From there you can get to wherever you need.
I am developing an application in C#. I have a folder called myfolder it contains a file called mypoints.bmp. The folder myfolder is in my project folder it is in D drive. The path is D:\Myproject\myfolder\mypoints.bmp.
Now, in my program, whereever I need mypoints.bmp I have hardcoded the whole path. When my project is copied in to a different system under C drive, I am not able to run my project because of the hardcoded path. How can I give the path so that there will not be any problem even if it is loaded in to a different system under a different drive.
The best would be to store the path in a configuration file.
Add -> New Item -> Application Configuration File
Then you can use the following class to pull the value you're looking for:
System.Configuration.ConfigurationManager
Have a look here on the usage.
If the images you are referring to are non-changing and are simply acting as a resource for your application then you could consider embedding them in resource files which are compiled as part of your assembly. That way you need never worry about paths or copying them.
If the BMP file is used in the code and is accessed in runtime, there is no point in keeping it just in the project directory. it should be also present in the outputdirectory.
i.e you could write a post-build command to copy your folder to the output directory.
like copy "$(ProjectDir)\MyFolder\*.*" "$(OutDir)"
then in the code you could just write the relative path i.e "MyFolder\MyPoints.bmp"
But please remember one thing. If this BMP file is not going to change through out your program execution, it is better that you put it as a resource.
You can use something like GetCurrentPath(), which will return your .exe file path, then add to this path your \myfolder\mypoints.bmp. This will not be depend on C or D or ... drive and folder
There is one path that you can always reliably find, no matter how your program got deployed to the target machine: the directory in which your .exe is stored.
Take advantage of this, put the .bmp file in the same directory. Or a subdirectory of the install directory. You didn't say what kind of program you wrote, it is easy with Application.ExecutablePath in Windows Forms. But the generic solution that works everywhere:
public static string GetImagePath(string filename) {
string exeFile = System.Reflection.Assembly.GetEntryAssembly().Location;
string exePath = System.IO.Path.GetDirectoryName(exeFile);
string imgPath = System.IO.Path.Combine(exePath, #"images");
string imgFile = System.IO.Path.Combine(imgPath, filename);
return imgFile;
}
Which assumes the images are stored in a subdirectory named "images". Tweak as necessary.
Last but not least: don't forget that you can add images to your program's resources so it is baked in the .exe file. Project + Properties, Resources tab. Highly recommended.
You can also use predefined or existed system variable. Anyway you must decide when the user (or you) have to define that path (in config file or wherever) - during instalaltion, first run, before first run or in any time when app is running.
Personally I would use the Environment class in C#. Then I will locate the local app settings folder using the Environment.SpecialFolder enum.
I will then store all my applications specific files inside a folder in the local app settings folder.
This way you are also UAC safe, since UAC will complain if you want to store files in Program Files if you are not Administrator.