I just ran
File.Move(#"C:\sub1\file.xml", "file" + ".XMl"));
The file did dissappear from C:\sub1. No error was thrown. Did the file go somewhere?
When directory is not specified, current one is used:
https://msdn.microsoft.com/en-us/library/system.io.file.move(v=vs.110).aspx
The sourceFileName and destFileName arguments can include relative or
absolute path information. Relative path information is interpreted as
relative to the current working directory. To obtain the current
working directory, see GetCurrentDirectory.
Environment.CurrentDirectory = #"C:\Test";
// C:\sub1\file.xml will be moved to C:\Test\file.XMl
File.Move(#"C:\sub1\file.xml", "file" + ".XMl"));
It moved to the app working directory. Usually it is where executable file located
Yes, it's in your running folder.
Related
If my executable is running from a path with no spaces, this
if(!File.Exists(#"\program.exe"))
MessageBox.Show("File not found");
doesn't fire. But, if the program executes from a path with spaces, like C:\Program Files\etc\ I get a file not found. My executable application and program.exe lies in the same folder.
How can I avoid this?
Though it may seem like the path has something to do with it, it's the Working Directory. Try this code:
var path = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
"program.exe");
if (!File.Exists(path))
The path in which is it launched wouldn't cause that code to fail. However, when the working directory isn't what's expected; that will.
Furthermore, never assume that you're in a specific directory. Always provide a fully qualified path. Objects like the SaveFileDialog and OpenFileDialog can change the working directory on you at runtime.
Just get the execution directory path and append it to your exe file name
string DirPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
if(!File.Exists(DirPath + #"\program.exe"))
MessageBox.Show("File not found");
I would simply remove "\" from the argument of the method File.Exists(#"program.exe"), and check the current working directory by GetCurremtDirectory to be sure that relative path you are passing (that is relative path) constructs correct full path to executable.
File.Exist(#"\program.exe")
will check for program.exe at the root of the drive, eg c:\program.exe if your program is running from c:
To check the existence of your file in the "current directory", you have not to put the "\" character. So:
if(!File.Exists("program.exe"))
MessageBox.Show("File not found");
Here a little explanation.
I have a class in C# that saves an error message in a log file in case of an exception. Now I want to save the log file in the same folder containing the application's (in my case, a website) files. I tried using Environment.CurrentDirectory however it is not retrieving the path to my website. What can I do please to make use of a relative file path which points inside the website's directory?
Here is the class' code. As you can see, the path is absolute. I want to change it to a relative file path pointing to a folder in my website's directory.
Usually Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) returns the path where the current assembly resides. You could use
string logName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "MyLogFile.log");
to create the log file name.
Question is really whether logging to the application's folder is permitted by the OS. Also, for Web-applications, the log file would be publically visible and accessible through the web browser.
For a website use:
HttpContext.Current.Server.MapPath("~/");
You might also could try this solution:
string path = AppDomain.CurrentDomain.BaseDirectory + "anotherFolder";
This would put the base dir of the app and a folder inside of the project!
I had this console application. Now i have added Environment variable PATH to its setup so that it can be executed from any location through Console. Strangely, the same application is breaking after this change.
Installation directory contains, BIN and CONFIG folder. Exe is placed inside BIN folder.
I have this line of code,
WriteToFile(#"..\Config\Settings.xml")
The path used to write to a file Settings.xml inside Config folder inside the INSTALLATION DIRECTORY. However, now it tries to write to settings.xml inside Config folder at EXECUTION PATH.
So, if i execute my app from console as c:/users/guest/app.exe, it would try to interpret path relative to this location AND NOT relative to installation directory for the application.
Any help, suggestions?
Get the path of the executing assembly then add to it the folder and file name:
string pathOfExecutingAssembly = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string settingsPath = pathOfExecutingAssembly + "\\..\\Config\\Settings.xml"
Why don't you try getting the Executing Application's Path and append it before the path where you want to save
Path starting from \ means: start from the root directory on the current drive. \Config\Settings.xml executed from any C subdirectory gives: C:\Config\Settings.xml.
BTW, do you post exact code? It should be WriteToFile(#"\Config\Settings.xml") or WriteToFile("\Config\Settings.xml")
In any case, you need to decide, whether you want to search configuration file using absolute path, or path relative to current directory/installation directory/executable directory. The code, installation package and execution command should be changed accordingly.
I have code in C# like this.
xlWorkBook = xlApp.Workbooks.Open("data.xls", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
I have data.xls file where .exe are located.
When I compile and then run .exe, I'm receiving error that data.xls could not be found.
What I do wrong?
If your xls will always be in the same location as your .exe, you can use this to get a path that won't be hardcoded to the build directory:
string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string path = Path.Combine(directory, "data.xls");
By default, Excel assumes that the folder of the file specified is user's "My Documents" directory. If the file is not there, any attempt to open it will fail.
By specifying an absolute path to the file, you can ensure that correct file is being picked up. Make sure the file exists.
Eg-
//file is in D:\TestFolder, and its called abc.xlsx
xlApp.Workbooks.Open( #"D:\TestFolder\abc.xlsx", ....
Hope it helps.
Other answers show you how to use the absolute path to the file which is kept at certain location.
Unless you've changed your project settings, when your C# app gets built, it is being built in a bin/debug (or bin/release) folder under your project. When you run from the IDE, that's the current working directory for your app.
Try using an absolute path, or moving the data.xls file into your application's bin/debug folder.
When you specify the absolute path, make sure to prefix the string with an # sign to escape out the slashes. string path = #"c:\data\excel\data.xls";
UPDATE:
If you need to use a relative path, I would get the absolute path based on the relative patht this way:
FileInfo fileInfo = new FileInfo("data.xls");
String path = fileInfo.FullName;
This might be preferable to getting the full path based on the .exe location, because it will work even if the CWD is not the same as the .exe location.
It depends on how exactly you run your application. What makes you think that application is being ran in the same directory where executable file is located? Most probably you just forgot to set the working directory right. How to do it? See this Q&A.
i think this is a problem with the location of the Excel file. the application's working directory is not where the .exe file is located, but probably in the bin/debug folder.
I have an app launching an executable which is in the same folder as that app, by doing:
Process procStarter = new Process();
procStarter.StartInfo.FileName = "OtherApp.exe";
procStart.Start();
which works fine, until I used a file open or file save dialog in my app. Then it can't find OtherApp.exe.
Does that seem normal? Can I just fix it by adding the current folder to StartInfo.Filename (and how do I obtain the current folder)?
Using the file dialog probably changes the current directory of your process. To access a file in the same folder as your current executable you can use the following code:
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
path = Path.Combine(path, "OtherApp.exe");
Or you could add to your code:
saveFileDialog1.RestoreDirectory = true ;
when prompting for filenames.
The issue is that you can possibly change the current working directory when doing other file operations.
You want to remember the path as the other posters have showed you, but it may be that you want to create your own ProcessStartInfo instance and use the ProcessStartInfo.WorkingDirectory property so that you can remember the correct path.
Try explicitly specifying the path to OtherApp.exe.
Your open file dialog may be changing the current directory.
If you don't specify the folder explicitly, the system will look in the "current working directory" for the process.
The current working directory (usually) starts off as the application exe directory, but can be changed by browsing to with an Open or Save dialog.
Using an explicit filepath is the right answer. Best way is to not rely on the working directory at all, but to use the filepath of the current executable as a base.
Here are some ways to do this: Application.StartupPath, Application.ExecutablePath
Code might look something like this ...
var exeName = "sample.exe";
var exePath
= Path.Combine(
Path.GetDirectoryName( Application.ExecutablePath ),
exeName);
Try System.IO.Path.Combine( System.Windows.Forms.Application.StartupPath, "myfile.exe" );
If it's not a winforms project divo's answer is best (imo, at the time of this answer)