I'm writing an application which starts through command prompt mysql.exe and mysqldump.exe.
The idea is to make a backup and then upload it to mysql with different database name.
The problem is that I didn't think what would happen if these mysql.exe and mysqldump.exe are not registered in the cmd and you have to type the path to them manually.
And my application breaks.
So I'm wondering is there any automatic way to find the path to these two files to run them correctly.
The other solution that comes to my mind is to let the user browse his files and select these executables.
To manage this path you can use an environment variable and get it from your application.
string sPath = System.Environment.GetEnvironmentVariable("MYSQL_DIR");
Other solution is copy both executables to your application path and access them with a relative path.
string sPath = "./";
Hope it helps.
Related
I created a C# application which run with Microsoft Access database and after I deployed the project and installed it on C drive the database file becomes read only, and, if I install it on D or another drive it works fine.
Please if any one can help it is appreciated (SIS is access database file) the problem is i want to make it work in C drive also.
this is my setup SIS is the access file
And this is the connection string im using
String cs = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\\SIS_DB.accdb;";
Your problem is your database file is in %ProgramFiles%. It should be in %AppData%
There are two ways to resolve
1.modify the setup project.
when you make the setup,you should specify the path of f.mdf,ensure that the file will install into AppData folder.
2.copy f.mdf to AppData folder by app.
every time you run you app,the first thing is to copy the file to AppData folder,
you can add the follow code in your Main(or init) method and try again:
string sourcePath=#"C:\PROGRAM FILES\DEFAULT COMPANY NAME\SETUPER2";
string appDataPath= Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string fileName="F.MDF";
System.IO.File.Copy(sourcePath+"\\"+fileName, appDataPath+"\\"+fileName ,false);
*1 is better.
You are old school ... the C drive these days is forbidden area.
Use either the Program Data folder for application specific data - or, for user data, the %AppData% folder where you create a folder for your application and use this folder for the user's data.
Some resources I have in my project are fine and working Ok using string paths but what if I move the project to another directory or to another computer, it will stop working.
Please I need to get the path of the resources folder of my project in a string variable,
Something like this
C:\Users\User1\Documents\<projects folder>\<project name>\Resources\
Thanks in advance.
If you know the path relative to where the app is running, you can do something like this.
First, get the app's running path:
string RunningPath = AppDomain.CurrentDomain.BaseDirectory;
Then, get navigate to the relative path using something like this:
string FileName = string.Format("{0}Resources\\file.txt", Path.GetFullPath(Path.Combine(RunningPath, #"..\..\")));
In this example I my "Resources" folder is located two directories up from my running one.
I should also mention, that if your resource is included in the project, you should be able to get it using:
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
this will return an array of your resources.
If the files are stored in your project folder, you can retrieve the files using
System.AppDomain.CurrentDomain.BaseDirectory.
This statement retrieves the path as to where your application is installed.
Click Here to get a detailed explanation on this.
This might not be the cleanest way, but it has been useful to me.
If you had a structure like:
C:\...\MyApp\app.exe
C:\...\MyApp\ConfigFiles\MyConfig.xml
The code will return a path relative to the running assembly.
GetPath("ConfigFiles/MyConfig.xml") // returns the full path to MyConfig.xml
private string GetPath(string relativePath)
{
var appPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string pattern = #"^(.+\\)(.+exe)$";
Regex regex = new Regex(pattern, RegexOptions.None);
var match = regex.Match(appPath);
return System.IO.Path.GetFullPath(match.Groups[1].Value + relativePath);
}
You could try to use |DataDirectory| documented here.
Here's a description of how it works from an old Microsoft article
One of the reasons why it was hard to work with database files before
is that the full path to the database was serialized in different
places. This made it harder to share a project and also to deploy the
application. In this version, the .NET runtime added support for what
we call the DataDirectory macro. This allows Visual Studio to put a
special variable in the connection string that will be expanded at
run-time. So instead of having a connection string like this:
"Data Source=.\SQLExpress;AttachDbFileName=c:\program files\app\data.mdf"
You can have a connection string like this:
"Data Source=.\SQLExpress;AttachDbFileName=|DataDirectory|\data.mdf"
This connection string syntax is supported by the SqlClient and OleDb
managed providers.
By default, the |DataDirectory| variable will be expanded as follow:
- For applications placed in a directory on the user machine, this will be the app's (.exe) folder.
- For apps running under ClickOnce, this will be a special data folder created by ClickOnce
- For Web apps, this will be the App_Data folder
I want to copy a picture to homegroup shared folder.
I have opened cmd.exe through Start Menu > Run > cmd.exe and typed:
copy C:\pic1.png \\SOMECOMP\Users\SOMEONE\Shared
The picture has been copied well.
However, when i try to do the same with C#, like that:
System.Diagnostics.Process.Start(#"cmd.exe", #"/c start copy C:\pic1.png \\SOMECOMP\Users\SOMEONE\Shared");
I get the following message:
Access is denied.
How can i fix this?
P.S. - File.Copy throws the same error. For me, the cmd way looked more promising.
Why on earth would you use Process.Start to copy files when there's File.Copy?
File.Copy(#"C:\pic1.png", #"\\SOMECOMP\Users\SOMEONE\Shared\pic1.png", true);
As far as the access denied message is concerned you will need to ensure that the account you are executing your program under has write permissions to this UNC share which might not be the case with ASP.NET applications or Windows NT services which run under limited privileges accounts.
You probably need to map the directory
& "net" "use" "$mapdrive" "$password" "/USER:$username"
& "copy" $src "$dest"
& "net" "use" $mapdrive "/delete"
But yes, if you can, use the System.IO.File namespace to copy the file.
I have done an application in c# windows application. In this i created a project with name ACHWINAPP. I have written some code to get the path that i required as follows
strFilePath = Directory.GetCurrentDirectory();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = strFilePath + "\\ACH\\";
But when i create a setup for the project and installed in a direcotry namely some F:\ i am getting the error ACH as not found .
What i need is when user clicks on save i would like to save the file in the directory where he installed my setup file with the folder name ACH
Any Idea please..
Do you mean:
Application.StartupPath
Might not be what you want... but its the folder from which your executable is located
Link: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.startuppath.aspx
This is a relatively simple bit of code:
string currentPath = Directory.GetCurrentDirectory();
if (!Directory.Exists(Path.Combine(currentPath, "ACH")))
Directory.CreateDirectory(Path.Combine(currentPath, "ACH"));
//at this point your folder should exist
of course there can be a bunch of reasons why you can fail to create the folder, including insufficient privileges to do so. So you should also practice safe coding and catch exceptions when dealing with the file system.
It's hard to understand exactly what you want. Maybe use the answers to this question to load files next to the currently running application?
Otherwise, either trace out using Console.WriteLine() (or if you're using Visual Studio, add a Breakpoint) to find out the initial value of strFilePath. It's probably not what you expect.
Rather than 'adding' strings together, use Path.Combine(path1, path2) to create your path:
strFilePath = Path.Combine(strFilePath, "ACH");
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.