asp.net实现创建映射,发送文件,删除映射
2007-08-10 14:55
429 查看
前台页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<INPUT class="inputnormal" id="txtDown" style="WIDTH: 400px; HEIGHT: 24px" type="file"
size="47" name="smlPic" runat="server">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
</form>
</body>
</html>
后台文件:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Diagnostics;
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
try
{
p.Start();
p.StandardInput.WriteLine("net use h: ////IP//共享文件夹名称/"密码/" /user:用户名");//创建映射
p.StandardInput.WriteLine("exit");
p.WaitForExit();
p.Close();
string shortSmlPicFileName = "";
string smlPicFileName = "";
if (txtDown.PostedFile.FileName.Trim() != "")
{
shortSmlPicFileName = GetUniqueFileName(txtDown.PostedFile.FileName);
smlPicFileName = "h:/" + shortSmlPicFileName;
UpLoadPic(txtDown, smlPicFileName);
}
string vSmlPicFileName = (shortSmlPicFileName == "") ? "" : "h:/" + shortSmlPicFileName;
}
catch (Exception ex)
{
}
finally
{
p.Start();
p.StandardInput.WriteLine("net use h: /del");//删除映射
p.StandardInput.WriteLine("exit");
p.WaitForExit();
p.Close();
}
}
private string GetUniqueFileName(string fileName)
{
string newFileName = Guid.NewGuid().ToString();
string ExtName = Path.GetExtension(fileName);
return newFileName + ExtName;
}
private void UpLoadPic(HtmlInputFile inputFile, string fileName)
{
if (inputFile.PostedFile != null)
{
inputFile.PostedFile.SaveAs(fileName);
}
}
}
注意:共享文件夹要有写的权限
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<INPUT class="inputnormal" id="txtDown" style="WIDTH: 400px; HEIGHT: 24px" type="file"
size="47" name="smlPic" runat="server">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
</form>
</body>
</html>
后台文件:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Diagnostics;
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.CreateNoWindow = true;
try
{
p.Start();
p.StandardInput.WriteLine("net use h: ////IP//共享文件夹名称/"密码/" /user:用户名");//创建映射
p.StandardInput.WriteLine("exit");
p.WaitForExit();
p.Close();
string shortSmlPicFileName = "";
string smlPicFileName = "";
if (txtDown.PostedFile.FileName.Trim() != "")
{
shortSmlPicFileName = GetUniqueFileName(txtDown.PostedFile.FileName);
smlPicFileName = "h:/" + shortSmlPicFileName;
UpLoadPic(txtDown, smlPicFileName);
}
string vSmlPicFileName = (shortSmlPicFileName == "") ? "" : "h:/" + shortSmlPicFileName;
}
catch (Exception ex)
{
}
finally
{
p.Start();
p.StandardInput.WriteLine("net use h: /del");//删除映射
p.StandardInput.WriteLine("exit");
p.WaitForExit();
p.Close();
}
}
private string GetUniqueFileName(string fileName)
{
string newFileName = Guid.NewGuid().ToString();
string ExtName = Path.GetExtension(fileName);
return newFileName + ExtName;
}
private void UpLoadPic(HtmlInputFile inputFile, string fileName)
{
if (inputFile.PostedFile != null)
{
inputFile.PostedFile.SaveAs(fileName);
}
}
}
注意:共享文件夹要有写的权限
相关文章推荐
- asp.net实现创建映射,发送文件,删除映射
- asp.net c#实现文件的创建、移动、删除、复制
- asp.net创建、删除、移动文件夹 文件
- asp.net上传文件并创建文件夹和删除文件
- asp.net如何实现删除文件的操作? (转)
- asp.net中对文件的创建,移动,复制,及删除操作
- asp.net ftp 上传,下载,删除文件,创建文件夹,删除文夹,重命名类
- ASP.NET对文件的操作,创建文件,判断文件是否存在,判断文件是否存在删除文件夹
- ASP.NET中对文件的操作 创建 读取 删除 上传 下载(转 备份用)
- ASP.NET中对文件的操作 创建 读取 删除 上传 下载
- asp.net如何实现删除文件的操作?
- asp.net如何实现删除文件的操作?
- asp.net操作xml实现创建,添加,删除
- asp.net文件的,创建,删除,复制,粘贴
- asp.net 文件夹和文件的创建、删除
- asp结合fso实现文件或文件夹创建删除等操作的函数
- asp.net中对文件的创建,移动,复制,及删除操作
- 点击图片,AJAX删除后台图片文件的实现代码(asp.net)
- asp.net如何实现删除文件夹及文件内容操作