how to access/create a .txt file with a "relative" path - c#

I'm creating an app that can take a text from one file, read it, edit the text via a string, then write the edited string in a new text file.
My problem is that the file can't be found. In the following example I placed the file in the folder indicated in the screenshot and in another attempt in the 'bin' file, both without success and identical error message.
What I was trying to a achieve is the the following (as I am aware I could write the whole path from "C:\"), the text file should be somewhere in the application's directory so that the whole app can be moved without having to re-write the path.
So I need a way to write a "relative" path, if such thing is possible. this should pls work with creating a file similarly as well.
screenshot of the error message
visual studio screenshot with code and file location
thanks in advance.

Summary / Explanation: The file is not found because the directory you have set is not the directory of the file. If the file is in the same directory as to the C# file you are currently working on, then there's no need to add a folder's directory into it but rather just type the filename. You can also consider what's written below this paragraph.
FIRST:
Click InputText.txt
SECOND:
Right Click and Choose "Properties" or just press Alt + Enter
THIRD:
Set the "Build Action" property to "Embedded Resource"
FOURTH:
Set the "Copy to Output Directory" property to "Copy always"
LASTLY:
Change the Readline's first parameter to -> #"InputText.txt"
...
foreach (string line in File.ReadLines(#"InputText.txt", Encoding.UTF8))
{
originalTextInLines.Add(line);
}
...
This is the sample output (in my case of trying this).
This is the file that the example application above
The file is in the same directory the Program.cs is found that the file may be addressed by typing its filename only.

Related

How to modify and export files included in project?

For example, lets say I have a text file with one line of code named Template.txt included in the project folder.
Since the text file is included with the project, is there another way to specify this file or would I still have to specify the full file path?
If it's included in the project and if you select the option copy always from the property Copy to Output Directory (Right click on the file and select properties in VS), you will have the file available in the bin/ folder after compilation. So, you would not need the entire path. Just the name of the file.
If i understand your question,
you can put the file on the same folder where the exe file is and specify only the name of the file [Template.txt].

Inserting a file into the project - trying to eliminate OpenFileDialog

I have a file (.doc file), that is required for the software to work properly. The program starts with the user opening the file manually (OpenFileDialog) and continuing on normally. Is there a way to have the file already within the project so the user doesn't have to manually open the file each time the project runs? Thanks!
Here's a link to the tutorial I used to make this project. https://www.youtube.com/watch?v=wYse5nh5nB8
You can Add your document with right click context menu Add->Existing Item, in your project.
Select Properties of document by right clicking and Change Build Action from Compile to Embedded Resource and Copy to Output Directory" of the report file to "Copy always".
You can get the path of this file as
string fileName = "YourDocument.doc";
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
once you have file path you can read its contents.

Txt project file writing problems C#

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.

FileNotFound Exception While Opening File

I have a project that reads several text files into a List via StreamReader, I have the files added to my solution under Resources, when I try to reference the file using StreamReader, I get a "FileNotFound" exception.
The files are being copied over to bin\debug\Resources, and the error says it's trying to locate them under bin\debug.. How do I reference them without using the literal path? (e.g. C:\users\etc) since when I compile it won't run on another person's PC if I reference actual path.
Code that calls text file:
using (sr = new StreamReader("FileName.txt"))
{
while (!sr.EndOfStream)
Names.Add(sr.ReadLine());
}
Under the text files properties I have it set to "Copy Always" and under Build Action it's set to "Embedded Resource".
Basically my main goal is to compile the project into an exe with the text files being referenced internally (their contents aren't changed by the program), so my application will be portable.
Error is clear. Change code to:
using (sr = new StreamReader("Resources\FileName.txt"))
{
while (!sr.EndOfStream)
Names.Add(sr.ReadLine());
}
In your situation, the error will be removed by just replacing the path Resources\FileName.txt
In other case, as you mentioned that you want to make a portable .exe. Then you need to embed files in your application. Now see how to embed files in your .exe :
Expand Properties in Solution Explorer
Double click on Resources
On left top of Resources tab, there will be a combo box
Choose Files
Now just drag and drop your files there
To embed files, you can go to properties of every file by right
clicking on it. Choose Embedded in .resx from Persistence
property.
To use the file you can use Properties.Resources.YourFile.
For more details, follow the link http://msdn.microsoft.com/en-us/library/7k989cfy(v=vs.80).aspx

Reading Xml file path from Windows for application

I have my files place in a directory name XMLpackage which is located under the Root of my project
At run time i need to read the file by passing the Path.
By putting my file in debug folder i was able to read. But in this specific XMLpackage folder i can not read it.
How to do this?
Change the file property 'Copy to Output' with value other than 'Do not copy'
To get the file path try,
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XMLpackage", "file1.xml");
Try this. Right click the file in Solution Explorer and click on Properties. Change the "Copy to Output Directory" to "Copy Always" as shown in the image.
You can access the file like
string f = System.IO.File.ReadAllText(#"XMLpackage\TableScripts.xml");

Categories