C# 不通过FTP 将本地文件拷贝到服务器(代码)
2个回答
展开全部
System.IO.相关的类可以完成这个任务。下面是我写的代码片段,仅供参考。注意:首先你要连接到服务器。
祝你好运!呵呵!
代码片段如下:
1 连接到服务器片段:
/// 可以调用它执行连接服务器的操作
public static uint WNetAddConnection(string username, string password, string remoteName, string localName)
{
NetResource netResource = new NetResource();
netResource.dwScope = 2; //RESOURCE_GLOBALNET
netResource.dwType = 1; //RESOURCETYPE_ANY
netResource.dwDisplayType = 3; //RESOURCEDISPLAYTYPE_GENERIC
netResource.dwUsage = 1; //RESOURCEUSAGE_CONNECTABLE
netResource.lpLocalName = localName;
netResource.lpRemoteName = remoteName.TrimEnd('\\');
//netResource.lpRemoteName = lpComment;
//netResource.lpProvider = null;
uint result = WNetAddConnection2(netResource, password, username, 0);
return result;//HGDVTES22
}
2 操作服务器上的文件片段:
try
{
// 弹出新建文件夹窗口,并将自己(指针)传入到子窗口中去
NewFile_Folder newFolderWin = new NewFile_Folder(this);
newFolderWin.ShowDialog();
// 判断文件名是否为 空字符串 或者 null
if (!"".Equals(this.strNewFileName))
{
if (this.tvwServerFileSys.SelectedNode != null)
{
string strServerRoot = this.txtSourceServer.Text.Trim();
strServerRoot = strServerRoot + txtServerFileSysPath.Text.Substring(0, txtServerFileSysPath.Text.LastIndexOf('\\') + 1);
if (System.IO.Directory.Exists(this.tvwServerFileSys.SelectedNode.Name))
{
// 新建文件/文件夹节点
TreeNode newNode = new TreeNode(this.strNewFileName);
newNode.Name = this.tvwServerFileSys.SelectedNode.Name + "\\" + this.strNewFileName;
this.tvwServerFileSys.SelectedNode.Nodes.Insert(this.tvwServerFileSys.SelectedNode.Nodes.Count, newNode);
// 在服务器上建立 文件/文件夹
if (this.strNewType == "file")
{
System.IO.File.Create(newNode.Name);
}
else if (this.strNewType == "folder")
{
System.IO.Directory.CreateDirectory(newNode.Name);
}
else
{
throw new Exception("没有选择新建类型");
}
}
else
{
throw new Exception("清选择目录节点新建文件夹");
}
}
else
{
throw new Exception("请选择 目录节点");
}
}
else
{
throw new Exception("新文件名称是空字符串");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
祝你好运!呵呵!
代码片段如下:
1 连接到服务器片段:
/// 可以调用它执行连接服务器的操作
public static uint WNetAddConnection(string username, string password, string remoteName, string localName)
{
NetResource netResource = new NetResource();
netResource.dwScope = 2; //RESOURCE_GLOBALNET
netResource.dwType = 1; //RESOURCETYPE_ANY
netResource.dwDisplayType = 3; //RESOURCEDISPLAYTYPE_GENERIC
netResource.dwUsage = 1; //RESOURCEUSAGE_CONNECTABLE
netResource.lpLocalName = localName;
netResource.lpRemoteName = remoteName.TrimEnd('\\');
//netResource.lpRemoteName = lpComment;
//netResource.lpProvider = null;
uint result = WNetAddConnection2(netResource, password, username, 0);
return result;//HGDVTES22
}
2 操作服务器上的文件片段:
try
{
// 弹出新建文件夹窗口,并将自己(指针)传入到子窗口中去
NewFile_Folder newFolderWin = new NewFile_Folder(this);
newFolderWin.ShowDialog();
// 判断文件名是否为 空字符串 或者 null
if (!"".Equals(this.strNewFileName))
{
if (this.tvwServerFileSys.SelectedNode != null)
{
string strServerRoot = this.txtSourceServer.Text.Trim();
strServerRoot = strServerRoot + txtServerFileSysPath.Text.Substring(0, txtServerFileSysPath.Text.LastIndexOf('\\') + 1);
if (System.IO.Directory.Exists(this.tvwServerFileSys.SelectedNode.Name))
{
// 新建文件/文件夹节点
TreeNode newNode = new TreeNode(this.strNewFileName);
newNode.Name = this.tvwServerFileSys.SelectedNode.Name + "\\" + this.strNewFileName;
this.tvwServerFileSys.SelectedNode.Nodes.Insert(this.tvwServerFileSys.SelectedNode.Nodes.Count, newNode);
// 在服务器上建立 文件/文件夹
if (this.strNewType == "file")
{
System.IO.File.Create(newNode.Name);
}
else if (this.strNewType == "folder")
{
System.IO.Directory.CreateDirectory(newNode.Name);
}
else
{
throw new Exception("没有选择新建类型");
}
}
else
{
throw new Exception("清选择目录节点新建文件夹");
}
}
else
{
throw new Exception("请选择 目录节点");
}
}
else
{
throw new Exception("新文件名称是空字符串");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询