开发者

How to save a file without prompting the user for a name/path?

I'm trying to open a stream to a file.

First I need to save a file to my desktop and then open a stream to that file.

This code works well (from my previous project) but in this case, I don't want to prompt the user to pick the save location or even the name of the file. Just save it and open the stream:

Stream myStream;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
    if ((myStream = saveFileDialog1.OpenFile()) != null)
    {
        PdfWriter.GetInstance(document, myStream);

Here's my code for the newer project (the 开发者_如何学Creason for this question):

namespace Tutomentor.Reporting
{
    public class StudentList
    {
        public void PrintStudentList(int gradeParaleloID)
        {
            StudentRepository repo = new StudentRepository();
            var students = repo.FindAllStudents()
                               .Where(s => s.IDGradeParalelo == gradeParaleloID);

            Document document = new Document(PageSize.LETTER);
            Stream stream;

            PdfWriter.GetInstance(document, stream);
            document.Open();
            foreach (var student in students)
            {
                Paragraph p = new Paragraph();
                p.Content = student.Name;
                document.Add(p);
            }

        }
    }
}


Use Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) to get the desktop directory.

string fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
                               "MyFile.pdf");
using(var stream = File.OpenWrite(fileName))
{
    PdfWriter.GetInstance(document, stream);
}


// However you initialize your instance of StudentList
StudentList myStudentList = ...;

using (FileStream stream = File.OpenWrite(@"C:\Users\me\Desktop\myDoc.pdf")) {
    try {
        myStudentList.PrintStudentList(stream, gradeParaleloID);
    }
    finally {
        stream.Close();
    }
}

You should pass the stream into your method:

public void PrintStudentList(Stream stream, int gradeParaleloID) { ... }

EDIT

Even though I hard coded a path above, you shouldn't do that, use something like this to get the path to your desktop:

Environment.GetFolderPath(Environment.SpecialFolder.Desktop);


If this is a local (e.g. Windows/console) application just make the stream a FileStream to whatever path you want (check this for info on how to get the desktop folder path). If the user running the application has write permitions to that file it will be created/saved there.

If this is a web (e.g. ASP.Net) application you won't be able to save the file directly in the client machine without prompting the user (for security reasons).


Stream myStream = new FileStream(@"c:\Users\[user]\Desktop\myfile.dat", FileMode.OpenOrCreate);

Your FileMode may differ depending on what you're trying to do. Also I wouldn't advise actually using the Desktop for this, but that's what you asked for in the question. Preferably, look into Isolated Storage.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜