Joshua12
Joshua12

Reputation: 21

Writing a string into a text file from resources

I'm trying to write a string called "a" into a text file from resources which is called "file.txt". Anyway, I always get "Invalid Characters in path" error. String "a" is obtained by string.join of "\r\n" with an array called "lines" with has no empty entries.

I've tried using a regex to replace invalid characters but it has not worked.

I'd like to string a to be saved into the text.

        string a = string.Join("\r\n", lines);
        string ee = Regex.Replace(a, @"[\[\]\\\^\$\.\|\?\*\+\(\)\{\}%,;><!@#&\-\+/]", "");
        System.IO.File.WriteAllText(Properties.Resources.file, ee);

Upvotes: 1

Views: 479

Answers (2)

DaGrooveNL
DaGrooveNL

Reputation: 187

You can't modify your Properties.Resources files like this. The error you are getting is because the path (Properties.Resources.xxxx) you are referring to is not a valid path.

Try and save the string to an actual file e.g:

    string a = string.Join("\r\n", lines);
    string ee = Regex.Replace(a, @"[\[\]\\\^\$\.\|\?\*\+\(\)\{\}%,;><!@#&\-\+/]", "");
    System.IO.File.WriteAllText("output.txt", ee);

This would create a file called 'output.txt' in the current directory your application is running. Make sure the application does have write permissions in this directory.

Alternatively, you could make use of Properties.Settings, where you can define a variable and perform read/write operations to it during runtime.

Upvotes: 0

Keith Nicholas
Keith Nicholas

Reputation: 44288

the problem is with Properties.Resources.file it has illegal characters. a is fine, you do not have to do any regex on it to solve this problem.

If you don't know what Properties.Resources.file is, try breakpointing on that line and checking what Properties.Resources.file is set to

Upvotes: 1

Related Questions