将文件保存到特定路径

时间:2011-05-20 02:35:16

标签: c# .net file autosave

我有使用savefiledialog将gridview保存为html文件的代码。我想将它保存到特定的路径(不使用savefiledialog)...我该怎么做?

这是我的代码:

SaveFileDialog dialog = new SaveFileDialog();
dialog.DefaultExt = "*.html";
dialog.Filter = "WORD Document (*.html)|*.html";

if (dialog.ShowDialog() == true)
{
    RadDocument document = CreateDocument(rgvReportData);

    document.LayoutMode = DocumentLayoutMode.Paged;

    document.Measure(RadDocument.MAX_DOCUMENT_SIZE);
    document.Arrange(new RectangleF(PointF.Empty, document.DesiredSize));
    document.SectionDefaultPageMargin = new Telerik.Windows.Documents.Layout.Padding(2, 2, 2, 2);
    document.SectionDefaultPageOrientation = PageOrientation.Landscape;

    HtmlFormatProvider provider = new HtmlFormatProvider();

    using (Stream output = dialog.OpenFile())
    {
        provider.Export(document, output);
    }
} 

如何在不使用savefiledialog的情况下进行搜索?

4 个答案:

答案 0 :(得分:0)

using(StreamWriter output = new StreamWriter("path\to\your\file")) {
     provider.Export(document, output);
}

会做同样的事情,但会走向一条特定的道路。您可以在file access on MSDN上阅读更多内容。

答案 1 :(得分:0)

using (var output = new FileStream("path", FileMode.Create, FileAccess.Write))
{
    provider.Export(document, output);
}

答案 2 :(得分:0)

String fileName = "youfilename.html"; // give the full path if required
    RadDocument document = CreateDocument(rgvReportData);

    document.LayoutMode = DocumentLayoutMode.Paged;

    document.Measure(RadDocument.MAX_DOCUMENT_SIZE);
    document.Arrange(new RectangleF(PointF.Empty, document.DesiredSize));
    document.SectionDefaultPageMargin = new Telerik.Windows.Documents.Layout.Padding(2, 2, 2, 2);
    document.SectionDefaultPageOrientation = PageOrientation.Landscape;

    HtmlFormatProvider provider = new HtmlFormatProvider();

    Stream output = File.Open(filename, FileMode.Open, FileAccess.ReadWrite);
    provider.Export(document, output);
} 

答案 3 :(得分:0)

OpenFileDialog 显示您选择的特定路径。 您可以设置 SaveFileDialog 的路径,不需要像 dialog.ShowDialog() 那样显示对话框,您只需设置像 dialog.Filename = "file name" 这样的路径。
并将其替换为:

SaveFileDialog dialog = new SavefileDialog(); 
dialog.FileName = "path"; // like "C:\\someFolder\\someFile.html"

你可以试试