using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TestMVC.Models;
using System.Da
ta.SqlClient;
using System.Collections;
using TestMVC.Class;
using System.IO;
using System.Diagnostics;
using System.Text;
using System.Security.AccessControl;
using System.Runtime.InteropServices;
namespace TestMVC.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
Dictionary<string, string> stackholder = new Dictionary<string, string>();
stackholder.Add("name", "value");
stackholder.Add("name1", "value1");
stackholder.Add("name2", "value2");
stackholder.Add("name3", "value3");
ViewData["stackholder"] = stackholder;
return View();
}
public string CreateDirectory()
{
//string path = "G:\\";
string path = "D:\\iis2";
uint state = 0;
if (!Directory.Exists("Z:"))
{
state = WNetHelper.WNetAddConnection(@"administrator", "xmintop1234qwer", @"\\192.168.1.1\xmintop$", "Z:");
}
if (state.Equals(0))
{
Directory.CreateDirectory(path);//创建本地一个路径
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = " cmd.exe ";
startInfo.Arguments = @" /c E:\o.bat";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = false;
startInfo.RedirectStandardOutput = false;
startInfo.CreateNoWindow = true;
process.StartInfo = startInfo;
process.Start();
//process.WaitForExit();
process.Dispose();
return "";
}
else
{
throw new Exception("添加驱动器错误" + state.ToString());
}
}
public string WriteToFile()
{
//创建文件(user:用户名提取当前登录用户的ID做为远程登录的用户名)
System.IO.StreamWriter sw = new System.IO.StreamWriter(Server.MapPath("~/") + "\\" + "1.bat");
sw.WriteLine("net use x: \\\\192.168.1.225\\testweb1 \"123456\" /user:\"wyb\"");
sw.WriteLine("pause");
sw.Flush();
sw.Close();
//首先清除其他连接会话
Process processClear = new Process();
ProcessStartInfo startInfoClear = new ProcessStartInfo();
startInfoClear.FileName = " cmd.exe ";
startInfoClear.Arguments = @" /c E:\test\Projects\testMVC\TestMVC\clear.bat";
startInfoClear.UseShellExecute = false;
startInfoClear.RedirectStandardInput = false;
startInfoClear.RedirectStandardOutput = false;
startInfoClear.CreateNoWindow = true;
processClear.StartInfo = startInfoClear;
processClear.Start();
//process.WaitForExit();无限期等待执行
processClear.Dispose();
//执行文件
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = " cmd.exe ";
startInfo.Arguments = @" /c E:\test\Projects\testMVC\TestMVC\1.bat";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = false;
startInfo.RedirectStandardOutput = false;
startInfo.CreateNoWindow = true;
process.StartInfo = startInfo;
process.Start();
//process.WaitForExit();
process.Dispose();
return "";
}
public string DelWriteToFile()
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(Server.MapPath("~/") + "\\" + "-1.bat");
sw.WriteLine("net use x: /delete");
sw.WriteLine("pause");
sw.Flush();
sw.Close();
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = " cmd.exe ";
startInfo.Arguments = @" /c E:\test\Projects\testMVC\TestMVC\-1.bat";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = false;
startInfo.RedirectStandardOutput = false;
startInfo.CreateNoWindow = true;
process.StartInfo = startInfo;
process.Start();
//process.WaitForExit();
process.Dispose();
return "";
}
/// <summary>
/// 创建文件夹
/// </summary>
/// <param name="Path"></param>
public string FolderCreate()
{
判断目标目录是否存在如果不存在则新建之
//string Path = "E:\\web2";
//if (!Directory.Exists(Path))
// Directory.CreateDirectory(Path);
string path = @"E:\web2\MyTest.txt";
//设置某个文件或文件夹属性,如,只读、隐藏等
// Create the file if it does not exist.
if (!System.IO.File.Exists(path))
{
System.IO.File.Create(path);
}
if ((System.IO.File.GetAttributes(path) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
// Show the file.
System.IO.File.SetAttributes(path, FileAttributes.Archive);
Console.WriteLine("The {0} file is no longer hidden.", path);
}
else
{
// Hide the file.
System.IO.File.SetAttributes(path, System.IO.File.GetAttributes(path) | FileAttributes.ReadOnly);
Console.WriteLine("The {0} file is now hidden.", path);
}
//if ((System.IO.File.GetAttributes(path) & FileAttributes.Hidden) == FileAttributes.Hidden)
//{
// // Show the file.
// System.IO.File.SetAttributes(path, FileAttributes.Archive);
// Console.WriteLine("The {0} file is no longer hidden.", path);
//}
//else
//{
// // Hide the file.
// System.IO.File.SetAttributes(path, System.IO.File.GetAttributes(path) | FileAttributes.Hidden);
// Console.WriteLine("The {0} file is now hidden.", path);
//}
return "";
}
#region 获取指定文件详细属性
/****************************************
* 函数名称:GetFileAttibe(string filePath)
* 功能说明:获取指定文件详细属性
* 参 数:filePath:文件详细路径
* 调用示列:
* string file = Server.MapPath("robots.txt");
* Response.Write(EC.FileObj.GetFileAttibe(file));
*****************************************/
/// <summary>
/// 获取指定文件详细属性
/// </summary>
/// <param name="filePath">文件详细路径</param>
/// <returns></returns>
public static string GetFileAttibe(string filePath)
{
string str = "";
System.IO.FileInfo objFI = new System.IO.FileInfo(filePath);
//str += "详细路径:" + objFI.set.FullName + "<br>文件名称:" + objFI.Name + "<br>文件长度:" + objFI.Length.ToString() + "字节<br>创建时间" + objFI.CreationTime.ToString() + "<br>最后访问时间:" + objFI.LastAccessTime.ToString() + "<br>修改时间:" + objFI.LastWriteTime.ToString() + "<br>所在目录:" + objFI.DirectoryName + "<br>扩展名:" + objFI.Extension;
return str;
}
#endregion
#region 为计算机用户添加对某个文件或文件夹的权限
/// <summary>
/// 为计算机用户添加对某个文件或文件夹的权限
/// </summary>
/// <returns></returns>
public string setAuthority()
{
string fileName = @"E:\web2\MyTest.docx";
//给文件添加"Everyone,Users"用户组的完全控制权限
FileInfo fi = new FileInfo(fileName);
System.Security.AccessControl.FileSecurity fileSecurity = fi.GetAccessControl();
fileSecurity.AddAccessRule(new FileSystemAccessRule("wyb", FileSystemRights.Read, AccessControlType.Allow));
fileSecurity.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow));
移除对某个文件的某项权限
//fileSecurity.RemoveAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow));
fi.SetAccessControl(fileSecurity);
//给Excel文件所在目录添加"Everyone,Users"用户组的完全控制权限
DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(fileName));
System.Security.AccessControl.DirectorySecurity dirSecurity = di.GetAccessControl();
dirSecurity.AddAccessRule(new FileSystemAccessRule("wyb", FileSystemRights.Read, AccessControlType.Allow));
dirSecurity.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow));
移除对某个文件的某项权限
//dirSecurity.RemoveAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow));
di.SetAccessControl(dirSecurity);
return "";
}
#endregion
[DllImp
ort("kernel32.dll")]
public static extern IntPtr _lopen(string lpPathName, int iReadWrite);
[DllImp
ort("kernel32.dll")]
public static extern bool CloseHandle(IntPtr hObject);
public const int OF_READWRITE = 2;
public const int OF_SHARE_DENY_NONE = 0x40;
public readonly IntPtr HFILE_ERROR = new IntPtr(-1);
public bool IsFileInUse2()
{
string vFileName = @"E:\web2\MyTest.txt";
if (!System.IO.File.Exists(vFileName))
{
//MessageBox.Show("文件都不存在,你就不要拿来耍了");
//return;
}
IntPtr vHandle = _lopen(vFileName, OF_READWRITE | OF_SHARE_DENY_NONE);
if (vHandle == HFILE_ERROR)
{
//MessageBox.Show("文件被占用!");
//return;
}
CloseHandle(vHandle);
//MessageBox.Show("没有被占用!");
return true;
}
/// <summary>
/// 判断文件是否被占用
/// </summary>
/// <returns></returns>
public bool IsFileInUse()
{
bool inUse = true;
//文本文件不可以判断,其他文件类型未知(同个文件可以多开的类型不可以判断)
string fileName = @"E:\web2\1.docx";
//string fileName = @"X:\user1.txt";
try
{
System.IO.File.Move(fileName, fileName);
}
catch (Exception ex)
{
//正在打开或使用
Console.WriteLine(ex.Message);
}
return inUse;//true表示正在使用,false没有使用
}
}
}