In my c# window application. I have 2 folders in the same path where exe is exist. One is Input folder where we put all files for process and second is processed folder where after processing files are moved.
Let say we have file name 1.txt at input folder after processing the file is cut from input and moved into processed folder. Immediatly when i placed same name file 1.txt at input folder then after processing it will again moved into processed folder where already another file exist of same name 1.txt.
Her i found that old 1.txt automatically moved out from processed folder and comes in the path where exe is placed. and new 1.txt will comes in processed folder.
I would like to know whether dotnet behaves in same way or i did something wrong?
It is not part of .NET (or the underlying Win32) to move files when they are overwritten. So, yes it is something you are doing.
Probably best to explicitly specify the path.
Also beware of using System.Environment.CurrentDirectory, because that is the systemwide current directory. Another program (like Windows Explorer, or some other program's file open/save dialog) can change that.
If it's a WinForms app you can use Application.StartupPath. If not I think you can use
System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().Location)
View > Solution Explorer > Right Click on 1.txt > Properties > Copy to output directory
NOTE:Your problem has got nothing to do with .NET.
Related
I want to acess a .txt file, which I stored in the resources folder of my project, there where all the imported pictures are stored as well.
I want to use something like a relative path, since every user would have safed his programm somewehere else on his Pc, but there is always the resources folder at the same place, compared to the programm folder.
I tried to used this: displayText = System.IO.File.ReadAllText("Resources\\startmessages.txt"); but this isn't working.
I get this error message: System.IO.DirectoryNotFoundException:, it lists the unrelative path to the .txt there as well, so I don't get, why it cant read it.
Thanks for your Help.
What #ChetanRanpariya is trying to tell you is, that your programm is built in another folder than your folder Resources is sitting to. So you have explictly tell your file Resources\startmessages.txt to copy itself on build process, so it get copied to said another folder. Assuming that you are using Visual Studio, you have to right click on your file and set Copy To Output Directory to true. Its relative folder path (Resources\) will be taken over. You find your build folder somewhere in your bin folder depending on configuration and framework. :)
Current Path where your executable is
Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).FullName
Path to Solution
If you are using Visual Studio and need to access the folders in the solution directory, you can use .Parent method,
Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName
Use of Path.Combine
and once you have the location of your Resource folder, use Path.Combine to get the location to read files / content etc
Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName, "Resources\\startMessages.txt")
I would like to modify the contents of a file placed in my project folder (Specifically "Assets/recentChanges.txt") at runtime. Let's say it contains information about recent changes in application. It is supposed to be read and bound to a textbox control, which later on can be edited by developers and saved back. The problem with content files is that they are copied to output directories and therefore I'm not editing the original file in Assets folder, but the one in bin/Debug/Assets/ folder. And the Embedded Resource build action is not an option since such files cannot be modified.
The question is: how can I modify mentioned .txt file in its original root/Assets/recentChanges.txt location during runtime? All of the below instructions point to similiar build-dependant path:
Environment.CurrentDirectory;
Assembly.GetExecutingAssembly().Location;
Or, in other words, what would be the correct approach for implementing "Recent Changes" section with following conditions:
based on text file in its original project location
possibilty to modify the file at runtime through UI
file copied to the output folder and visible to the user (through textbox) in published application
I am aware that it's probably not the perfect approach, because "recent changes" info could be collected from database or the .txt file could be modified manually and then copied to output locations... but nevertheless I think the issue is somewhat confusing and I would love some help. Thanks!
The compiled and running application doesn't know anything about some "original" location. This location is only important to Visual Studio when the application is built.
If you want to modify the file in this folder you should specify an absolute path to it, i.e. "c:\your_project_folder\file.txt".
The .exe cannot be supposed to know from where it was compiled
under window 7 and 8, if a file generated by the program is placed under the same path of the application executable files (by using 'Application.StartupPath' for example), windows will automatically move it to the 'virtualstore' folder.
For this situation, inside the program if I check the existence of the file using:
if file.exists(application.startuppath & file), it will return True even though the file has been moved to the virtualstore.
For some users however, they somehow disable the file visualization, so the file will stay under the program folder. My question is, how do I know for sure which folder this file is under by code?
Edit:
the question is more like: how to check if a file exists under application folder, 'File.Exists' doesn't work well for this situation, as it always return True.
By checking the VirtualStore location.
VirtualStore is a subfolder of %localappdata%(Environment.SpecialFolder.LocalApplicationData). Then add the folder of your application to it (starting from Program Files), and the filename. Now check whether the file exists or not.
I have a application with one folder which i added by right clicking the project, selecting add folder. Inside this folder i have xml files which are set to build action:content, copy to output directory: copy if newer (i have tried setting to embedded resource) As well as this i have a few text files and so on.
In my bin/debug output directory i have the exe, the folder with the xml, the stand alone .txt files and so on. My problem is, if i send the exe to my friend to try he always gets an exception thrown.
Say he puts the exe on the desktop, my programme at some point reads the filenames of the xml files in the folder. It uses the following code to do so
String[] filePaths = Directory.GetFiles(#"DataSources\");
I assume that because of this, when the exe runs from the desktop, it expect the folder of .xml files to be in the same place? I have the same type of exception when trying to read the .txt files too. What am i doing wrong here?
Thanks for your time
When reading from files using relative paths you get the one relative to the applications current directory. tip: In C# you can see what directory that is using Environment.CurrentDirectory.
So if you create a shortcut on your desktop, you need to make sure you right click the shortcut and set its "Start in"-folder to the directory of your application. That way its current directory will be set when its started and relative paths will be relative to that path and not the path of the shortcut.
If you actually moved the exe file to the desktop you also need to move any resources that it needs, so if it wants a folder named "datasources" you would have to move that folder as well, or set the current directory when you start the application.
Have you tried something like: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.executablepath.aspx or http://msdn.microsoft.com/en-us/library/system.environment.currentdirectory.aspx ?
So
Directory.GetFiles(environment.currentdirectory + #"\DataSources\");
I need some help with paths please!
Basically I have a project with the following folder structure:
Project (root directory which contains the .sln file etc.)
Project/MyProj (contains the code)
Project/MyProjTest (the test folder)
Project/TestResults
Now with this Project I need to have a common folder where I can stick a bunch of files for use with the Application without having to copy the files to multiple locations etc. What is the best way to do this? Ideally I would like to have the folder as Project/ResourcesFolder, so that both the Code folder and Test folder can access it. Now if this is the case how do I call this folder from within C#? I've tried Application.StartupPath, Environment.GetCurrentDirectory but they both just return the CURRENT folder which is not what I want.
Thanks in advance.
You can add a solution folder to your solution and place common files in it.
You'll have to copy the files, you'll want your program to operate the same way after it is deployed. The simplest way to do so is by adding them to your project. In the Properties window, set Build Action = None, Copy to Output Directory = Copy if Newer. The latter setting ensures that you don't waste time copying the files over and over again.
This ensures that the files will be present in the same directory as your EXE. Both when you debug and after you deploy it. Simply use Application.StartupPath to locate them. Creating the Setup project for the app is now very simple as well.
Note that if the files are small you really want to embed them as resources.
.. goes one directory up. That is, from Project/MyProjTest you could access Project/MyProj via ../MyProj.
Use Server.MapPath("~") to get to the root folder of your application. From there you can get to wherever you need.