以上就是给各位分享如何获取当前用户在Windowslogin会话的唯一ID–,其中也会对如何获取当前登录的用户名称进行解释,同时本文还将给你拓展c#windows窗体,当我以管理员身份运行时,我无法获
以上就是给各位分享如何获取当前用户在Windowslogin会话的唯一ID –,其中也会对如何获取当前登录的用户名称进行解释,同时本文还将给你拓展c# windows 窗体,当我以管理员身份运行时,我无法获取当前用户名、c# – 在Windows 7中获取当前系统卷、c# – 如何在asp.net中获取当前的Windows用户?、JS 获取当前用户在 win 系统的临时文件夹路径等相关知识,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:- 如何获取当前用户在Windowslogin会话的唯一ID –(如何获取当前登录的用户名称)
- c# windows 窗体,当我以管理员身份运行时,我无法获取当前用户名
- c# – 在Windows 7中获取当前系统卷
- c# – 如何在asp.net中获取当前的Windows用户?
- JS 获取当前用户在 win 系统的临时文件夹路径
如何获取当前用户在Windowslogin会话的唯一ID –(如何获取当前登录的用户名称)
我需要得到一个唯一标识当前Windows用户的login会话的值。 这是一个winforms应用程序,而不是ASP.NET。 我将从多个进程中检索它,因此在同一个login会话中检索时需要返回相同的值。 在所有用户会话期间,只需在当前机器上保持唯一性 – 例如直到下一次重新启动机器。
我认为WindowsloginID是正确的,但似乎有点痛苦检索。 还有什么或更简单的方法来得到这个?
我将使用该ID来包含在一个命名pipe道服务的地址中,以在机器上运行的两个进程之间进行通信。 我想包括loginID,以避免有多个用户login时发生冲突,包括可能是同一用户的多个会话。
应用程序重启API不重新启动失败的应用程序
.NET EXE和DLL之间的堆栈/堆区别
如何在安装程序下载时在.NET安装程序中embedded用户特定的数据?
将Tif索引的8位颜色转换为32位颜色
如何在ColdFusion中运行.net函数
访问Windows剪贴板
用Visual Basic在Windows窗体中embeddedDOS控制台
文件属性对话框中的自定义标签
什么是Visual Studio创build.NET窗体时分配的窗口类名称?
如何为Windows窗体应用程序创build安装程序和卸载程序
据我所知,你需要的是这样的:
SID:S-1-5-5-XY名称:登录会话描述:登录会话。 这些SID的X和Y值对于每个会话都是不同的。
Windows操作系统中众所周知的安全标识符http://support.microsoft.com/kb/243330
有人问这里有类似的东西:
如何在C#中登录SID
他们在那里有一个很好的答案,但我想补充我自己的
这是我的解决方案:
using System; using System.Windows.Forms; using System.Runtime.InteropServices; namespace TestlogonSid { public partial class Form1 : Form { private delegate bool EnumDesktopProc(string lpszDesktop,IntPtr lParam); public Form1() { InitializeComponent(); } private void button1_Click_1(object sender,EventArgs e) { this.textBox1.Text = GetlogonSid.getlogonSid(); } } public class GetlogonSid { //The SID structure that identifies the user that is currently associated with the specified object. //If no user is associated with the object,the value returned in the buffer pointed to by lpnLengthNeeded is zero. //Note that SID is a variable length structure. //You will usually make a call to GetUserObjectinformation to determine the length of the SID before retrieving its value. private const int UOI_USER_SID = 4; //GetUserObjectinformation function //Retrieves information about the specified window station or desktop object. [DllImport("user32.dll")] static extern bool GetUserObjectinformation(IntPtr hObj,int nIndex,[MarshalAs(UnmanagedType.LPArray)] byte[] pvInfo,int nLength,out uint lpnLengthNeeded); //GetThreadDesktop function //Retrieves a handle to the desktop assigned to the specified thread. [DllImport("user32.dll")] private static extern IntPtr GetThreadDesktop(int dwThreadId); //GetCurrentThreadId function //Retrieves the thread identifier of the calling thread. [DllImport("kernel32.dll")] public static extern int GetCurrentThreadId(); //ConvertSidToStringSid function //The ConvertSidToStringSid function converts a security identifier (SID) to a string format suitable for display,storage,or transmission. //To convert the string-format SID back to a valid,functional SID,call the ConvertStringSidToSid function. [DllImport("advapi32",CharSet = CharSet.Auto,SetLastError = true)] static extern bool ConvertSidToStringSid( [MarshalAs(UnmanagedType.LPArray)] byte[] pSID,out IntPtr ptrSid); /// <summary> /// The getlogonSid function returns the logon Session string /// </summary> /// <returns></returns> public static string getlogonSid() { string sidString = ""; IntPtr hdesk = GetThreadDesktop(GetCurrentThreadId()); byte[] buf = new byte[100]; uint lengthNeeded; GetUserObjectinformation(hdesk,UOI_USER_SID,buf,100,out lengthNeeded); IntPtr ptrSid; if (!ConvertSidToStringSid(buf,out ptrSid)) throw new System.ComponentModel.Win32Exception(); try { sidString = Marshal.PtrToStringAuto(ptrSid); } catch { } return sidString; } } }
获取Session ID最简单的方法是查看Process.SessionId属性:
System.Diagnostics.Process.GetCurrentProcess().SessionId
该值与GetTokeninformation(…,TokenSessionId,…)返回的值相同。
注意:您应该记住的一件事是会话Id不是登录ID 。 例如,在同一个会话中,在同一用户下启动的Win7提升进程将具有不同的logonId(共享到非提升的),但会具有相同的SessionId。 即使Runnung非升级过程与RunAs显式指定相同用户的凭据将创建新的登录Sesison Id。 例如,当您映射网络驱动器时,此类行为具有意义。 自Vista以来,即使进程在同一个会话和同一用户下运行,使用一个令牌logonId的进程也不会看到与另一个logonId映射的网络目录。
以下是您可以在不同会话/信誉上启动以查看其差异的示例应用程序:
using System; using System.Runtime.InteropServices; namespace GetCurrentSessionId { class Program { enum TokeninformationClass { TokenUser = 1,TokenGroups,TokenPrivileges,TokeNowner,TokenPrimaryGroup,TokenDefaultDacl,TokenSource,TokenType,TokenImpersonationLevel,TokenStatistics,TokenRestrictedSids,TokenSessionId,TokenGroupsAndPrivileges,TokenSessionReference,TokenSandBoxInert,TokenAuditPolicy,Tokenorigin } enum TokenType { TokenPrimary = 1,TokenImpersonation } enum SecurityImpersonationLevel { SecurityAnonymous,SecurityIdentification,SecurityImpersonation,SecurityDelegation } [StructLayout(LayoutKind.Sequential)] struct TokenStatistics { public Int64 TokenId; public Int64 AuthenticationId; public Int64 ExpirationTime; public TokenType TokenType; public SecurityImpersonationLevel ImpersonationLevel; public Int32 DynamicCharged; public Int32 DynamicAvailable; public Int32 GroupCount; public Int32 PrivilegeCount; public Int64 ModifiedId; } struct Tokenorigin { public Int64 OriginatinglogonSession; } [DllImport("advapi32.dll",EntryPoint = "GetTokeninformation",SetLastError = true)] static extern bool GetTokeninformation( IntPtr tokenHandle,TokeninformationClass tokeninformationClass,IntPtr tokeninformation,int tokeninformationLength,out int ReturnLength); public const int ERROR_INSUFFICIENT_BUFFER = 0x7a; static void Main(string[] args) { try { Console.WriteLine("Session Id: {0}",System.Diagnostics.Process.GetCurrentProcess().SessionId); IntPtr tokenInfo; bool result; int infoSize; IntPtr hToken = System.Security.Principal.WindowsIdentity.GetCurrent().Token; result = GetTokeninformation(hToken,TokeninformationClass.TokenStatistics,IntPtr.Zero,out infoSize); if (!result && Marshal.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER) { tokenInfo = Marshal.AllocHGlobal(infoSize); result = GetTokeninformation(hToken,tokenInfo,infoSize,out infoSize); if (result) { TokenStatistics tokenStats = (TokenStatistics)Marshal.PtrToStructure(tokenInfo,typeof(TokenStatistics)); Console.WriteLine("logonId: 0x{0:X16}",tokenStats.AuthenticationId); } else { Console.Error.WriteLine("logonId: Failed with 0x{0:X08}",Marshal.GetLastWin32Error()); } Marshal.FreeHGlobal(tokenInfo); } else { Console.Error.WriteLine("logonId: Failed with 0x{0:X08}",Marshal.GetLastWin32Error()); } tokenInfo = Marshal.AllocHGlobal(sizeof (Int32)); result = GetTokeninformation(hToken,TokeninformationClass.TokenSessionId,sizeof (Int32),out infoSize); if (result) { int tokenSessionId = Marshal.ReadInt32(tokenInfo); Console.WriteLine("TokenSessionId: {0}",tokenSessionId); } else { Console.Error.WriteLine("TokenSessionId: Failed with 0x{0:X08}",Marshal.GetLastWin32Error()); } Marshal.FreeHGlobal(tokenInfo); result = GetTokeninformation(hToken,TokeninformationClass.Tokenorigin,out infoSize); if (result) { Tokenorigin tokenorigin = (Tokenorigin) Marshal.PtrToStructure(tokenInfo,typeof (Tokenorigin)); Console.WriteLine("OriginatingSessionId: 0x{0:X16}",tokenorigin.OriginatinglogonSession); } else { Console.WriteLine("OriginatingSessionId: Failed with 0x{0:X08}",Marshal.GetLastWin32Error()); } Marshal.FreeHGlobal(tokenInfo); } else { Console.WriteLine("OriginatingSessionId: Failed with 0x{0:X08}",Marshal.GetLastWin32Error()); } Console.WriteLine("Press any key..."); Console.ReadKey(); } catch (Exception ex) { Console.Error.WriteLine("Unexpected error: {0}",ex); Console.ReadKey(); } } } }
您可以尝试Environment.UserDomainName & Environment.UserName
为了确保每个用户会话的唯一ID我想你将不得不使用你提到的痛苦的方法 – 如何获得在C#登录SID 。
如果我正确理解你,你可以生成一个GUID
c# windows 窗体,当我以管理员身份运行时,我无法获取当前用户名
您可以尝试 WMI,看看它是否有效(如果您没有引用它,则有一个适用于它的 Nuget 包)。
string username;
using (var searcher = new ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem"))
{
using (var collection = searcher.Get())
{
username = collection.Cast<ManagementBaseObject>().First()["UserName"].ToString();
}
}
c# – 在Windows 7中获取当前系统卷
我在谷歌中查了一下,但每个解决方案都返回了-1或4686346这样的值,但没有明确解释它们的含义.
解决方法
EndpointVolume
API.这是在Windows Vista中发布的新音频API的一部分,它可用于获取或设置主音量.
由于您不需要在Vista之前支持Windows版本(即Windows XP),因此在这些操作系统版本之间对相关基础架构进行了重大更改,因此这项工作变得相当容易.这可能是您尝试过的现有样品无法正常工作的原因.
CodeProject上有一个完整的托管包装器库:Vista Core Audio API Master Volume Control.它可能实现了比您需要的更多功能,但是您可以了解从C#应用程序确定主系统卷需要执行的操作.
c# – 如何在asp.net中获取当前的Windows用户?
如何在远程服务器中获取用户名
我尝试了一些代码,如:
System.Security.Principal.WindowsPrincipal= System.Threading.Thread.CurrentPrincipal as System.Security.Principal.WindowsPrincipal; string strName = p.Identity.Name;
要么
string strName = HttpContext.Current.User.Identity.Name.ToString();
要么
string Name = Environment.GetEnvironmentvariable("USERNAME");
要么
string Name = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
要么
WindowsIdentity currentIdentity = WindowsIdentity.GetCurrent(); string usernamex = currentIdentity.Name.ToString();
要么
HttpContext.Current.User.Identity.Name.ToString();
解决方法
1) Double click on Authentication (This can be done at server level and your web site level so check both!) 2) Right click on Anonymous authentication and disable it and likewise enable Windows authentication 3) Click Basic settings. Check which application pool your website is using. Ensure it is using pass-through authentication for an "Application User" by clicking "Connect As" i.e. A user using a browser will be the person requesting authentication. 4) Click "Application Pools". Click the Application pool from (3). Click "Advanced Settings". Check the Identity is set to ApplicationPoolIdentity
JS 获取当前用户在 win 系统的临时文件夹路径
<script>
var WshShell = new ActiveXObject( "WScript.Shell ");
var Desktop = WshShell.SpecialFolders( "Desktop ");
alert(Desktop);
</script>
<SCRIPT>
var fso = new ActiveXObject("Scripting.FileSystemObject");
var tmppath = fso.GetSpecialFolder(2);
alert (tmppath);
</SCRIPT>
一、功能实现核心:FileSystemObject 对象 要在 javascript 中实现文件操作功能,主要就是依靠 FileSystemobject 对象。 二、FileSystemObject 编程 使用 FileSystemObject 对象进行编程很简单,一般要经过如下的步骤: 创建 FileSystemObject 对象、应用相关方法、访问对象相关属性 。 (一)创建 FileSystemObject 对象 创建 FileSystemObject 对象的 代码只要 1 行: var fso = new ActiveXObject ("Scripting.FileSystemObject"); 上述代码执行后,fso 就成为一个 FileSystemObject 对象实例。 (二)应用相关方法 创建对象实例后,就可以使用对象的相关方法了。比如,使用 CreateTextFile 方法创建一个文本文件: var fso = new ActiveXObject ("Scripting.FileSystemObject"); var f1 = fso.createtextfile ("c:\\myjstest.txt",true"); (三)访问对象相关属性 要访问对象的相关属性,首先要建立指向对象的句柄,这就要通过 get 系列方法实现:GetDrive 负责获取驱动器信息,GetFolder 负责获取文件夹信息,GetFile 负责获取文件信息。比如,指向下面的代码后,f1 就成为指向文件 c:\test.txt 的句柄: var fso = new ActiveXObject ("Scripting.FileSystemObject"); var f1 = fso.GetFile ("c:\\myjstest.txt"); 然后,使用 f1 访问对象的相关属性。比如: var fso = new ActiveXObject ("Scripting.FileSystemObject"); var f1 = fso.GetFile ("c:\\myjstest.txt"); alert ("File last modified: "+ f1.DateLastModified); 执行上面最后一句后,将显示 c:\myjstest.txt 的最后修改日期属性值。 但有一点请注意:对于使用 create 方法建立的对象,就不必再使用 get 方法获取对象句柄了,这时直接使用 create 方法建立的句柄名称就可以: var fso = new ActiveXObject ("Scripting.FileSystemObject"); var f1 = fso.createtextfile ("c:\\myjstest.txt",true"); alert ("File last modified:" + f1.DateLastModified); 三、操作驱动器(Drives) 使用 FileSystemObject 对象来编程操作驱动器(Drives)和文件夹(Folders)很容易,这就象在 Windows 文件浏览器中对文件进行交互操作一样,比如:拷贝、移动文件夹,获取文件夹的属性。 (一)Drives 对象属性 Drive 对象负责收集系统中的物理或逻辑驱动器资源内容,它具有如下属性: l TotalSize:以字节(byte)为单位计算的驱动器大小。 l AvailableSpace 或 FreeSpace:以字节(byte)为单位计算的驱动器可用空间。 l DriveLetter:驱动器字母。 l DriveType:驱动器类型,取值为:removable(移动介质)、fixed(固定介质)、network(网络资源)、CD-ROM 或者 RAM 盘。 l SerialNumber:驱动器的系列码。 l FileSystem:所在驱动器的文件系统类型,取值为 FAT、FAT32 和 NTFS。 l IsReady:驱动器是否可用。 l ShareName:共享名称。 l VolumeName:卷标名称。 l Path 和 RootFolder:驱动器的路径或者根目录名称。 (二)Drive 对象操作例程 下面的例程显示驱动器 C 的卷标、总容量和可用空间等信息: var fso, drv, s =""; fso = new ActiveXObject ("Scripting.FileSystemObject"); drv = fso.GetDrive (fso.GetDriveName ("c:\\")); s +="Drive C:"+" - "; s += drv.VolumeName +"\n"; s +="Total Space: "+ drv.TotalSize/ 1024; s +=" Kb"+"\n"; s +="Free Space: "+ drv.FreeSpace/ 1024; s +=" Kb"+"\n"; alert (s); 四、操作文件夹(Folders) 涉及到文件夹的操作包括创建、移动、删除以及获取相关属性。 Folder 对象操作例程:下面的例程将练习获取父文件夹名称、创建文件夹、删除文件夹、判断是否为根目录等操作: var fso, fldr, s = ""; // 创建 FileSystemObject 对象实例 fso = new ActiveXObject ("Scripting.FileSystemObject"); // 获取 Drive 对象 fldr = fso.GetFolder ("c:\\"); // 显示父目录名称 alert ("Parent folder name is: "+ fldr +"\n"); // 显示所在 drive 名称 alert ("Contained on drive "+ fldr.Drive +"\n"); // 判断是否为根目录 if (fldr.IsRootFolder) alert ("This is the root folder."); else alert ("This folder isn''t a root folder."); alert ("\n\n"); // 创建新文件夹 fso.CreateFolder ("C:\\Bogus"); alert ("Created folder C:\\Bogus"+"\n"); // 显示文件夹基础名称,不包含路径名 alert ("Basename = "+ fso.GetBaseName ("c:\\bogus") +"\n"); // 删除创建的文件夹 fso.DeleteFolder ("C:\\Bogus"); alert ("Deleted folder C:\\Bogus"+"\n"); 五、操作文件(Files) 对文件进行的操作要比以上介绍的驱动器(Drive)和文件夹(Folder)操作复杂些,基本上分为以下两个类别:对文件的创建、拷贝、移动、删除操作和对文件内容的创建、添加、删除和读取操作。下面分别详细介绍。 (一)创建文件 一共有 3 种方法可用于创建一个空文本文件,这种文件有时候也叫做文本流(text stream)。 第一种是使用 CreateTextFile 方法。代码如下: var fso, f1; fso = new ActiveXObject ("Scripting.FileSystemObject"); f1 = fso.CreateTextFile ("c:\\testfile.txt", true); 第二种是使用 OpenTextFile 方法,并添加上 ForWriting 属性,ForWriting 的值为 2。代码如下: var fso, ts; var ForWriting= 2; fso = new ActiveXObject ("Scripting.FileSystemObject"); ts = fso.OpenTextFile ("c:\\test.txt", ForWriting, true); 第三种是使用 OpenAsTextStream 方法,同样要设置好 ForWriting 属性。代码如下: var fso, f1, ts; var ForWriting = 2; fso = new ActiveXObject ("Scripting.FileSystemObject"); fso.CreateTextFile ("c:\\test1.txt"); f1 = fso.GetFile ("c:\\test1.txt"); ts = f1.OpenAsTextStream (ForWriting, true); (二)添加数据到文件 当文件被创建后,一般要按照 “打开文件-> 填写数据-> 关闭文件” 的步骤实现添加数据到文件的目的。 打开文件可使用 FileSystemObject 对象的 OpenTextFile 方法,或者使用 File 对象的 OpenAsTextStream 方法。 填写数据要使用到 TextStream 对象的 Write、WriteLine 或者 WriteBlankLines 方法。在同是实现写入数据的功能下,这 3 者的区别在于:Write 方法不在写入数据末尾添加新换行符,WriteLine 方法要在最后添加一个新换行符,而 WriteBlankLines 则增加一个或者多个空行。 关闭文件可使用 TextStream 对象的 Close 方法。 (三)创建文件及添加数据例程 下面的代码将创建文件、添加数据、关闭文件几个步骤结合起来进行应用: var fso, tf; fso = new ActiveXObject ("Scripting.FileSystemObject"); // 创建新文件 tf = fso.CreateTextFile ("c:\\testfile.txt", true); // 填写数据,并增加换行符 tf.WriteLine ("Testing 1, 2, 3.") ; // 增加 3 个空行 tf.WriteBlankLines (3) ; // 填写一行,不带换行符 tf.Write ("This is a test."); // 关闭文件 tf.Close (); (四)读取文件内容 从文本文件中读取数据要使用 TextStream 对象的 Read、ReadLine 或 ReadAll 方法。Read 方法用于读取文件中指定数量的字符; ReadLine 方法读取一整行,但不包括换行符;ReadAll 方法则读取文本文件的整个内容。读取的内容存放于字符串变量中,用于显示、分析。 方法或者属性 描述 BuildPath () 生成一个文件路径 CopyFile () 复制文件 CopyFolder () 复制目录 CreateFolder () 创建新目录 CreateTextFile () 生成一个文件 DeleteFile () 删除一个文件 DeleteFolder () 删除一个目录 DriveExists () 检验盘符是否存在 Drives 返回盘符的集合 FileExists () 检验文件是否存在 FolderExists 检验一个目录是否存在 GetAbsolutePathName () 取得一个文件的绝对路径 GetBaseName () 取得文件名 GetDrive () 取得盘符名 GetDriveName () 取得盘符名 GetExtensionName () 取得文件的后缀 GetFile () 生成文件对象 GetFileName () 取得文件名 GetFolder () 取得目录对象 GetParentFolderName 取得文件或目录的父目录名 GetSpecialFolder () 取得特殊的目录名 GetTempName () 生成一个临时文件对象 MoveFile () 移动文件 MoveFolder () 移动目录 OpenTextFile () 打开一个文件流 f.Files// 目录下所有文件集合 f.attributes// 文件属性 Case 0 Str="普通文件。没有设置任何属性。" Case 1 Str="只读文件。可读写。" Case 2 Str="隐藏文件。可读写。" Case 4 Str="系统文件。可读写。" Case 16 Str="文件夹或目录。只读。" Case 32 Str="上次备份后已更改的文件。可读写。" Case 1024 Str="链接或快捷方式。只读。" Case 2048 Str="压缩文件。只读。" f.Datecreated// 创建时间 f.DateLastAccessed// 上次访问时间 f.DateLastModified// 上次修改时间 f.Path// 文件路径 f.Name// 文件名称 f.Type// 文件类型 f.Size// 文件大小(单位:字节) f.ParentFolder// 父目录 f.RootFolder// 根目录
//获取当前文件目录下所有文件路径
searchFiles = function() {
// alert("window.location.href:"+window.location.href);
// alert("window.location:"+window.location);
// alert("location.href:"+location.href);
// alert("parent.location.href:"+parent.location.href);
// alert("top.location.href:"+top.location.href);
// alert("document.location.href:"+document.location.href);
// alert("document.URL:"+document.URL);
// 获取当前文件所在路径
// 方法一
// var str = location.href;
// var arr = str.split("/");
// delete arr[arr.length - 1];
// var dir = arr.join("/");
// alert(dir);
// 获取当前文件所在路径
// 方法二
var path = document.URL;
path = path.substr(0, path.lastIndexOf("\\") + 1);
path = path.substr(path.indexOf("//") + 2, path.length);
alert(path);
var fso = new ActiveXObject("Scripting.FileSystemObject");
var f = fso.GetFolder(path + "image\\");
var fc = new Enumerator(f.files);
// 以下内容是显示文件名
for (; !fc.atEnd(); fc.moveNext()) {
document.write(fc.item() + "<br>");
}
// 以下内容是显示目录名
fk = new Enumerator(f.SubFolders);
for (; !fk.atEnd(); fk.moveNext()) {
document.write(fk.item() + "<br>");
}
};
//BuildPath(路径,文件名)
//这个方法会对给定的路径加上文件,并自动加上分界符
testBuildPath = function(path, filesName) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var newpath = fso.BuildPath("c:\\tmp", "51js.txt"); // 生成 c:\tmp\51js.txt的路径
alert(newpath);
};
// CopyFile(源文件, 目标文件, 覆盖)
// 复制源文件到目标文件,当覆盖值为true时,如果目标文件存在会把文件覆盖
testCopyFile = function(oldpath, newpath) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var newpath = fso.CopyFile("c:\\autoexec.bat", "d:\\autoexec.bak");
alert(newpath);
};
// CopyFolder(对象目录,目标目录 ,覆盖)
// 复制对象目录到目标目录,当覆盖为true时,如果目标目录存在会把文件覆盖
testCopyFolder = function(oldpath, newpath) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
fso.CopyFolder("c:\\WINDOWS\\Desktop", "d:\\"); // 把C盘的Desktop目录复制到D盘的根目录
};
// CreateFolder(目录名)
// 创建一个新的目录
testCopyFolder = function(newpath) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var newFolderName = fso.CreateFolder("c:\\51JS"); // 在C盘上创建一个51JS的目录
alert(newFolderName);
};
// CreateTextFile(文件名, 覆盖)
// 创建一个新的文件,如果此文件已经存在,你需要把覆盖值定为true
testCreateTextFile = function(oldpath, newpath) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
// 脚本将在C盘创建一个叫autoexec51JS.bat的文件
var newFileObject = fso.CreateTextFile("c:\\autoexec51JS.bat", true);
alert(newFileObject);
};
// DeleteFile(文件名, 只读?)
// 删除一个文件,如果文件的属性是只读的话,你需要把只读值设为true
testDeleteFile = function(oldpath, newpath) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
// 为了安全我先把要删除的autoexec.bat备份到你的D盘
var newpath = fso.CopyFile("c:\\autoexec.bat", "d:\\autoexec.bat");
alert(newpath);
// 把C盘的autoexec.bat文件删除掉
fso.DeleteFile("c:\\autoexec.bat", true);
};
// DeleteFolder(文件名, 只读?)
// 删除一个目录,如果目录的属性是只读的话,你需要把只读值设为true
testDeleteFolder = function(oldpath, newpath) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
// 为了安全我先把你C盘的Desktop目录复制到你D盘的根目录
fso.CopyFolder("c:\\WINDOWS\\Desktop", "d:\\");
// 把你的Desktop目录删除,但因为desktop是系统的东西,所以不能全部删除,但.........
fso.DeleteFolder("c:\\WINDOWS\\Desktop", true);
};
// DriveExists(盘符)
// 检查一个盘是否存在,如果存在就返会真,不存在就返回.......
testDriveExists = function(diskDrive) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
hasDriveD = fso.DriveExists("d"); // 检查系统是否有D盘存在
hasDriveZ = fso.DriveExists("z"); // 检查系统是否有Z盘存在
if (hasDriveD)
alert("你的系统内有一个D盘");
if (!hasDriveZ)
alert("你的系统内没有Z盘");
};
// FileExists(文件名)
// 检查一个文件是否存在,如果存在就返会真,不存在就返回.......
testFileExists = function(filePath) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
fileName = fso.FileExists("c:\\autoexec.bat");
if (fileName)
alert("你在C盘中有autoexec.bat文件,按下确定后这个文件将被删除!"); // 开个玩笑:)
};
// FolderExists(目录名)
// 检查一个目录是否存在,如果存在就返会真,不存在就返回.......
testFolderExists = function(folderPath) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
folderName = fso.FolderExists("c:\\WINDOWS\\Fonts");
if (folderName)
alert("按下确定后系统的字库将被删除!"); // 开个玩笑:)
};
// GetAbsolutePathName(文件对象)
// 返回文件对象在系统的绝对路径
testGetAbsolutePathName = function(filePath) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
pathName = fso.GetAbsolutePathName("c:\\autoexec.bat");
alert(pathName);
};
// GetBaseName(文件对象)
// 返回文件对象的文件名
testGetBaseName = function(filePath) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
baseName = fso.GetBaseName("c:\\autoexec.bat"); // 取得autoexec.bat的文件名autoexec
alert(baseName);
};
// GetExtensionName(文件对象)
// 文件的后缀
testGetExtensionName = function(filePath) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
exName = fso.GetExtensionName("c:\\autoexec.bat"); // 取得autoexec.bat后缀bat
alert(exName);
};
// GetParentFolderName(文件对象)
// 取得父级的目录名
testGetParentFolderName = function(filePath) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
parentName = fso.GetParentFolderName("c:\\autoexec.bat"); // 取得autoexec.bat的父级目录C盘
alert(parentName);
};
// GetSpecialFolder(目录代码)
// 取得系统中一些特别的目录的路径,目录代码有3个分别是 0:安装Window的目录 1:系统文件目录 2:临时文件目录
testGetSpecialFolder = function(code) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
tmpFolder = fso.GetSpecialFolder(2); // 取得系统临时文件目录的路径 如我的是
// C:\windows\temp
alert(tmpFolder);
};
// GetTempName()
// 生成一个随机的临时文件对象,会以rad带头后面跟着些随机数,就好象一些软件在安装时会生成*.tmp
testGetSpecialFolder = function() {
var fso = new ActiveXObject("Scripting.FileSystemObject");
tmpName = fso.GetTempName(); // 我在测试时就生成了radDB70E.tmp
alert(tmpName);
};
// MoveFile(源文件, 目标文件)
// 把源文件移到目标文件的位置
testMoveFile = function(oldpath, newpath) {
var fso = new ActiveXObject("Scripting.FileSystemObject");
var newpath = fso.MoveFile("c:\\autoexec.bat", "d:\\autoexec.bat"); // 把C盘的autoexec.bat文件移移动到D盘
alert(newpath);
};
我们今天的关于如何获取当前用户在Windowslogin会话的唯一ID –和如何获取当前登录的用户名称的分享已经告一段落,感谢您的关注,如果您想了解更多关于c# windows 窗体,当我以管理员身份运行时,我无法获取当前用户名、c# – 在Windows 7中获取当前系统卷、c# – 如何在asp.net中获取当前的Windows用户?、JS 获取当前用户在 win 系统的临时文件夹路径的相关信息,请在本站查询。
本文标签: