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

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)

Related

Finding a specific file path when two files have the same name in different locations in C#

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.

ReadAllLines() local directory not working

For some reason the ReadAllLines() looks in the wrong folder.
string[] LoadLines = File.ReadAllLines(#"Assets\\UserFile.txt");
The "Assets\UserFile.txt" is located where the exe file is.
The Debugger throws a System.IO.DirectoryNotFoundException with a comment:
"Could not find a part of the path C:\WINDOWS\SysWOW64\Assets\UserFile.txt"
Why is it checking in the wrong folder?
Try this...
string path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), #"Assets\UserFile.txt");
string[] lines= File.ReadAllLines(path);
Note: This will look in the folder you are executing from so make sure the folder/file exists in there.
If the executing folder is your bin folder, ensure that the file property is set to "Content" and "Always copy" or "Copy when newer" within Visual Studio.
Relative path names are resolved relative to the working directory of the process, not relative to the executable. So presumably your process has a working directory of c:\Windows\SysWOW64.
If your code needs to load assets that are effectively bundled with the application, I'd use embedded resources as a way of not having to worry about physical file paths.

Why does my file directory include '/bin'?

I'm trying to find a file in a settings folder in my application. I have a xml file there. When I run the following code:
XDocument xDoc = XDocument.Load(#"..\settings\Settings.xml");
I get the DirectoryNotFoundException and the exception says not found at \bin\settings\Settings.xml'., instead of above. I even tried the full root directory to see the issue, C://... but it still includes a bin folder?
How can I have it so it doesn't include the bin part?
By default, the build results in Visual Studio are saved in a folder like bin\Debug. Since you use a relative path that jumps one folder higher, you get yourProjectFolder\bin\settings\Settings.xml. That file doesn't exist, since it's presumably in the project folder, not the bin folder.
The typical way to deal with this is to make sure the files that are supposed to be a part of the content actually have Build Action set to Content.
Using a rooted path definitely works - most likely, you made a mistake somewhere; either the path isn't rooted at all, or you're doing something like interpreting the path as an URI rather than a file path. XDocument.Load takes a URI, not a file path - the proper way to reference an absolute path on the filesystem would be file://C:/ThePath/Settings/Settings.xml.

How to attach file to windows service?

Is it possible to attach a flat file to windows-service?
My windows-service uses few flat file (for data purposes). Usually, if it's a normal executable I would place those files in the same directory as exe. How can I achieve that with windows-service?
I've done some research on this, but all I found was:
1. Pass a path to those files as a parameter to windows-service.
2. Make a fixed path and just require those files there
But I don't like those solutions. I was wondering if it's possible to attach those files to the windows service while installing it?
How about adding these files inside the project as Embedded Resources? They won't show up on the disk, but you could still properly read them from inside the assembly itself.
Here's some reference: https://support.microsoft.com/en-ie/kb/319292
You can look up the directory that your application is installed in at runtime, using the Application.StartupPath property from System.Windows.Forms. This works for both applications and services. You should avoid hard-coding the path that you think your application is installed in, because the end user may have installed it somewhere else. You should also avoid using the current directory (i.e., opening the file by name only, without a specific path) because the user may be running your application with a different current directory.
Note that installutil does not make a copy of your service executable. So the service runs from the same directory that it was in when you installed it, and any files you place in that directory should still be there when the service is running.

Location of applicationname.exe.config in C#

Where I can find the applicationname.exe.config file in my Windows form application.
Where can I find the application exe... mentioned that config file should be placed in Release or Debug files. But I cannot see the file in those places.
Also, I manually copied the config file from project root directory and past it in Release directory. Still the changes are not effected.
Then, I renamed the config as applicationname.exe.config, still the not effected.
Anybody can tell me how can I overcome ?
It should be in the same folder as applicationname.exe. That is generally where the config files are for win forms applications.
If it is not there, you can create a new one, with the name applicationname.exe.config.
Try like this path. i.e, inside bin folder
C:\Documents and Settings\deepika\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\bin\Debug
Try,
string path = Environment.GetCommandLineArgs()[0] + ".config";

Categories

Resources