Windows用户类型:Administrator、Guest、自定义用户等;文件的权限不外乎:Read、Write、Modify、Remove等等,其中拥有所有权限的字符叫做FullControl。
///
/// 是否拥有程序数据路径权限
///
private static bool HasProgramDataRights=false;
public static bool IsAdministrator()
{
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
return principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
}
///
/// 读取系统程序数据路径权限&判断是否可以使用临时目录进行保存操作
///
///
public void ReadProgramDataRights(string path)
{
// 通过判定文件的权限来控制路径
System.Security.AccessControl.DirectorySecurity dirSec = new System.IO.DirectoryInfo(path).GetAccessControl();
System.Security.AccessControl.AuthorizationRuleCollection rules = dirSec.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
foreach (System.Security.AccessControl.FileSystemAccessRule rule in rules)
{
Console.WriteLine(path + "文件目录权限字符:" + rule.FileSystemRights);
// 判定文件是否具有FullControl或Write权限
if (rule.FileSystemRights.ToString().Equals("FullControl") || rule.FileSystemRights.ToString().Equals("Write"))
{
HasProgramDataRights = true;
break;
}
}
// 为IE保护模式创建临时保存路径
if (HasProgramDataRights)
{
// 创建操作路径
string recordTemp = FileHelp.GetAppDataLocalLow() + "\\CvNetVideo\\Record\\";
string screenshotTemp = FileHelp.GetAppDataLocalLow() + "\\CvNetVideo\\Screenshot\\";
string recordDst = FileHelp.GetCommonApplicationData() + "\\CvNetVideo\\Record\\";
string screenshotDst = FileHelp.GetCommonApplicationData() + "\\CvNetVideo\\Screenshot\\";
Directory.CreateDirectory(recordTemp);
Directory.CreateDirectory(screenshotTemp);
Directory.CreateDirectory(recordDst);
Directory.CreateDirectory(screenshotDst);
}
}
public class FileHelp
{
public enum GetDirectoryType
{
ByAppDomain,
ByAssembly
}
public static string GetCurrentDirectory(GetDirectoryType type = GetDirectoryType.ByAppDomain)
{
switch (type)
{
case GetDirectoryType.ByAppDomain:
return AppDomain.CurrentDomain.BaseDirectory;
case GetDirectoryType.ByAssembly:
return Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
default:
return AppDomain.CurrentDomain.BaseDirectory;
}
}
public static string GetCurrentDirectoryByAssembly()
{
return GetCurrentDirectory(GetDirectoryType.ByAssembly);
}
///
///程序数据路径- C:\ProgramData
///
///
public static string GetCommonApplicationData()
{
return Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
}
///
/// 用户数据路径
///
///
public static string GetApplicationData()
{
return Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
}
///
/// 用户数据本地路径
///
///
public static string GetLocalApplicationData()
{
return Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
}
///
/// 用户路径
///
///
public static string GetUserProfile()
{
return Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
}
///
/// 用户的图片路径
///
///
public static string GetMyPictures()
{
return Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
}
///
/// 用户的视频路径
///
///
public static string GetMyVideos()
{
return Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);
}
///
/// 用户的文档路径
///
///
public static string GetMyDocuments()
{
return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
}
///
/// IE保护模式下的低权限操作路径(Temporary Internet Files/Low)
/// 参考:https://blog.csdn.net/xt_xiaotian/article/details/5336809
///
///
public static string GetTemporaryInternetFiles()
{
return GetLocalApplicationData()+ "\\Microsoft\\Windows\\Temporary Internet Files\\Low";
}
///
/// IE保护模式下的低权限操作路径(%userprofile%/AppData/LocalLow)
/// 参考:https://blog.csdn.net/xt_xiaotian/article/details/5336809
///
///
public static string GetAppDataLocalLow()
{
return GetUserProfile() + "\\AppData\\LocalLow";
}
///
/// 获取系统字体文件路径
///
///
public static string GetFonts()
{
return Environment.GetFolderPath(Environment.SpecialFolder.Fonts);
}
///
/// 二进制文件读取
///
/// 文件路径
///
public static byte[] BinaryRead(string FileUrl)
{
List lst = new List();
try
{
//文件路径
String filename = FileUrl;
//打开文件
FileStream FStream;
if (File.Exists(filename))
{
FStream = new FileStream(filename, FileMode.Open);
}
else
{
return null;
}
int BufferSize = 1048576; //每次读取的字节数 每次读取1MB
byte[] Buffer = new byte[BufferSize];
long FileLength = FStream.Length;//文件流的长度
int ReadCount = (int)(FileLength / BufferSize) + 1; //需要对文件读取的次数
//数据读取
BinaryReader BWrite = new BinaryReader(FStream);
//br.BaseStream.Seek(0, SeekOrigin.Begin);
//while (br.BaseStream.Position < br.BaseStream.Length){}
for (int a = 0; a < ReadCount; a++)
{
Buffer = BWrite.ReadBytes(BufferSize);
lst.AddRange(Buffer);
}
BWrite.Close();
BWrite.Close();
return lst.ToArray();
}
catch (System.Exception ex)
{
Log.WriteLog4Ex("FileHelp.BinaryRead", ex);
return null;
}
}
///
/// 二进制文件写入
///
///
/// 文件目录路径
/// 文件名称
///
public static bool BinaryWrite(byte[] Bts, string DirectoryUrl, string FileName)
{
try
{
//文件路径
string Filepath = DirectoryUrl + "\\" + FileName;
//目录创建
if (!Directory.Exists(DirectoryUrl))
Directory.CreateDirectory(DirectoryUrl);
//文件创建
FileStream FStream;
if (File.Exists(Filepath))
{
FStream = new FileStream(Filepath, FileMode.Append);
}
else
{
FStream = new FileStream(Filepath, FileMode.Create);
}
//数据写入
BinaryWriter BWrite = new BinaryWriter(FStream);
BWrite.Write(Bts);
BWrite.Close();
FStream.Close();
return true;
}
catch (System.Exception ex)
{
Log.WriteLog4Ex("FileHelp.BinaryWrite", ex);
return false;
}
}
///
/// 二进制文件删除
///
/// 文件路径
public static void FileDelete(string FileUrl)
{
try
{
//文件路径
String filename = FileUrl;
//删除文件
if (File.Exists(filename))
{
File.Delete(filename);
}
}
catch (System.Exception ex)
{
Log.WriteLog4Ex("FileHelp.FileDelete", ex);
}
}
}
注:如果您是开发OCX遇到路径问题请使用工具类中IE保护模式下的低权限路径。