I have an app.config file where in I have a section for specifying the path of file to load it using reflection. Luckily assembly to be loaded and application .exe both are at same place. therefore, I have added following section in app.config
<pluing name = "xyz" path = "1P.SlotAssignment">
// path is simply name of assembly as it is located in the same folder where .exe is.
It seems to work fine when I open .exe from shortcut. But if I double click a file(file which is created and saved at different location via my .exe) to open in my application, then it crashes.
Because in this case, the path it looks is where file is located. It is not searching where .exe of application is located(~programfiles).
So, How to configure the path in correct way.
A Simple solution generate your assembly dynamically path by concating
System.Reflection.Assembly.GetEntryAssembly().Location;
and path from:
If you share your assembly load code i can help with that
Related
I'm trying to load and save an xml file called Modules.xml in my code. I have currently got the file path hardcoded as shown below. I am trying to get the file path within my code without it being hardcoded.
I have tried using Path.GetDirectoryName and new FileInfo("Modules.xml").Directory.FullName. However, both of these target the file in my debug folder, when the file I need is in the main solution folder.
Is there a way to target the file in my main solution folder instead of my debug folder? (both files are called Modules.xml)
doc.Save("C:\\Users\\Matthew\\Desktop\\Year4\\Object Oriented\\Project1\\Project1\\Modules.xml");
Both file locations are shown below:
C:\Users\Matthew\Desktop\Year4\Object Oriented\Project1\Project1\Modules.xml
^^^this is the file path I need for my code^^^
C:\Users\Matthew\Desktop\Year4\Object Oriented\Project1\Project1\bin\Debug\Modules.xml
The best approach here would be to use a configuration file, e.g. app.config, for storing such a string. Then you can change file path without recompiling the code, and your file can be stored in any location accessible by application.
If you really want to access your file the way you explained, AppDomain.CurrentDomain.BaseDirectory will provide you with the bin/Debug location in runtime. Then you can find a relative path from there like:
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, #"..\..\", fileName);
where fileName is "Modules.xml" for example.
I have tried using Path.GetDirectoryName and new
FileInfo("Modules.xml").Directory.FullName. However, both of these
target the file in my debug folder, when the file I need is in the
main solution folder.
That's because bin\Debug is your working directory when you start and run the project. To change that, you can set the working directory environment variable to point to your solution directory (instead of bin\debug|release) which I wouldn't recommend that. Because when you finally endup with development, and release the application, there wouldn't be any solution directory that holds your XML file. What I can suggest is to copy your XML file to the output folder. Either you are in development (debug) or production (release) mode, the XML always going to be copied to final directory. And you can access the working directory with something like AppDomain.CurrentDomain.BaseDirectory. To enabling copy XML to output directory, right-click on it, choose Properties, set Build Action to None, and set Copy to Output Directory to Copy Always or Copy if newer. You're good to go now.
I placed a dll file in the bin folder of a class library project and added a reference to it. Added namespace but when I am creating object of a class of this dll it is giving me run time error -
Could not load file or assembly 'dll name' or one of its dependencies. The system cannot find the file specified.
I tried using the below code to load the dll:
Assembly MyDALL = Assembly.LoadFile("Test.dll");
Type MyLoadClass = MyDALL.GetType("Test.Class1");
object obj = Activator.CreateInstance(MyLoadClass);
Now it looks for its dependent dll in given error again.
Any solution to this?
You should try to load the .dll from the directory you are running your application in. You can get that from code using:
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
Note that if you put your .dll in the /bin directory, it might get overwritten next time you compile your application.
You might be better off including it into the project, and then marking it for output by right clicking it in the Solution Explorer, selecting Properties, and setting Copy to Output Directory to Copy Always or Copy if newer.
That way it will be published to your /bin directory automatically.
I want to show a local html file which exists in my project:
The calling file is the HelpFile.cs and in that the form contains the WebBrowser control.
The address I'm trying to reach is:
C:\Users\Keith\Desktop\Lingerie\Corset\Corset\Bra.html
The file is being called from:
The result is the dreaded page can't be displayed.
What I would like to do is be able to call the file as a relative html page. At a later stage, I would like to be able to call different help files.
Is this the best way to proceed or have I made a fundamental error and gone down the wrong path?
Any constructive help would be appreciated.
A file which exists in your project, lives in a specific location in your machine. But after you distribute the program and run it on user's machine, the file will not exists in the target machine.
You may want to distribute the file or add it as resource. To solve the problem you can use either of the following solutions:
You can copy the file to output directory at build time
You can add the file to a resource file like Resources.resx
You can make the file as an embedded resource
Then to show the file, you can use the following methods:
Get the file path and call the Navigate method or assign it to Url property
Get the resource content and assign it to DocumentText property
Get the resource stream and assign it to DocumentStream property
Copy the file to Output Directory
To copy the file to output directory at build time:
Solution explorer → See properties of your file
Set Build Action to Content.
Set Copy to Output Directory to Copy always.
Then the file will be copied to your output directory and you can use it this way:
var path = System.IO.Path.Combine(Application.StartupPath, "test.html");
this.webBrowser1.Navigate(path);
Please note, if the file is located under a folder in your project, for example under MyFolder, then it will be copied into a folder with the same name in the output directory of the application:
var path = System.IO.Path.Combine(Application.StartupPath, "MyFolder", "test.html");
this.webBrowser1.Navigate(path);
Add the file to a resx resource file like Resources.Resx
You can add the file to resource file of the project. This way it will be distributed in a resource assembly and you don't need to copy the file to output directory. To do so:
Solution explorer → Your project → Properties folder → open Resources.Resx file
From toolbar of the designer → Add existing file → Add the html file.
Then the content of the file will be available through a string property of the Resources. The property name will be same as the file name, for example if the file name is test.html, the property name will be test and You can use it this way:
this.webBrowser1.DocumentText = Properties.Resources.test;
Please note, for this solution the file doesn't need to be distributed by your project and it will be part of the resource file. However it will be part of your project file.
Make the file as an embedded resource
You can make the file as an embedded resource. This way it will be distributed in a resource assembly and you don't need to copy the file to output directory. To do so:
Solution explorer → See properties of your file
Set Build Action to Embedded Resource.
Set Copy to Output Directory to Do not copy.
Then to use, you need to get the file content from embedded resources. Assuming the file name is "test.html":
var fileName = "test.html";
var name = Assembly.GetExecutingAssembly().GetManifestResourceNames()
.Where(x => x.EndsWith(fileName)).First();
webBrowser1.DocumentStream =
Assembly.GetExecutingAssembly().GetManifestResourceStream(name);
Please note, if you have the file inside a folder like MyFolder in the project, then the filename in above example will be "MyFolder.test.html".
I am creating an application in WPF.
In that I am using XML file to store some settings.
My app will run for every 10 sec. So it will use that XML file settings.
My issue is in My local system i am calling the XML file as D://Foldername/projectname/test.xml .
But after deployment it is storing in C://Programfiles/Projectname/test.xml .
So how to give a generic path so that it runs in all the client systems.
I am creating setup file to install in clients systems.
Please help me.
Open the project properties page.
Click on Settings tab.
Add a new item called "MyPath". Make it an Application Setting of type String and give it a sensible default path name as value.
Reference the value in code with Properties.Settings.Default.MyPath.
If you open the applications config there will be a setting called MyPath where you can override the path at runtime.
I suggest you to put the XML file in the same folder as your EXE file and then use Assembly to get its current path.
var cfgPath = Assembly.GetExecutingAssembly().Location + ".config"
Update
it's better to name your config file the same with your exe file but with ".config" extension.
If you are really using ClickOnce, I hardly recommend you to create your own directory for data and configuration files:
private static string GetDataDir()
{
var dataDir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"YourApplicationName");
if (!Directory.Exists(dataDir))
Directory.CreateDirectory(dataDir);
return dataDir;
}
The problem with storing the data in the directory of the executable is, that it will be at a different location. While debugging, it will be in you \bin directory. When the application is deployed by ClickOnce, you gonna have a bad time. The installation directory for a ClickOnce application is created for every version. So if you EVER update your application at "customers", all their settings will be lost.
I had a folder on my desktop with files in it. I copied that into the folder of my solution and in the solution explorer I referenced that folder into the solution. However, Im not able to open files in that folder with a relative path.
The relative path from the cs-file would be "../FolderIAdded/blabla" as seen in the solution explorer. But in the windows explorer, the path is differen of course:
Solutionfolder
- SolutionFolder.sln
- Solutionfolder.v11.suo
- SolutionFolder
-- bin
-- obj
-- Properties
-- TheFolderIAdded
-- App.config
-- Form1.cs
-- etc.
Here, it would be "FolderIAdded/blabla"
Where do I have to put that folder?
My goal: I want to be able to open files from that folder in my c#-code with a relative path.
You're assuming that your program runs in the directory where your source code is located. That's not the case. Depending on your configuration, your program will execute from a directory inside Solutionfolder\bin.
One possible solution is to copy the file(s) to the output directory when you build your project.
Another alternative is to embed the files into your application's assembly at compile time, although this precludes editing of them after deployment. To do that, set Build Action to 'Embedded Resource', then you can access them using the GetManifestResourceStream method of the Assembly class. The filename you need to give it will be derived from the path within the project structure, so in your example it would be "TheFolderIAdded.Filename.ext".
Yes, that's a dot, not a backslash.
Assuming the files are embedded in the same assembly the code that wants to read them is in, the code will look something like
var assembly = Assembly.GetExecutingAssembly();
using (var stream =
assembly.GetManifestResourceStream("TheFolderIAdded.Filename.ext"))
using (var reader = new StreamReader(stream)) {
string fileContents = reader.ReadToEnd();
}
I don't think it's a good idea to write relative path from .cs file. Better build the path base on where the application is executed:
One example, there are plenty other on the web: How can I get the application's path in a .NET console application?
(Your application is not running in the solution's root folder but where the .exe file is locatated. For example when you debug a desktop application, it runs typically from [solution folder]/bin/debug/ )
Then make sure the file you want to open property Copy to Output Directory is set to Copy Always or Copy if newer. (Right click on the file in your Solution Explorer and click on "Properties" to be sure to access it.)