简体   繁体   中英

Portable Stream writer c#

I'm currently making an app that needs to print out to a text file, I'm having trouble making it portable below is what I have so far

FileStream fs = new FileStream((@"|DataDirectory|\print" + fileName + ".txt"), FileMode.Create, FileAccess.Write);
            using (StreamWriter writer = new StreamWriter(fs))
            {
                    write.write("HelloWorld"
            }

The error comes on the first line saying im using an invalid character, I'm assuming that is because of the |DataDirectory| but other than that I don't know how I'd be able to use it on a user's machine?

Thanks in advance

Revise your first line to the following:

var fs=new FileStream(
       string.Format(@"{0}\print\{1}.txt",
       Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
       filename),FileMode.Create,FileAccess.Write);

If you're developing a desktop app, try with this:

string dir = Path.GetDirectory(Assembly.GetExecutingAssembly().Location);
string filename = Path.Combine(dir, "print", filename + ".txt");
FileStream fs = new FileStream(filename,....);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM