I am trying to access the help file that I made in the resource of the visual studio. The way that I access it is using:
Help.ShowHelp(this, "D:\TEs\TEs\Resources\Manual.chm");
This doesn't work when I create an executable file and try it on another computer because there is no file in the TEs directory.
What is the best way for my programme to always call the help file from the resources?
Place your .chm in the same folder as the executable and use a relative path to access it.
Note that your application's working folder may change during execution, so you'll probably want to use a folder relative to the location of Assembly.GetEntryAssembly().
Related
I want to acess a .txt file, which I stored in the resources folder of my project, there where all the imported pictures are stored as well.
I want to use something like a relative path, since every user would have safed his programm somewehere else on his Pc, but there is always the resources folder at the same place, compared to the programm folder.
I tried to used this: displayText = System.IO.File.ReadAllText("Resources\\startmessages.txt"); but this isn't working.
I get this error message: System.IO.DirectoryNotFoundException:, it lists the unrelative path to the .txt there as well, so I don't get, why it cant read it.
Thanks for your Help.
What #ChetanRanpariya is trying to tell you is, that your programm is built in another folder than your folder Resources is sitting to. So you have explictly tell your file Resources\startmessages.txt to copy itself on build process, so it get copied to said another folder. Assuming that you are using Visual Studio, you have to right click on your file and set Copy To Output Directory to true. Its relative folder path (Resources\) will be taken over. You find your build folder somewhere in your bin folder depending on configuration and framework. :)
Current Path where your executable is
Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).FullName
Path to Solution
If you are using Visual Studio and need to access the folders in the solution directory, you can use .Parent method,
Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName
Use of Path.Combine
and once you have the location of your Resource folder, use Path.Combine to get the location to read files / content etc
Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName, "Resources\\startMessages.txt")
How can we get a file in another folder of a console application?
I have an image file in a folder relative to the.cs file which is trying to access it.
For now I am using the exact path to the file, but when I ship this application to the client, it may not work as the path would not be valid on the client's machine.
Here's the code I am using right now:
workSheetIntroduction.Pictures.Add(5,1, #"C:\Users\Charu\Documents\Visual Studio 2013\Projects\AsmEpmReports\EmpReports.DomainLayer\Resources\Images\EpmLogo.png");
The Microsoft ideal is to install the application and then use the user path.
The files are then saved to the user's tree. If you cannot do an install, you can check if the directory exists before saving and then create it. You can also have the application ask for the directory and then create it where the user desires it. Or set it up as a configuration variable.
Lots of options. I would choose the first, if possible, as that is the preferred Microsoft method.
As Gregory said, relative paths would be the ideal, but you could ensure that the file is in the relative path by performing a FILE.Exists(<path>) where <path> is the relative path of the file to be used.
See Naming Files, Paths, and Namespaces for more information on relative vs. absolute paths.
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\", "");
I have a application with one folder which i added by right clicking the project, selecting add folder. Inside this folder i have xml files which are set to build action:content, copy to output directory: copy if newer (i have tried setting to embedded resource) As well as this i have a few text files and so on.
In my bin/debug output directory i have the exe, the folder with the xml, the stand alone .txt files and so on. My problem is, if i send the exe to my friend to try he always gets an exception thrown.
Say he puts the exe on the desktop, my programme at some point reads the filenames of the xml files in the folder. It uses the following code to do so
String[] filePaths = Directory.GetFiles(#"DataSources\");
I assume that because of this, when the exe runs from the desktop, it expect the folder of .xml files to be in the same place? I have the same type of exception when trying to read the .txt files too. What am i doing wrong here?
Thanks for your time
When reading from files using relative paths you get the one relative to the applications current directory. tip: In C# you can see what directory that is using Environment.CurrentDirectory.
So if you create a shortcut on your desktop, you need to make sure you right click the shortcut and set its "Start in"-folder to the directory of your application. That way its current directory will be set when its started and relative paths will be relative to that path and not the path of the shortcut.
If you actually moved the exe file to the desktop you also need to move any resources that it needs, so if it wants a folder named "datasources" you would have to move that folder as well, or set the current directory when you start the application.
Have you tried something like: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.executablepath.aspx or http://msdn.microsoft.com/en-us/library/system.environment.currentdirectory.aspx ?
So
Directory.GetFiles(environment.currentdirectory + #"\DataSources\");
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.