use correct path of folder in win app - c#

I write a win app,now i want to make setup for my app project.i have a folder in the
root path of my project and it contains some wave files that i use theire path
in my app like: "F:\test\Resources\1.wav".
now my queistion is: how can i use their path in correct format ,that if i install setup file,on
another computer my app works true ,only because of the path of the wave files.
thanks alot for your attention.

You can get your EXE's path by calling System.Reflection.Assembly.GetExecutingAssembly().Location.
Alternatively you could store you install path in the registry somewhere with the installer and then you can check that registry entry.

You can useApplication.StartupPath, and append your subfolder.
So something like Application.StartupPath + #"\Resources\1.wav" should do the trick.
This is without Reflection, so I guess it will be a little faster, but I haven't benchmarked it.

Related

C# File Disappeared after FileInfo.MoveTo with Local Path (Windows)

I Ran FileInfo.MoveTo("filename.txt") with just a name instead of passing a full path and the file just disappeared. I believe in linux this would make it go to the root directory "/", but on Windows I'm not sure if there is a such thing as a root directory beyond just C: Is there any way to locate my lost file?
It is likely in the working folder that your executable is running from. For example, MyApp\bin\Debug, depending on the configuration you are running in.
It should be in project folder. Usually files without specefying path are saved there. (in folder with .exe file)

C# - Copy a file to user folder

I'm making the GUI program that users can decompile/recompile an APK file but compiling won't work correctly becuase the framework file is missing. I'm making the simple framework installer that must be installed on user folder (for apktool.jar). Instead using the path that only works on my computer. i want to make it work for all users.
here is the code i made
File.Copy(#"do-not-touch\1.apk", #"C:\Users\quoc\apktool\framework\1.apk");
You can use the Environment.SpecialFolder Enumeration to get the path like so:
var userDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Note: Please use ApplicationData and not the root profile, it's rude to fill the user's toplevel profile with stuff.

Set XML file to startup path

I have C# windows application with XML file. After installing the set up file I need to edit the XML file time to time. But my XML file not going to the path where the executable is located.
So that is giving error.
With in a program I'm getting XML path like this.
private string PATH = Path.Combine(Application.StartupPath, "XMLFile1.xml");
Please some one can suggest a way to do.
If you have installed your application on Windows Vista, 7, or 8, it's quite possible that you get security exceptions. Since you haven't told what kind of errors you get I have to ask my crystal ball to think with me.
He thinks that because you are trying to write in a protected folder you get an exception.
He suggest you move the XML to %appdata% or %localappdata%
Use Application.ExecutablePath, the Application.StartupPath property will change if your app is started from desktop shortcut or any other shortcuts.
private string PATH = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "XMLFile1.xml");
You have to include it in project. Here's a helpful link: How to include XML file while creating setup file for windows application
while your application starts copy your XML file to a common folder path, if it is not exist in the path. Do your edit on the xml file in the common folder.
better to use common folder as Local application Data Folder
Path.Combine(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Your application name")

Read from output file in installed WCF service

I included a text file as output in a WCF set up project.
The text file is correctly located in the same folder with the dll, exe, and config file after the project is installed (C:\Program Files\KLTesting\KLTestApp).
However when the program tries to read it, it looks under "C:\Windows\system32", what's the correct way to find & read it?
I have
string a = Directory.GetCurrentDirectory();
a += "/R0303.txt";
string content = File.ReadAllText(a);
Thanks.
You should use AppDomain.CurrentDomain.BaseDirectory or AppDomain.CurrentDomain.SetupInformation.ApplicationBase instead, to get the Directory of your .exe file.
First you should not call Directory.GetCurrentDirectory() and concatenate with the file name. The service is running within a web server like IIS or some other type of container, so GetCurrentDirectory will not give you what you are thinking as you found out. (On a quick tangent, as a recommendation in the future if you want to do path combining you should use Path.Combine method as it will handle all the corner cases and be cross platform.)
There are a few ways to do this:
Assembly myAssembly = Assembly.GetAssembly(SomeTypeInAssm.GetType());
string fullFileName = Path.Combine(myAssembly.Location, "MyFile.txt");
The fullFileName should be what you are looking for. Make sure you use a type that is actually in an assembly located and referenced from the directory in question. However be aware because this file in your question in the Program Files area this is a secure area on Vista and higher OS's and you may not have access to do anything but read the file.
Also you could have the installer of the application place in the registry the install path. Then your service if on the same box can pull this information.
Also you could make sure the file you want to load is within the web server environment that is accessable to the WCF service in question.

How to get to app-relative subdirectory in .net windows app?

I have an application that contains a sub folder that contain xml file ,that is use as a database now i want take the path of the xml file at run time ,how can i achieve this in window application?
I know how it does in asp.net using Server.MapPath but i want this is same in windows application
please help
thanks in advance .
Use Aplication.ExecutablePath property when am XML document and executable are reside in the same directory.
I think the recommended way in Windows is to use the Application.StartupPath property.
And with Path.Combine you can have your xml file path Server.MapPath-style like this:
var appPath = Application.StartupPath;
var xmlPath = Path.Combine(appPath, "data/my_db.xml");
// xmlPath now points to app-relative data/my_db.xml file
...
A nuanced answer:
The best way to access data would be to put it in Application.CommonAppDataPath or Application.UserAppDataPath so that it does not depend on the application's installed path. However, there are many reasons why you might need to avoid this.
To answer your question:
If the application is a standard forms application deployed to the client's machine by an installer or XCopy deployment, then the path to the executable is Application.ExecutablePath
If the application is Click-Once deployed, then I would not recommend using the above since the app's path is obscured, shadow-copied and put in the sandbox. You can use ApplicationDeployment.IsNetworkDeployed to test for click-once deployment then ApplicationDeployment.CurrentDeployment.ActivationUri to get the URI that the application was launched from. Your app-relative file will be on that web server; you will always be able to download it.
of course in click-once deployent it would be better to tag the file as Data in which case it would be accessible through ApplicationDeployment.CurrentDeployment.DataDirectory
if the application is a web app, then the Application class is useless. In this case you should use Assembly.GetExecutingAssembly().Location This works because currently executing assembly for a web app is almost always in the web app's /bin directory.
For a "portable" assembly where you don't have an installer, and for rare cases where you don't want to use the Application class, use Assembly.GetEntryAssembly().Location This works because it figures out what the entry point assembly is (your application) and uses that location. This is reliable because assemblies that your entry assembly load don't have to be in the same directory as the entry assembly.
You can get the directory of the currently executing assembly using
System.Runtime.Reflection.Assembly.GetExecutingAssembly().Location
from there you can get to your subdirectory.

Categories

Resources