自己用c#写的保存文件
2008-01-05 17:48
267 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.IO;
using System.Windows.Forms;
public class SaveHelper
{
public static bool IsValidFileName(string fileName)
{
System.Diagnostics.Debug.Assert(fileName != null);
bool isValid = true;
try
{
FileInfo fileInfor = new FileInfo(fileName);
if (fileInfor.Name != fileName)
{
isValid = false;
}
}
catch (ArgumentException ex)
{
isValid = false;
}
catch (PathTooLongException ex)
{
isValid = false;
}
catch (NotSupportedException ex)
{
isValid = false;
}
catch (Exception e)
{
//isValid = true;
}
return isValid;
}
public static bool IsReadOnly(string fileName)
{
bool isReadonly = false;
try
{
FileInfo fileInfor = new FileInfo(fileName);
if (fileInfor.Exists)
{
isReadonly = fileInfor.IsReadOnly;
}
else
{
System.Diagnostics.Debug.Fail("File is not exist.");
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.Fail(ex.Message);
}
return isReadonly;
}
private static string AddExtension(string filename, string ext,bool replace)
{
int ind = filename.LastIndexOf(".");
if (ind < 0)
{
return filename + "." + ext;
}
else if (filename.Substring(ind + 1).ToLower() != ext.ToLower())
{
if (replace)
return filename.Substring(0, ind) + "." + ext;
else
return filename + "." + ext;
}
else
{
return filename;
}
}
public static void SavingWidgetDialog(string Name, string uid)
{
if ((Name == null) || (Name.Trim().Length == 0)
|| (uid == null) || uid.Trim().Length == 0)
return;
Stream stream = null;
SaveFileDialog dialog = new SaveFileDialog();
dialog.AddExtension = true;
dialog.DefaultExt = "bidml";
string filter = string.Format("(*.x)|*.x|{2} (*.*)|*.*");
dialog.Filter = filter;
dialog.FilterIndex = 1;
dialog.RestoreDirectory = true;
dialog.OverwritePrompt = false;
dialog.Title = "保存";
string tempfilename =AddExtension(Name, dialog.DefaultExt,true);
if (WidgetSaveHelper.IsValidFileName(tempfilename))
{
dialog.FileName = tempfilename;
}
dialog.FileOk += new CancelEventHandler(dialog_FileOk);
if (dialog.ShowDialog() == DialogResult.OK)
{
try
{
if ((stream = dialog.OpenFile()) != null)
{
using (stream)
{
stream.Close();
}
}
}
catch (Exception ex)
{
if (stream != null)
stream.Close();
string errorMessage = "lala";
string caption = "lalala";
try
{
File.Delete(dialog.FileName);
}
catch (Exception e)
{
}
}
}
}
private static void dialog_FileOk(object sender, CancelEventArgs e)
{
SaveFileDialog dialog = sender as SaveFileDialog;
dialog.FileName = AddExtension(dialog.FileName, dialog.DefaultExt, false);
if (File.Exists(dialog.FileName))
{
string overwriteMessage = "lala";
string readonlyMessage = "lalala";
string caption = "la";
//window.Form.StartPosition = FormStartPosition.CenterScreen;
if (SaveHelper.IsReadOnly(dialog.FileName))
{
e.Cancel = true;
}
else
{
//DialogResult dr = window.Form.ShowDialog();
if (dr != DialogResult.OK)
{
e.Cancel = true;
}
}
}
4000
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.IO;
using System.Windows.Forms;
public class SaveHelper
{
public static bool IsValidFileName(string fileName)
{
System.Diagnostics.Debug.Assert(fileName != null);
bool isValid = true;
try
{
FileInfo fileInfor = new FileInfo(fileName);
if (fileInfor.Name != fileName)
{
isValid = false;
}
}
catch (ArgumentException ex)
{
isValid = false;
}
catch (PathTooLongException ex)
{
isValid = false;
}
catch (NotSupportedException ex)
{
isValid = false;
}
catch (Exception e)
{
//isValid = true;
}
return isValid;
}
public static bool IsReadOnly(string fileName)
{
bool isReadonly = false;
try
{
FileInfo fileInfor = new FileInfo(fileName);
if (fileInfor.Exists)
{
isReadonly = fileInfor.IsReadOnly;
}
else
{
System.Diagnostics.Debug.Fail("File is not exist.");
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.Fail(ex.Message);
}
return isReadonly;
}
private static string AddExtension(string filename, string ext,bool replace)
{
int ind = filename.LastIndexOf(".");
if (ind < 0)
{
return filename + "." + ext;
}
else if (filename.Substring(ind + 1).ToLower() != ext.ToLower())
{
if (replace)
return filename.Substring(0, ind) + "." + ext;
else
return filename + "." + ext;
}
else
{
return filename;
}
}
public static void SavingWidgetDialog(string Name, string uid)
{
if ((Name == null) || (Name.Trim().Length == 0)
|| (uid == null) || uid.Trim().Length == 0)
return;
Stream stream = null;
SaveFileDialog dialog = new SaveFileDialog();
dialog.AddExtension = true;
dialog.DefaultExt = "bidml";
string filter = string.Format("(*.x)|*.x|{2} (*.*)|*.*");
dialog.Filter = filter;
dialog.FilterIndex = 1;
dialog.RestoreDirectory = true;
dialog.OverwritePrompt = false;
dialog.Title = "保存";
string tempfilename =AddExtension(Name, dialog.DefaultExt,true);
if (WidgetSaveHelper.IsValidFileName(tempfilename))
{
dialog.FileName = tempfilename;
}
dialog.FileOk += new CancelEventHandler(dialog_FileOk);
if (dialog.ShowDialog() == DialogResult.OK)
{
try
{
if ((stream = dialog.OpenFile()) != null)
{
using (stream)
{
stream.Close();
}
}
}
catch (Exception ex)
{
if (stream != null)
stream.Close();
string errorMessage = "lala";
string caption = "lalala";
try
{
File.Delete(dialog.FileName);
}
catch (Exception e)
{
}
}
}
}
private static void dialog_FileOk(object sender, CancelEventArgs e)
{
SaveFileDialog dialog = sender as SaveFileDialog;
dialog.FileName = AddExtension(dialog.FileName, dialog.DefaultExt, false);
if (File.Exists(dialog.FileName))
{
string overwriteMessage = "lala";
string readonlyMessage = "lalala";
string caption = "la";
//window.Form.StartPosition = FormStartPosition.CenterScreen;
if (SaveHelper.IsReadOnly(dialog.FileName))
{
e.Cancel = true;
}
else
{
//DialogResult dr = window.Form.ShowDialog();
if (dr != DialogResult.OK)
{
e.Cancel = true;
}
}
}
4000
}
}
相关文章推荐
- C# 文件读取方法,自己写的例子,保存一下,备用
- 关于 c# 操作 Excel文件中遇到的保存出错问题
- 保存下自己的vim配置文件
- C# 配置文件保存为dat文件
- C#代码实现把网页文件保存为mht文件
- C# 修改保存 配置文件的两种方式以及区别
- C#代码实现把网页文件保存为mht文件
- 自己动手写CSDN博客提取器源码分析之一:处理网页保存为txt文件
- FCK在ASP.NET(C#)中的应用,FCK上传文件按照年月的形式保存
- C#项目打开/保存文件夹/指定类型文件,获取路径
- C#窗体中利用richtextbox控件,打开指定word文件,并进行编辑保存等操作,出现Office或者是Interop未定义的解决方案
- [转]C# 注册自己的右键菜单 (文件夹/文件)
- C#中保存GIF文件后透明背景问题的一个解决方法
- C#获取应用程序图标获取后在保存成ICO文件
- C#:读取csv文件,保存为csv文件
- 如何通过C#保存一篇Excel文件?
- 自己动手写中文分词解析器完整教程,并对出现的问题进行探讨和解决(附完整c#代码和相关dll文件、txt文件下载)
- 【C#学习笔记】保存文件
- c# 使用 静态类+xml序列化 保存配置文件
- c#客户端图片上传到服务器端文件并且把路径保存到SQL数据库