windows form 里有没有保存文件的方法(c#) windows form 里有没有像fileupload控件一样的保存文件的方法(c#),是在哪个控件上的方法? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 好像没有,filestream有write方法可以保存文件。 自己按不同文件格式自行写入.好像Graphics / Bitmap有Save方法 在windows form里怎么将文件上传到服务器? Stream.Read() To convert the file into Stream. Then save it to DB by using insert method with parameter. 你要传到LAN的服务器的共享文件夹,还是FTP服务器,还是其他的??? 当然是传到虚拟主机上的ftp服务器空间了。 貌似没有现成的控件可用,可以做个简单的FTP控件 C# WinForm 文件上传下载**//// <summary> /// WebClient上传文件至服务器 /// </summary> /// <param name="fileNamePath">文件名,全路径格式</param> /// <param name="uriString">服务器文件夹路径</param> private void UpLoadFile(string fileNamePath,string uriString) { //string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("\\") + 1); NewFileName = DateTime.Now.ToString("yyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + fileNamePath.Substring(fileNamePath.LastIndexOf(".")); string fileNameExt = fileName.Substring(fileName.LastIndexOf(".") + 1); if(uriString.EndsWith("/") == false) uriString = uriString + "/"; uriString = uriString + NewFileName; /**//// 创建WebClient实例 WebClient myWebClient = new WebClient(); myWebClient.Credentials = CredentialCache.DefaultCredentials; // 要上传的文件 FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read); //FileStream fs = OpenFile(); BinaryReader r = new BinaryReader(fs); try { //使用UploadFile方法可以用下面的格式 //myWebClient.UploadFile(uriString,"PUT",fileNamePath); byte[] postArray = r.ReadBytes((int)fs.Length); Stream postStream = myWebClient.OpenWrite(uriString,"PUT"); if(postStream.CanWrite) { postStream.Write(postArray,0,postArray.Length); } else { MessageBox.Show("文件目前不可写!"); } postStream.Close(); } catch { MessageBox.Show("文件上传失败,请稍候重试~"); } } /**//// <summary> /// 下载服务器文件至客户端 /// </summary> /// <param name="URL">被下载的文件地址,绝对路径</param> /// <param name="Dir">另存放的目录</param> public void Download(string URL,string Dir) { WebClient client = new WebClient(); string fileName = URL.Substring(URL.LastIndexOf("\\") + 1); //被下载的文件名 string Path = Dir+fileName; //另存为的绝对路径+文件名 try { WebRequest myre=WebRequest.Create(URL); } catch { //MessageBox.Show(exp.Message,"Error"); } try { client.DownloadFile(URL,Path); } catch { //MessageBox.Show(exp.Message,"Error"); } } 不知道这个对你是否有帮助,楼主请看看! 通过System.IO来操作。//读取文件private static void ReadFile(){ using(FileStream fs = new FileStream("d:\\Service.Txt",FileMode.Open,FileAccess.Read)) { StreamReader sr = new StreamReader(fs,Encoding.Default); Console.WriteLine(sr.ReadToEnd()); sr.Close(); }}//写文件private static void WriteFile(){ StringBuilder sb = new StringBuilder(); using(FileStream fs = new FileStream("c:\\CreateText.txt",FileMode.Create,FileAccess.Write)) { StreamWriter sr = new StreamWriter(fs); for(int i=0;i<10000;i++) sb.Append(i.ToString("0000")+","); sr.Write(sb.ToString(0,sb.Length-1)); sr.Close(); }} c#中数据库access中查询日期段 请教一下Codesmith获取数据库连接串的问题 微软的软件开发平台如.NET系列,是用什么语言开发的 怎么在winform中一个cs的类里面获取工程所在路径啊 急,在线等,谢谢,大侠们快进 高分请教大家,winform怎么读取cookie值(急) 如何屏蔽FORM中控件的事件捕捉? onPaint无法重写,出错.提示没有找到适合的方法重写 Repeater控件 c#怎么实现excel作业自动批阅 请教:在panel上画图后,最大化窗口,显示不出所画的内容。 webclient tcp连接限制问题
好像Graphics / Bitmap有Save方法
你要传到LAN的服务器的共享文件夹,还是FTP服务器,还是其他的???
**//// <summary>
/// WebClient上传文件至服务器
/// </summary>
/// <param name="fileNamePath">文件名,全路径格式</param>
/// <param name="uriString">服务器文件夹路径</param>
private void UpLoadFile(string fileNamePath,string uriString)
{
//string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf("\\") + 1);
NewFileName = DateTime.Now.ToString("yyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + fileNamePath.Substring(fileNamePath.LastIndexOf("."));
string fileNameExt = fileName.Substring(fileName.LastIndexOf(".") + 1);
if(uriString.EndsWith("/") == false) uriString = uriString + "/";
uriString = uriString + NewFileName;
/**//// 创建WebClient实例
WebClient myWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials;
// 要上传的文件
FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
//FileStream fs = OpenFile();
BinaryReader r = new BinaryReader(fs);
try
{
//使用UploadFile方法可以用下面的格式
//myWebClient.UploadFile(uriString,"PUT",fileNamePath);
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(uriString,"PUT");
if(postStream.CanWrite)
{
postStream.Write(postArray,0,postArray.Length);
}
else
{
MessageBox.Show("文件目前不可写!");
}
postStream.Close();
}
catch
{
MessageBox.Show("文件上传失败,请稍候重试~");
}
} /**//// <summary>
/// 下载服务器文件至客户端
/// </summary>
/// <param name="URL">被下载的文件地址,绝对路径</param>
/// <param name="Dir">另存放的目录</param>
public void Download(string URL,string Dir)
{
WebClient client = new WebClient();
string fileName = URL.Substring(URL.LastIndexOf("\\") + 1); //被下载的文件名
string Path = Dir+fileName; //另存为的绝对路径+文件名
try
{
WebRequest myre=WebRequest.Create(URL);
}
catch
{
//MessageBox.Show(exp.Message,"Error");
}
try
{
client.DownloadFile(URL,Path);
}
catch
{
//MessageBox.Show(exp.Message,"Error");
}
} 不知道这个对你是否有帮助,楼主请看看!
//读取文件
private static void ReadFile()
{
using(FileStream fs = new FileStream("d:\\Service.Txt",FileMode.Open,FileAccess.Read))
{
StreamReader sr = new StreamReader(fs,Encoding.Default);
Console.WriteLine(sr.ReadToEnd());
sr.Close();
}
}//写文件
private static void WriteFile()
{
StringBuilder sb = new StringBuilder();
using(FileStream fs = new FileStream("c:\\CreateText.txt",FileMode.Create,FileAccess.Write))
{
StreamWriter sr = new StreamWriter(fs);
for(int i=0;i<10000;i++)
sb.Append(i.ToString("0000")+",");
sr.Write(sb.ToString(0,sb.Length-1));
sr.Close();
}
}