c# wpf System.ArgumentException

Zaug 306 Reputation points
2022-04-11T13:24:15.203+00:00

hello. i workin' a project on vs2022 and i have a problem with filestreams.

using (StreamReader sr = new StreamReader(@"E:\MUHAMMED\Idea Engine\Idea Engine\Idea Engine\bin\Debug\projectDataSource\projekonumveadi.txt"))//i give text from a file (text = a file location)
{
projeOtoOkuma = sr.ReadToEnd();//set variable to file's text.
using (StreamReader srr = new StreamReader(projeOtoOkuma.ToString()))//give the file location and open it.
{
MessageBox.Show(projeOtoOkuma);
FileStream fs = File.Open(srr.ReadToEnd(), FileMode.Open, FileAccess.Read);//error on here = There are invalid characters in the path
savedCanvas = XamlReader.Load(fs) as Canvas;
fs.Close();
sp.Children.Remove(canvasmain);
sp.Children.Add(savedCanvas);
savedCanvas.PreviewMouseUp += canvasmain_PreviewMouseUp;
savedCanvas.PreviewMouseMove += canvasmain_PreviewMouseMove;
}
}

I don't understand why this problem occurs.

XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
641 questions
0 comments No comments
{count} votes

Accepted answer
  1. Michael Taylor 41,551 Reputation points
    2022-04-11T13:46:27.527+00:00

    Firstly I'd like to recommend that you don't use the full file path. This may work in your debug app but it won't work elsewhere. Use a relative path from where your binary is. I'm guessing your project name is Idea Engine and you have a subfolder called projectDataSource where the file projekonumveadi.txt resides.

    using (StreamReader sr = new StreamReader(@"projectDataSource\projekonumveadi.txt"))
    {
    };
    

    You are reading the entire text so I'm making an assumption that this text file just contains a path to another file. A StreamReader is overkill if you want all the text.

    string projectText = File.ReadAllText(@"projectDataSource\projekonumveadi.txt");
    

    At this point you're making the assumption that the only thing in the file is the path to another file. If this is true then you can subsequently read the next file.

    string nextProjectText = File.ReadAllText(projectText);
    

    Again, assuming this new file contains only a single filename then you can open it using a stream

    using (FileStream fs = File.OpenRead(nextProjectText))
    {
       savedCanvas = XamlReader.Load(fs) as Canvas;
    };
    

    It is here that I'm confused as you're code is saying the original text file contains a single line that is the name of a second file. That second file contains a single line that is the name of a third file. It is the third file that contains your data. This would be a very, very odd design to me. At a minimum either of the earlier files would contain other data as well. Furthermore it wouldn't make sense to have 1 file point to the next just to point to the next. You should verify your file structure is what you expect.

    File 1.txt
    ----------
    file 2.txt
    
    File 2.txt
    ---------
    file 3.dat
    
    File 3.dat
    ----------
    canvas data
    

    At this point I believe your actual error is that the second file contains more than just a filename and hence why you are getting an error.

    0 comments No comments

0 additional answers

Sort by: Most helpful