#region 设置某个文件或文件夹属性,如,只读、隐藏等
/// <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 "";
}
#endregion
#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 测试系统监听方法
//static int TimeoutMillis = 2000; //定时器触发间隔
//System.IO.FileSystemWatcher fsw = new System.IO.FileSystemWatcher();
//System.Threading.Timer m_timer = null;
//List<String> files = new List<string>(); //记录待处理文件的队列
//WatcherTimer watcher = new WatcherTimer(onFileSystem_Changed, TimeoutMillis);
FileSystemWatcher fsw;
string filePath = @"E:\web2";
SqlDataReader dr;
#region 设置监视单个文件夹
public string testsystemWatcher()
{
try
{
fsw = new FileSystemWatcher { Path = filePath };
fsw.Filter = "*";
fsw.IncludeSubdirectories = true;
fsw.EnableRaisingEvents = true;
fsw.Created += onFileSystem_Changed; //被创建时事件
fsw.Deleted += onFileSystem_Changed; //被删除时事件
fsw.Changed += onFileSystem_Changed; //被修改内容时事件
fsw.Renamed += watcher_Renamed; //被重命名时事件
}
catch (Exception ex)
{
string filePath = @"D:/TempExceptions.txt";
StreamWriter sw = System.IO.File.AppendText(filePath);
sw.Write("\r\n" + "错误信息:" + ex.ToString() + "\r\n时间:" + System.DateTime.Now + "\r\n" + "\r\n");
sw.Close();
}
return "";
}
#endregion
//private static void OnChanged(object source, FileSystemEventArgs e)
//{
// // Specify what is done when a file is changed, created, or deleted.
// Console.WriteLine("File: " + " " + e.ChangeType);
// string filepath = e.FullPath; //mypath + "//" + cr.Name;
// FileInfo fi = new FileInfo(filepath);
// if (!fi.Exists)
// {
// Console.WriteLine("file not exits!!");
// }
// Console.WriteLine(fi.IsReadOnly.ToString());
if (!fi.IsReadOnly)
{
// // fi.AppendText();
}
// try
// {
// fi.OpenRead();
// }
// catch (IOException ex)
// {
// Console.WriteLine(ex.Message);
// Thread.Sleep(3000);
// //goto HELLO;
// }
// //OnRMChanged(e.FullPath);//对文件进行任意处理
//}
#region 设置监视多个文件夹
public string testsystemWatcherMore()
{
string[] args = new string[] { @"E:\web2" };
FileSystemWatcher[] watchers;
//若未传递参数,则监视所有文件系统,包括CD-ROM(不可用),可移动磁盘(不可用)等
if (args.Length == 0)
{
string[] drivers = Directory.GetLogicalDrives();
watchers = new FileSystemWatcher[drivers.Length];
for (int i = 0; i < drivers.Length; i++)
{
try
{
watchers[i] = new FileSystemWatcher { Path = drivers[i] };
}
catch (Exception ex)
{
Trace.TraceWarning(ex.Message);
}
}
}
else
{
watchers = new FileSystemWatcher[1];
watchers[0] = new FileSystemWatcher { Path = args[0] };
}
foreach (FileSystemWatcher w in watchers)
{
if (w == null) continue;
w.Filter = "*";
w.IncludeSubdirectories = true;
w.EnableRaisingEvents = true;
w.Created += onFileSystem_Changed;
w.Deleted += onFileSystem_Changed;
w.Changed += onFileSystem_Changed;
w.Renamed += watcher_Renamed;
}
return "";
}
#endregion
private static volatile object _lock = true;
string teststr = "";
void onFileSystem_Changed(object sender, FileSystemEventArgs e)
{
try
{
string newpath = "";
string newname = "";
int iChanged = 0;
int iCopy = 0;
//if (System.IO.File.GetAttributes(filePath) == FileAttributes.Directory)//true对文件夹操作,false对文件操作
lock (_lock)
{
//string url = "http://192.168.1.225:8081/"; //模拟网页访问服务器,为了得到request对象
//WebRequest req = WebRequest.Create(url);
//WebResponse res = req.GetResponse();
//Stream resStream = res.GetResponseStream();
//StreamReader sr = new StreamReader(resStream, Encoding.UTF8);
StreamReader sr = new StreamReader(resStream, Encoding.GetEncoding("gbk"));支持中文
//string contentHTML = sr.ReadToEnd(); //读取网页的源代码
Response.Write(contentHTML);
//resStream.Close();
//sr.Close();
//string teststr = contentHTML.Substring(contentHTML.IndexOf("$$")+2, contentHTML.LastIndexOf("$$") - contentHTML.IndexOf("$$")-2);//从第一个$$截取到第二个$$,中间即是IP
FilesBLL filesbll = new FilesBLL();
if (e.Name.ToString().IndexOf(".tmp") >= 0)//说明变动的文件是word等的临时文件
{
}
else if (e.Name.ToString().IndexOf(".db") >= 0)//说明变动的文件是临时文件
{
}
else
{
if (e.Name.ToString().IndexOf("~$") == 0)//说明修改的是WORD
{
newpath = e.FullPath.ToString().Replace("~$", "");
newname = e.Name.ToString().Replace("~$", "");
//string teststr = getAuthor(newpath);//获取WORD作者。不兼容
dr = filesbll.checkFile("文件名:" + newname + "类型:" + e.ChangeType.ToString(), "修改者:" + teststr, "文件路径:" + newpath, DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
if (!dr.Read())//如果数据库没有此条记录
{
CopyDir(newpath, @"E:\web3", newname);//有~$号说明就是修改WORD格式,直接备份
int istrue = filesbll.add("文件名:" + newname + "类型:Changed", "修改者:" + teststr, "文件路径:" + newpath);
}
dr.Close();
dr.Dispose();
dr = filesbll.checkFile("文件名:" + newname, "创建者:" + teststr, "文件路径:" + newpath, DateTime.Now.ToString("yyyy-MM-dd HH:mm"), DateTime.Now.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm"));
while (dr.Read())
{
iChanged++;
}
dr.Close();
dr.Dispose();
if (iChanged >= 3)
{
int istrue = filesbll.delFile("文件名:" + newname, "创建者:" + teststr, "文件路径:" + newpath, DateTime.Now.ToString("yyyy-MM-dd HH:mm"), DateTime.Now.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm"));
int istrue1 = filesbll.delFile("文件名:" + newname + "类型:Copy", "创建者:" + teststr, "文件路径:" + newpath, DateTime.Now.ToString("yyyy-MM-dd HH:mm"), DateTime.Now.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm"));
//int istrue2 = filesbll.add("文件名:" + newname + "类型:Changed1", "创建者:" + teststr, "文件路径:" + newpath);
}
//if (iupint == 2)
//{
// int istrue = filesbll.delFile("文件名:" + newname, "创建者:" + teststr, "文件路径:" + newpath, DateTime.Now.ToString("yyyy-MM-dd HH:mm"), DateTime.Now.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm"));
// int istrue1 = filesbll.add("文件名:" + newname + "类型:Copy", "创建者:" + teststr, "文件路径:" + newpath);
//}
}
else
{
//string teststr = getAuthor(e.FullPath.ToString());
if (e.Name.ToString().IndexOf("~") >= 0)
{
}
else
{
//if (e.Name.ToString().ToLower().IndexOf(".docx")>=0&&e.ChangeType.ToString().ToLower() == "created")//新建或删除的是WORD
//{
// teststr = getAuthor(e.FullPath);
//}
dr = filesbll.checkFile("文件名:" + e.Name.ToString() + "类型:" + e.ChangeType.ToString(), "创建者:" + teststr, "文件路径:" + e.FullPath.ToString(), DateTime.Now.ToString("yyyy-MM-dd HH:mm"));
if (!dr.Read())//如果数据库没有此条记录
{
if (e.ChangeType.ToString().ToLower() == "changed")//如果是修改或删除,则先备份文件//删除前暂不能备份 || e.ChangeType.ToString().ToLower() == "deleted"
{
CopyDir(e.FullPath.ToString(), @"E:\web3", e.Name.ToString());
}
int istrue = filesbll.add("文件名:" + e.Name.ToString() + "类型:" + e.ChangeType.ToString(), "创建者:" + teststr, "文件路径:" + e.FullPath.ToString());
}
dr.Close();
dr.Dispose();
dr = filesbll.checkFile("文件名:" + e.Name.ToString(), "创建者:" + teststr, "文件路径:" + e.FullPath.ToString(), DateTime.Now.ToString("yyyy-MM-dd HH:mm"), DateTime.Now.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm"));
while (dr.Read())
{
iCopy++;
}
dr.Close();
dr.Dispose();
//if (iupint >= 3)
//{
// int istrue = filesbll.delFile("文件名:" + newname, "创建者:" + teststr, "文件路径:" + newpath, DateTime.Now.ToString("yyyy-MM-dd HH:mm"), DateTime.Now.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm"));
// int istrue1 = filesbll.delFile("文件名:" + newname + "类型:Copy", "创建者:" + teststr, "文件路径:" + newpath, DateTime.Now.ToString("yyyy-MM-dd HH:mm"), DateTime.Now.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm"));
// int istrue2 = filesbll.add("文件名:" + newname + "类型:Changed", "创建者:" + teststr, "文件路径:" + newpath);
//}
//2和3有冲突。把传值类型等改下。
if (iCopy == 2)
{
int istrue = filesbll.delFile("文件名:" + e.Name.ToString(), "创建者:" + teststr, "文件路径:" + e.FullPath.ToString(), DateTime.Now.ToString("yyyy-MM-dd HH:mm"), DateTime.Now.AddMinutes(-1).ToString("yyyy-MM-dd HH:mm"));
int istrue1 = filesbll.add("文件名:" + e.Name.ToString() + "类型:Copy", "创建者:" + teststr, "文件路径:" + e.FullPath.ToString());
}
}
}
}
}
}
catch (Exception ex)
{
string filePath = @"D:/TempExceptions.txt";
StreamWriter sw = System.IO.File.AppendText(filePath);
sw.Write("\r\n" + "错误信息:" + teststr + "---" + ex.ToString() + "\r\n时间:" + System.DateTime.Now + "\r\n" + "\r\n");
sw.Close();
}
}
void watcher_Renamed(object sender, RenamedEventArgs e)
{
try
{
lock (_lock)
{
//HttpCookie getusercookie = Request.Cookies[Request.UserHostAddress.ToString().Replace(".", "")];//获取COOKIE中的值
//if (!string.IsNullOrEmpty(getusercookie.Value))
//{
// stra = getusercookie.Value;
//}
if (e.Name.ToString().IndexOf("~") == 0 || e.OldName.ToString().IndexOf("~") == 0)
{
}
else
{
//string teststr = getAuthor(e.FullPath.ToString());
FilesBLL filesbll = new FilesBLL();
SqlDataReader dr = filesbll.checkFile("重命名前文件名:" + e.OldName.ToString() + "类型:" + e.ChangeType.ToString(), "重命名者:" + teststr, "重命名前文件路径:" + e.OldFullPath.ToString(), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
if (!dr.Read())//如果数据库没有此条记录
{
int istrue = filesbll.add("重命名前文件名:" + e.OldName.ToString() + "类型:" + e.ChangeType.ToString(), "重命名者:" + teststr, "重命名前文件路径:" + e.OldFullPath.ToString());
}
dr.Close();
dr.Dispose();
SqlDataReader dr1 = filesbll.checkFile("重命名后文件名:" + e.Name.ToString() + "类型:" + e.ChangeType.ToString(), "重命名者:" + teststr, "重命名后文件路径:" + e.FullPath.ToString(), DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
if (!dr1.Read())//如果数据库没有此条记录
{
int istrue2 = filesbll.add("重命名后文件名:" + e.Name.ToString() + "类型:" + e.ChangeType.ToString(), "重命名者:" + teststr, "重命名后文件路径:" + e.FullPath.ToString());
}
dr1.Close();
dr1.Dispose();
}
}
}
catch (Exception ex)
{
string filePath = @"D:/TempExceptions.txt";
StreamWriter sw = System.IO.File.AppendText(filePath);
sw.Write("\r\n" + "错误信息:" + teststr + "---" + ex.ToString() + "\r\n时间:" + System.DateTime.Now + "\r\n" + "\r\n");
sw.Close();
}
}
#endregion
#region 将指定文件夹下面的所有内容copy到目标文件夹下面 果目标文件夹为只读属性就会报错。
/****************************************
* 函数名称:CopyDir
* 功能说明:将指定文件夹下面的所有内容copy到目标文件夹下面 果目标文件夹为只读属性就会报错。
* 参 数:srcPath:原始路径,aimPath:目标文件夹
* 调用示列:
* string srcPath = Server.MapPath("test/");
* string aimPath = Server.MapPath("test1/");
* EC.FileObj.CopyDir(srcPath,aimPath);
*****************************************/
/// <summary>
/// 指定文件夹下面的所有内容copy到目标文件夹下面
/// </summary>
/// <param name="srcPath">原始路径</param>
/// <param name="aimPath">目标文件夹</param>
/// <param name="filename">文件名</param>
public void CopyDir(string srcPath, string aimPath, string filename)
{
try
{
// 检查目标目录是否以目录分割字符结束如果不是则添加之
if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
aimPath += Path.DirectorySeparatorChar;
// 判断目标目录是否存在如果不存在则新建之
if (!Directory.Exists(aimPath))
Directory.CreateDirectory(aimPath);
// 得到源目录的文件列表,该里面是包含文件以及目录路径的一个数组
//如果你指向copy目标文件下面的文件而不包含目录请使用下面的方法
//string[] fileList = Directory.GetFiles(srcPath);
//string[] fileList = Directory.GetFileSystemEntries(srcPath);
//遍历所有的文件和目录
string strnewpath = aimPath + teststr + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("-", "").Replace(" ", "").Replace(":", "").Replace(".", "") + filename;
System.IO.File.Copy(srcPath, strnewpath, true);
//string[] fileList = Directory.GetFileSystemEntries(srcPath);
遍历所有的文件和目录
//foreach (string file in fileList)
//{
// //先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
// if (Directory.Exists(file))
// CopyDir(file, aimPath + Path.GetFileName(file));
// //否则直接Copy文件
// else
// System.IO.File.Copy(file, aimPath + Path.GetFileName(file), true);
//}
}
catch (Exception ee)
{
throw new Exception(ee.ToString());
}
}
#endregion
#region 测试给文件/文件夹分配权限
public string setAuthority1()
{
string fileName = @"E:\web2\wenj3\MyTest.docx";
//给文件添加"Everyone,SYSTEM,NETWORK SERVICE,Administrator,Administrators,Users"
FileInfo fi = new FileInfo(fileName);
System.Security.AccessControl.FileSecurity fileSecurity = fi.GetAccessControl();//获取文件的权限
bool istrue1 = fileSecurity.RemoveAccessRule(new FileSystemAccessRule("wyb", FileSystemRights.FullControl, AccessControlType.Allow));
bool istrue2 = fileSecurity.RemoveAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));
bool istrue3 = fileSecurity.RemoveAccessRule(new FileSystemAccessRule("SYSTEM", FileSystemRights.FullControl, AccessControlType.Allow));
bool istrue4 = fileSecurity.RemoveAccessRule(new FileSystemAccessRule("NETWORK SERVICE", FileSystemRights.FullControl, AccessControlType.Allow));
//bool istrue5 = fileSecurity.RemoveAccessRule(new FileSystemAccessRule("Administrator", FileSystemRights.FullControl, AccessControlType.Allow));
//bool istrue6 = fileSecurity.RemoveAccessRule(new FileSystemAccessRule("Administrators", FileSystemRights.FullControl, AccessControlType.Allow));
bool istrue7 = fileSecurity.RemoveAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow));
fileSecurity.AddAccessRule(new FileSystemAccessRule("wyb", FileSystemRights.Read, AccessControlType.Allow));
fileSecurity.AddAccessRule(new FileSystemAccessRule("wyb", FileSystemRights.ReadData, AccessControlType.Allow));
//fileSecurity.AddAccessRule(new FileSystemAccessRule("wyb", FileSystemRights.Write, AccessControlType.Allow));
//fileSecurity.AddAccessRule(new FileSystemAccessRule("wyb", FileSystemRights.WriteData, AccessControlType.Allow));
//fileSecurity.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow));
移除对某个文件的某项权限
//ReadData 列出文件夹/读取数据
//Read 读取属性,读取扩展属性,读取权限
//WriteData 创建文件/写入数据
fi.SetAccessControl(fileSecurity);
return "";
}
#endregion
#region 为计算机用户添加对某个文件或文件夹的权限
/// <summary>
/// 为计算机用户添加对某个文件或文件夹的权限
/// </summary>
/// <returns></returns>
public string setAuthority()
{
string luser = Request.Form["cuuser"];
string[] arruser = luser.Split(',');//获取选中的用户名集合。//要去掉false,字符
string lfile = Request.Form["files"];
string[] arrfiles = lfile.Split(',');//获取选中的文件集合。//要去掉false,字符
string lauthority = Request.Form["authority"];
string[] arrauthority = lauthority.Split(',');//获取选中权限集合。//要去掉false,字符
//首先把此用户的权限全部删除
foreach (string user in arruser)
{
foreach (string file in arrfiles)
{
string fileName = file;
//给文件添加"Everyone,Users"用户组的完全控制权限
FileInfo fi = new FileInfo(fileName);
System.Security.AccessControl.FileSecurity fileSecurity = fi.GetAccessControl();//获取文件的权限
先移除对某个文件的所有权限
fileSecurity.RemoveAccessRule(new FileSystemAccessRule(user, FileSystemRights.FullControl, AccessControlType.Allow));
//再把设置好,传过来的权限赋给用户
foreach (string authority in arrauthority)
{
switch (authority)
{
case "see":
fileSecurity.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.Read, AccessControlType.Allow));
fileSecurity.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.ReadData, AccessControlType.Allow));
break;
case "del":
fileSecurity.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.Delete, AccessControlType.Allow));
break;
case "update":
fileSecurity.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.Read, AccessControlType.Allow));
fileSecurity.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.ReadData, AccessControlType.Allow));
fileSecurity.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.Write, AccessControlType.Allow));
fileSecurity.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.WriteData, AccessControlType.Allow));
break;
case "all":
fileSecurity.AddAccessRule(new FileSystemAccessRule(user, FileSystemRights.FullControl, AccessControlType.Allow));
break;
default:
fileSecurity.RemoveAccessRule(new FileSystemAccessRule(user, FileSystemRights.FullControl, AccessControlType.Allow));
break;
}
}
//fileSecurity.AddAccessRule(new FileSystemAccessRule("wyb", FileSystemRights.Read, AccessControlType.Allow));
//fileSecurity.AddAccessRule(new FileSystemAccessRule("Users", FileSystemRights.FullControl, AccessControlType.Allow));
fi.SetAccessControl(fileSecurity);
}
}
//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
[DllImport("kernel32.dll")]
public static extern IntPtr _lopen(string lpPathName, int iReadWrite);
[DllImport("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);
#region 判断文件是否被占用
/// <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没有使用
}
#endregion
#region 获取当前已登录到 Windows 操作系统的人员的用户名或其他信息
/// <summary>
/// 获取当前已登录到 Windows 操作系统的人员的用户名或其他信息
/// </summary>
/// <returns></returns>
public string getLoginUserInfo()
{
Console.WriteLine("处理器数量:" + Environment.ProcessorCount);
Console.WriteLine("操作系统版本:" + Environment.OSVersion);
Console.WriteLine("公共语言运行时版本(CLR):" + Environment.Version);
Console.WriteLine("机器名:" + Environment.MachineName);
Console.WriteLine("用户名:" + Environment.UserName);//获取当前已登录到 Windows 操作系统的人员的用户名
//以登录名作为SQL的用户名,以记录
return Environment.UserName;
}
#endregion
#region 获取WORD作者
public string getAuthor(string filename)
{
Word.Application oWord;
Word._Document oDoc;
object oMissing = Missing.Value;
object docBuiltInProps;
object Source = filename;// @"E:\web2\Mytest.docx";
object Unknown = Type.Missing;
oWord = new Word.Application();
//获取作者
string index = "Author";//Author
string propsValue;
try
{
oDoc = oWord.Documents.Open(ref Source, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown);
docBuiltInProps = oDoc.BuiltInDocumentProperties;
Type typeDocBuiltInProps = docBuiltInProps.GetType();
object docAuthor = typeDocBuiltInProps.InvokeMember("Item",
BindingFlags.Default |
BindingFlags.GetProperty,
null, docBuiltInProps,
new object[] { index });
Type typeDocAuthorProp = docAuthor.GetType();
propsValue = typeDocAuthorProp.InvokeMember("Value",
BindingFlags.Default |
BindingFlags.GetProperty,
null, docAuthor,
new object[] { }).ToString();
//MessageBox.Show(propsValue, "Author");
}
finally
{
//关闭word进程
object save = false;
oWord.Quit(ref save, ref Unknown, ref Unknown);
System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);
}
return propsValue;
}
#endregion
#region 获取客户端的IP
public string GetClientIP()
{
string result = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (null == result || result == String.Empty)
{
result = Request.ServerVariables["REMOTE_ADDR"];
}
if (null == result || result == String.Empty)
{
result = Request.UserHostAddress;
}
return result;
}
#endregion
public string test()
{
return "";
}
//映射服务器路径到本地磁盘
//在WEB上点映射的时候,取WEB上登录名,去匹配数据库里的用户名,来生成对应的文件及文件夹的权限(增删改查等)
//实时通讯检查服务器下文件存放所有目录的变动,有变动则在数据库里同步
//(例如:某登录用户新粘贴进来一个文件,取该用户在服务器上的增删改的变动)
//粘到到哪个目录就取哪个文件夹的名字。做为文件的类型
}
}