delete
delete

Reputation:

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 reason 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);
            }

        }
    }
}

Upvotes: 0

Views: 6248

Answers (4)

mmutilva
mmutilva

Reputation: 18994

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).

Upvotes: 0

bitxwise
bitxwise

Reputation: 3594

// 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);

Upvotes: 0

Samuel Neff
Samuel Neff

Reputation: 74899

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

Upvotes: 4

Martin Doms
Martin Doms

Reputation: 8748

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.

Upvotes: -1

Related Questions