用C#代码修改FF和IE的Script状态
2009-05-14 08:28:43 来源:WEB开发网附下全部源码供大家分享。包括对当前文件路径的组合,文件读取,增删改操作。IE,FF,状态的读取,改变。
有一切问题都欢迎拍砖:)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Security.Principal;
using Microsoft.Win32;
namespace FileLoadAndModify
{
public class Program
{
public static void Main(string[] args)
{
//There exists two methods to get the current user info.
//Because the folder in generate by random and the file path includes the user alias infro.
//So we have to combine the strings to generate it.
string filePath = GetUserFilePathByXMLWithoutConfig();
string regPath = @"SoftwareMicrosoftWindowsCurrentVersionInternet SettingsZones3";
//string filePath = GetUserFilePathByXMLWithConfig();
string fileName = string.Empty;
DirectoryInfo dicInfo = new DirectoryInfo(filePath);
DirectoryInfo[] dicFolder = dicInfo.GetDirectories();
foreach (DirectoryInfo dicInfoFolder in dicFolder)
{
if(dicInfoFolder.FullName.Contains(".default"))
{
filePath += dicInfoFolder.Name + @"";
}
}
dicInfo = new DirectoryInfo(filePath);
FileInfo[] filesInDir = dicInfo.GetFiles();
foreach (FileInfo file in filesInDir)
{
if (file.FullName.Contains("prefs.js"))
{
filePath += file.Name;
}
}
bool state = GetFFCurrentJavaScripState(filePath);
EnableFFJavaScript(filePath);
DisableFFJaveScript(filePath);
GetIECurrentCookiesState(regPath);
GetIECurrentScriptState(regPath);
}
/// <summary>
/// Get Current FireFox JavaScript state
/// </summary>
/// <param name="filePath"></param>
/// <returns>true ---> enable/ false ----> disabled.</returns>
public static bool GetFFCurrentJavaScripState(string filePath)
{
bool ret = false;
using (FileStream aStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
StreamReader aReader = new StreamReader(aStream, Encoding.UTF8);
aReader.BaseStream.Seek(0, SeekOrigin.Begin);
string streamLine = aReader.ReadLine();
while (streamLine != null)
{
if ((streamLine.Contains(@"javascript.enabled") && streamLine.Contains("true")) || !streamLine.Contains(@"javascript.enabled"))
{
ret = true;
}
streamLine = aReader.ReadLine();
}
aReader.Close();
}
return ret;
}
/// <summary>
/// Enable FireFox javascript
/// </summary>
/// <param name="filePath"></param>
public static void EnableFFJavaScript(string filePath)
{
StringBuilder sb = new StringBuilder();
using(FileStream aStream = new FileStream(filePath,FileMode.OpenOrCreate,FileAccess.ReadWrite))
{
StreamReader aReader = new StreamReader(aStream, Encoding.UTF8);
aReader.BaseStream.Seek(0, SeekOrigin.Begin);
string streamLine = aReader.ReadLine();
while (streamLine != null)
{
if (streamLine.Contains(@"javascript.enabled") && streamLine.Contains("false"))
{
streamLine = streamLine.Replace("false", "true");
}
sb.Append(streamLine + "rn");
streamLine = aReader.ReadLine();
}
//writeLine = sb.ToString().Split("rn".ToCharArray());
aReader.Close();
}
File.Delete(filePath);
AddToFile(filePath, sb.ToString());
}
/// <summary>
/// Delete one line which contains the "javaScript.enabled.
/// </summary>
/// <param name="filePath">the path of js file.</param>
public static void DisableFFJaveScript(string filePath)
{
StringBuilder sb = new StringBuilder();
using (FileStream aStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
StreamReader aReader = new StreamReader(aStream, Encoding.UTF8);
StreamWriter aWriter = new StreamWriter(aStream, Encoding.UTF8);
aReader.BaseStream.Seek(0, SeekOrigin.Begin);
string streamLine = aReader.ReadLine();
bool isJSDisabled = false;
bool isJSFalse = false;
while (streamLine != null)
{
if (streamLine.Contains("javascript.enable") && streamLine.Contains("false"))
{
isJSFalse = true;
}
if (streamLine.Contains("javascript.enable") && streamLine.Contains("true"))
{
streamLine = streamLine.Replace("true", "false");
isJSDisabled = true;
}
sb.Append(streamLine + "rn");
streamLine = aReader.ReadLine();
}
if (!isJSDisabled && !isJSFalse)
sb.Append("user_pref(" + ""javascript.enabled"" + "," + " false);");
aReader.Close();
}
File.Delete(filePath);
AddToFile(filePath, sb.ToString());
}
/// <summary>
/// Add some stirng to a file.
/// </summary>
/// <param name="filePath">the path of the file</param>
/// <param name="writeLine">the content of on string.</param>
public static void AddToFile(string filePath, string writeLine)
{
using (FileStream aStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
using (StreamWriter aWriter = new StreamWriter(aStream, Encoding.UTF8))
{
aWriter.Flush();
aWriter.BaseStream.Seek(0, SeekOrigin.Begin);
aWriter.Write(writeLine);
aWriter.Flush();
}
}
}
/// <summary>
/// Delete one line of file contains user_pref(,true/false)
/// </summary>
/// <param name="aStream"></param>
public static void DeleteLineFile(FileStream aStream,string filePath)
{
string streamLine = string.Empty;
StreamReader aReader = new StreamReader(aStream, Encoding.UTF8);
aReader.BaseStream.Seek(0, SeekOrigin.Begin);
string[] streamAll = aReader.ReadToEnd().Split("rn".ToCharArray());
List<string> aString = new List<string>();
foreach (string aLine in streamAll)
{
if (!aLine.Contains(@"javascript.enable"))
{
aString.Add(aLine);
}
}
aReader.Close();
aStream.Close();
File.Delete(filePath);
using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite))
{
using (StreamWriter aWriter = new StreamWriter(fs, Encoding.UTF8))
{
aWriter.BaseStream.Seek(0, SeekOrigin.Begin);
foreach (string aLine in aString)
{
aWriter.WriteLine(aLine);
}
}
}
}
/// <summary>
/// Read derictly from the XML file with some configs.
/// That means users have to reconfig the XML node then can continue the test.
/// </summary>
/// <returns></returns>
public static string GetUserFilePathByXMLWithConfig()
{
string filePath = string.Empty;
string currentUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
string[] userName = currentUser.Split(@"".ToCharArray());
currentUser = userName[userName.Length - 1];
StringBuilder sb = new StringBuilder();
string xmlFileName = @"filePath.xml";
XmlDocument aDoc = new XmlDocument();
aDoc.Load(xmlFileName);
XmlNode aNode = aDoc.SelectSingleNode("filePaths");
return aNode.SelectSingleNode("filePath").InnerText;
}
/// <summary>
/// Read derictly from the XML file without any other configuration.
/// The system infomation which contains the User Alias.
/// </summary>
/// <returns></returns>
public static string GetUserFilePathByXMLWithoutConfig()
{
string filePath = string.Empty;
string currentUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
string[] userName = currentUser.Split(@"".ToCharArray());
currentUser = userName[userName.Length - 1];
StringBuilder sb = new StringBuilder();
string xmlFileName = @"filePath.xml";
XmlDocument aDoc = new XmlDocument();
aDoc.Load(xmlFileName);
XmlNode aNode = aDoc.SelectSingleNode("filePaths");
sb.Append(aNode.SelectSingleNode("filePathPartial1").InnerText);
sb.Append(currentUser);
sb.Append(aNode.SelectSingleNode("filePathPartial2").InnerText);
return sb.ToString();
}
/// <summary>
/// Get Current Script state of IE.
/// </summary>
/// <param name="regPath"></param>
/// <returns>true ---> allow Script.</returns>
public static bool GetIECurrentScriptState(string regPath)
{
bool ret = false;
RegistryKey regSubkey = null;
regSubkey = Registry.CurrentUser.OpenSubKey(regPath, true);
if (regSubkey.GetValue("1400").ToString() == "0")
{
ret = true;
}
if ((int)regSubkey.GetValue("1400") == 3)
{
ret = false;
}
return ret;
}
/// <summary>
/// Get the current cookies state of IE.
/// </summary>
/// <param name="regPath">the registry of internet zone.</param>
/// <returns>true--->allow all cookies.</returns>
public static bool GetIECurrentCookiesState(string regPath)
{
bool ret = false;
RegistryKey regSubKey = null;
regSubKey = Registry.CurrentUser.OpenSubKey(regPath, true);
if ((int)regSubKey.GetValue("1A02") == 0)
{
ret = true;
}
if ((int)regSubKey.GetValue("1A02") == 3)
{
ret = false;
}
return ret;
}
}
}
后文:
有人就会问了,用鼠标随便点点就能够轻松实现的东西,为什么还有写这么多乱七八糟的代码呢?
我认为有几点好处:
1.本例更适合使用在测试代码中,通过此,可以计算开发代码的code coverage
2.手工要比自动化效率低下,款且存在很大的误差。
3.开发人员也可以此检测当前用户状态,以改变不同的现实效果或者提示。
目前只是发现了FF和IE的操作,并且对IE8的注册表修改还是有小小的不同。对于其他众多浏览器,大家有研究的可以给我提供些新鲜的信息和想法。一起交流学习,有疏漏的地方大家补充。
出处:http://alexliu.cnblogs.com/
更多精彩
赞助商链接