将文件保存到特定路径

将文件保存到特定路径

问题描述:

我有使用savefiledialog将网格视图保存为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); 
    } 
} 

我怎么能SVE它不使用savefiledialog?

+0

这是什么语言? – 2011-05-20 02:38:53

+0

这是C#。重新标记。 – Ryan 2011-05-20 02:40:09

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

将做同样的事情,但具体路径。你可以阅读更多关于file access on MSDN

+0

谢谢你minitech,但它没有奏效。我试过这个:使用(StreamWriter output = new StreamWriter(@“C:\ myreport.html”)) { \t provider.Export(document,output); },错误是:无法从system.io.streamwriter转换为system.io.stream。 provider.export接受一个文档和一个流 – loraine96 2011-05-20 05:30:50

+0

你确定吗? StreamWriter从Stream继承。虽然你可以尝试一个明确的演员。 – Ryan 2011-05-20 15:00:10

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

谢谢你,但它没有奏效。 Export()接受Stream作为第二个参数。 – loraine96 2011-05-20 05:38:31

+0

@ lorane96 - 够简单。已更新以提供流。 – 2011-05-20 10:18:08

+0

@RitchMelton你能告诉我为什么它没有将数据保存到特定的路径'string FILE = @“F:\\”+ textBox1.Text;''System.IO.File.WriteAllLines(FILE +“.txt”,内容);' – AHF 2014-01-12 16:59:33

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

谢谢Ajay,但它没有工作... – loraine96 2011-05-20 05:26:54

+0

你看到任何错误?如果是的话,你能否提到你看到的错误 – AjayR 2011-05-20 08:57:34

我想这:

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("C:\\SAMPLE.html", FileMode.Open, FileAccess.ReadWrite); 
provider.Export(document, output); 

但有一个安全异常错误。文件操作不允许。访问路径“C:\ SAMPLE.html”被拒绝。这怎么能解决?