0

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');
2
Contributors
5
Replies
19
Views
4 Months
Discussion Span
Last Post by rproffitt
0

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");
        }
    }
}
1

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

Edited by jigglymig

Votes + Comments
Thanks for the update.
0

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);
            //}
        }
    }
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.