Well, I don't if I asked well this question because I have the next problem:
Code
As you can see here, in one of my many WindowsForms I read a specific file.exe as well with file.txt with their own directions, when I run this (Debug) it works perfectly fine because the Folder that storage those files are inside of \WindowsFormsApp1\WindowsFormsApp1\bin\Debug.
Maybe the worst idea, because I want to 'release' this project, once I do this, I get a lot of errors of missing files (Because of the folder) except with certain images that I imported to Resources.rex.
Any suggestion?, like how can I keep all those .txt files with the .exe files, and still reading and running them when I use 'release' from Visual Studio
EDIT:
Error
This is the Release version, some images are intact but when I try to read the .txt files or Run the .exe files I get this message
If you have created exe for that, best suggestion that store those files in C drive as there will be no such folder of debug or release when you export exe. And even instance will be running from another folder which may be not same as you have provided folder path in your application. You should provide a path of folder, if that path is not present then create the folder as below.
string folderPath = "C:\\YourFolderPath\\";
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
This may solve the issue but it is not still correct way to do.
Related
the problem that i have with my application is about how to reference some files, when i load and image, etc, i use Path.GetFullPath(Path.Combine(AppContext.BaseDirectory, "..\\..\\..\\")); and this works fine for debugging and for some files, but doesn't work for some essential files, credentials.txt, config.ini, this files are searched in appdata when i run the published file and i don't know how i should reference them.
I try to generate some initial files so i am sure where they are and that they exists, but for folders that works great with
System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().Location);
with that i can make the folders where the app is run but you can't generate a file without load the content of the original file, and i don't know how to reference them.
Example:
Project.
Root of the project.
utils/key/credentials.txt
When the published file is executed i want to generate and store credentials.txt like this:
Executable (app.exe stored for example in "my documents")
"my documents"/utils/key/credentials.txt
how i do that? when i run the published application you just have the .exe, the dlls, and the resources are embebbed, so the uri doesn't works.
Assuming you have sufficient permissions, you could create a folder in the output folder of your compiled .exe at runtime using the Directory.CreateDirectory method.
If the utils/key/credentials.txt file is part of your deployment, you should set its Build Action to Content and the Copy to Output Directory property to Copy if newer in Visual Studio. This will add the utils and key folders to the output directory of the .exe, which is typically c:<project-folder\bin\Debug or \Release when you build from Visual Studio.
You can get the absolute path of the output directory like this:
string path = System.IO.Path.GetDirectoryName(
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
Depending on your requirements, you may then append the relative path of the file within your app to the absolute path.
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 don't understand why I can read but cannot write to the file that is inside the project. When i selected release it appeared to write to the file, but on debugging mode it doesn't. When i use same function to write list into the file's lines to a different folder it worked always but not on the file that i want.
Example:
Function:
public void WriteLinesFromListToTextFile(List<string> listOfContent, string txtFileName)
{
StreamWriter writer = File.AppendText(txtFileName);
foreach (string Item in listOfContent)
{
writer.WriteLine(Item);
}
writer.Flush();
writer.Close();
}
and later
List<string> exampleList = new List<string>();
tmp.Add("line1");
tmp.Add("line2");
tmp.Add("line3");
WriteLinesFromListToTextFile(exampleList, "TextOnProjectRoot.txt")
In the file properties I selected "Copy Always" and also tried "Copy If Newer".
I ran VS as admin as well.
When I created the file it did copy it to the root folder but just didn't write to it.
I also want to state that there is no exception at all.
Thanks for any help
I'm a little unsure of what you are trying to archive, but I understand you this way:
You have a text file as part of you project. At runtime, you try to write some text to this file. When you application is closed, you expect to see the changes in the text file that's part of your project.
Am I right?
If so, I would guess the problem is as follow: When you first start your application, the text file that is part of your project is copied to the output folder together with your executable. The program runs, and the file is manipulated. But when the application closes, files are never copied back from the output folder to the project folder. That's just not how thing works...
If your program needs access to a file, and you don't specify a path, it uses the current location of the .exe. If this is good enough for your purposes then you just need to know that you can't add it to the solution; like Vegar said, it doesn't work that way. What you CAN do is compile your program, then browse to the folder where the .exe is (debug, release, or other) and run the .exe right from there, then open the text file and review your changes. If you need to create or ensure the file already exists, you can do that in the code as well. You won't be able to include it in your solution but you can still write to the file.
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 have a quick question regarding debug and release in VS 2008.
I have an app i've been working on - its not yet complete but the bulk of the functionality is there. So basically i'm trying to give a copy of it now to the person helping with documentation - just so they can have a play and get the feel for what i've made.
Now the question is how to provide it to them. I was told to just copy the .exe out of the debug/bin folder and put that onto USB. But when testing, if I run this .exe anywhere else (outside of this folder) it crashes. I've now worked out why this is:
var path = ConfigurationManager.AppSettings["PathToUse"];
var files = Directory.GetFiles(path);
throws a null reference, so that App.config file is not being used. If I copy that file in with the .exe it works again.
So actually my question is regarding the best way to manage this situation. What is the best way to provide a working copy to people, and, is there a reference on preparing apps for release - so everything is packaged together and installed in a clean structured folder heirarchy?
If you want to be safe grab everything in the debug/bin folder. If you use the drop down in VS to change to release and then compile the project, there will be fewer files in the release/bin folder because many debug related files are not included.
If there are third party DLLs you are referencing, like if you downloaded log4net or something like that, then you can simply put them in the same folder as the exe. This is called side-by-side deployment, and in my opinion is the simplest and easiest to test.
You can search for XCOPY deployment to learn more about what you are trying to do.
You can take another step of compressing the bin folder into an archive like a zip file to make it easy for the user to extract them, or you could use a tool like WIX or NSIS to create an installer to extract and copy the files.