I am trying to figure out how to read/write to a file without knowing it's exact path. I was using resources and was only reading in the files, but now I need to write to them and there doesn't seem to be an easy way about this. I was looking into dll and using getdirectory, but my directory would change if I debugged and threw the whole thing out of wack. Any help would be appreciated thanks.

my current code is like this:

GetQuestionsAndAnswers(Properties.Resources.CCNA1quiz1);

private void GetQuestionsAndAnswers(string PassedQuiz)
{
     string[] lines = PassedQuiz.Split('\n');

this fails me

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {

            StreamReader strRead = new StreamReader("mytext.txt");
            string myStr = "";

            while ((myStr = strRead.ReadLine()) != null)
                Console.WriteLine(myStr + "\n");
        }
    }
}

nevermind, found it. my copy to output directory was set to copy never. I'll cut the text files from my resources and paste them and do it like this. ended up using resources because the file issue. ty

commented: Thanks for the update. +12

now I can't write. getting this error code:

Severity Code Description Project File Line Suppression State
Error Unable to copy file "C:\Users\clinton\Desktop\ConsoleApp1\ConsoleApp1\TextFile2.txt" to "bin\Debug\TextFile2.txt". Access to the path 'bin\Debug\TextFile2.txt' is denied. ConsoleApp1

here is code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {

            //StreamReader strRead = new StreamReader("TextFile2.txt");
            //string myStr = "";

            //while ((myStr = strRead.ReadLine()) != null)
            //    Console.WriteLine(myStr + "\n");

            //StreamWriter strWriter = new StreamWriter("TextFile2.txt");
            //strWriter.Write("hoho");

            string myStr = "banananananana";

            StreamWriter sr = new StreamWriter("TextFile2.txt") ;
            try
            {
                sr.Write(myStr);
                // some exception occurs here 
                File.SetAttributes("TextFile2.txt", FileAttributes.Hidden);
            }
            finally
            {
                sr.Close();
            }

            //using (StreamWriter sr = new StreamWriter("TextFile2.txt"));
            //{
            //    sr.Write(myStr);
            //    // some exception occurs here 
            //    File.SetAttributes("TextFile2.txt", FileAttributes.Hidden);
            //}
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.