本文整理汇总了C#中StringCollection.CopyTo方法的典型用法代码示例。如果您正苦于以下问题:C# StringCollection.CopyTo方法的具体用法?C# StringCollection.CopyTo怎么用?C# StringCollection.CopyTo使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类StringCollection
的用法示例。
在下文中一共展示了StringCollection.CopyTo方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RelativePathTo
/// <summary>
/// Creates a relative path from one file or folder to another.
/// </summary>
/// <param name="fromDirectory">Contains the directory that defines the start of the relative path.</param>
/// <param name="toPath">Contains the path that defines the endpoint of the relative path.</param>
/// <returns>The relative path from the start directory to the end path.</returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="ArgumentException"></exception>
public static string RelativePathTo(string fromDirectory, string toPath)
{
if(fromDirectory == null)
throw new ArgumentNullException("fromDirectory");
if(toPath == null)
throw new ArgumentNullException("fromDirectory");
if(System.IO.Path.IsPathRooted(fromDirectory) && System.IO.Path.IsPathRooted(toPath))
{
if(string.Compare(System.IO.Path.GetPathRoot(fromDirectory),
System.IO.Path.GetPathRoot(toPath), true) != 0)
{
throw new ArgumentException(
string.Format("The paths '{0} and '{1}' have different path roots.",
fromDirectory, toPath));
}
}
StringCollection relativePath = new StringCollection();
string[] fromDirectories = fromDirectory.Split(System.IO.Path.DirectorySeparatorChar);
string[] toDirectories = toPath.Split(System.IO.Path.DirectorySeparatorChar);
int length = Math.Min(fromDirectories.Length, toDirectories.Length);
int lastCommonRoot = -1;
// find common root
for(int x = 0; x < length; x++)
{
if(string.Compare(fromDirectories[x], toDirectories[x], true) != 0)
break;
lastCommonRoot = x;
}
if(lastCommonRoot == -1)
{
throw new ArgumentException(
string.Format("The paths '{0} and '{1}' do not have a common prefix path.",
fromDirectory, toPath));
}
// add relative folders in from path
for(int x = lastCommonRoot + 1; x < fromDirectories.Length; x++)
if(fromDirectories[x].Length > 0)
relativePath.Add("..");
// add to folders to path
for(int x = lastCommonRoot + 1; x < toDirectories.Length; x++)
relativePath.Add(toDirectories[x]);
// create relative path
string[] relativeParts = new string[relativePath.Count];
relativePath.CopyTo(relativeParts, 0);
string newPath = string.Join(System.IO.Path.DirectorySeparatorChar.ToString(), relativeParts);
return newPath;
}
示例2: Main
static void Main(string[] args)
{
if (args.Length != 2)
Environment.Exit(1);
string ip = args[0];
string hostname = args[1];
try {
String hosts = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\drivers\etc\hosts");
string[] lines = File.ReadAllLines(hosts);
StringCollection linesToSave = new StringCollection();
foreach (string l in lines) {
if (!l.Contains(hostname) && !l.Contains(ip)) {
linesToSave.Add(l);
}
}
if (!".".Equals(ip))
linesToSave.Add(ip + "\t" + hostname);
lines = new String[linesToSave.Count];
linesToSave.CopyTo(lines, 0);
File.WriteAllLines(hosts, lines);
} catch (Exception e) {
Console.WriteLine("{0}", e);
}
}
示例3: CopyTo_ArgumentInvalidTest
public static void CopyTo_ArgumentInvalidTest(StringCollection collection, string[] data)
{
Assert.Throws<ArgumentNullException>(() => collection.CopyTo(null, 0));
Assert.Throws<ArgumentOutOfRangeException>(() => collection.CopyTo(data, -1));
if (data.Length > 0)
{
Assert.Throws<ArgumentException>(() => collection.CopyTo(new string[0], data.Length - 1));
Assert.Throws<ArgumentException>(() => collection.CopyTo(new string[data.Length - 1], 0));
}
// As explicit interface implementation
Assert.Throws<ArgumentNullException>(() => ((ICollection)collection).CopyTo(null, 0));
Assert.Throws<ArgumentOutOfRangeException>(() => ((ICollection)collection).CopyTo(data, -1));
if (data.Length > 0)
{
Assert.Throws<ArgumentException>(() => ((ICollection)collection).CopyTo(new string[0], data.Length - 1));
Assert.Throws<ArgumentException>(() => ((ICollection)collection).CopyTo(new string[data.Length - 1], 0));
}
}
示例4: CopyToTest
public static void CopyToTest(StringCollection collection, string[] data)
{
string[] full = new string[data.Length];
collection.CopyTo(full, 0);
Assert.Equal(data, full);
string[] large = new string[data.Length * 2];
collection.CopyTo(large, data.Length / 4);
for (int i = 0; i < large.Length; i++)
{
if (i < data.Length / 4 || i >= data.Length + data.Length / 4)
{
Assert.Null(large[i]);
}
else
{
Assert.Equal(data[i - data.Length / 4], large[i]);
}
}
}
示例5: runTest
public virtual bool runTest()
{
Console.WriteLine(s_strTFPath + " " + s_strTFName + " , for " + s_strClassMethod + " , Source ver: " + s_strDtTmVer);
int iCountErrors = 0;
int iCountTestcases = 0;
IntlStrings intl;
String strLoc = "Loc_000oo";
StringCollection sc;
string [] values =
{
"",
" ",
"a",
"aa",
"text",
" spaces",
"1",
"$%^#",
"2222222222222222222222222",
System.DateTime.Today.ToString(),
Int32.MaxValue.ToString()
};
string[] destination;
int cnt = 0;
try
{
intl = new IntlStrings();
Console.WriteLine("--- create collection ---");
strLoc = "Loc_001oo";
iCountTestcases++;
sc = new StringCollection();
Console.WriteLine("1. Copy empty collection into empty array");
iCountTestcases++;
destination = new string[values.Length];
for (int i = 0; i < values.Length; i++)
{
destination[i] = "";
}
sc.CopyTo(destination, 0);
if( destination.Length != values.Length)
{
iCountErrors++;
Console.WriteLine("Err_0001a, altered array after copying empty collection");
}
if (destination.Length == values.Length)
{
for (int i = 0; i < values.Length; i++)
{
iCountTestcases++;
if (String.Compare(destination[i], "", false) != 0)
{
iCountErrors++;
Console.WriteLine("Err_0001_{0}b, item = \"{1}\" insteead of \"{2}\" after copying empty collection", i, destination[i], "");
}
}
}
Console.WriteLine("2. Copy empty collection into non-empty array");
iCountTestcases++;
destination = values;
sc.CopyTo(destination, 0);
if( destination.Length != values.Length)
{
iCountErrors++;
Console.WriteLine("Err_0002a, altered array after copying empty collection");
}
if (destination.Length == values.Length)
{
for (int i = 0; i < values.Length; i++)
{
iCountTestcases++;
if (String.Compare(destination[i], values[i], false) != 0)
{
iCountErrors++;
Console.WriteLine("Err_0002_{0}b, altered item {0} after copying empty collection", i);
}
}
}
Console.WriteLine("3. add simple strings and CopyTo([], 0)");
strLoc = "Loc_003oo";
iCountTestcases++;
cnt = sc.Count;
sc.AddRange(values);
if (sc.Count != values.Length)
{
iCountErrors++;
Console.WriteLine("Err_0003a, count is {0} instead of {1}", sc.Count, values.Length);
}
destination = new string[values.Length];
sc.CopyTo(destination, 0);
for (int i = 0; i < values.Length; i++)
{
iCountTestcases++;
if ( String.Compare(sc[i], destination[i], false) != 0 )
{
iCountErrors++;
Console.WriteLine("Err_0003_{0}b, copied \"{1}\" instead of \"{2}\"", i, destination[i], sc[i]);
}
}
Console.WriteLine("4. add simple strings and CopyTo([], {0})", values.Length);
sc.Clear();
//.........这里部分代码省略.........
示例6: deleteBookmark
private void deleteBookmark(int num)
{
StringCollection sc = new StringCollection();
FileStream aFile = new FileStream(bookmarkFile, FileMode.Open);
StreamReader sr = new StreamReader(aFile);
string strLine = sr.ReadLine();
while(strLine != null) {
sc.Add(strLine);
strLine = sr.ReadLine();
}
sr.Close();
aFile.Close();
if (num - 1 > -1) {
sc.RemoveAt(num - 1);
String[] bookmarks = new String[sc.Count];
sc.CopyTo(bookmarks, 0);
File.WriteAllLines(bookmarkFile, bookmarks);
}
}
示例7: VEventParser
//.........这里部分代码省略.........
vEvent.Status.DeserializeParameters(parameters);
vEvent.Status.EncodedValue = propertyValue;
break;
case PropertyType.RequestStatus:
RequestStatusProperty rs = new RequestStatusProperty();
rs.DeserializeParameters(parameters);
rs.EncodedValue = propertyValue;
vEvent.RequestStatuses.Add(rs);
break;
case PropertyType.Duration:
vEvent.Duration.DeserializeParameters(parameters);
vEvent.Duration.EncodedValue = propertyValue;
break;
case PropertyType.AudioAlarm:
case PropertyType.DisplayAlarm:
case PropertyType.EMailAlarm:
case PropertyType.ProcedureAlarm:
// These are converted to a VAlarm object
vAlarm = new VAlarm();
ParseVCalendarAlarm(ntvEvent[idx].EnumValue, parameters, propertyValue);
vEvent.Alarms.Add(vAlarm);
vAlarm = null;
break;
case PropertyType.RecurrenceRule:
RRuleProperty rr = new RRuleProperty();
rr.DeserializeParameters(parameters);
rr.EncodedValue = propertyValue;
vEvent.RecurrenceRules.Add(rr);
break;
case PropertyType.RecurDate:
// There may be more than one date in the value. If so, split them into separate ones. This
// makes it easier to manage. They'll get written back out as individual properties but
// that's okay.
parts = propertyValue.Split(',', ';');
// It's important that we retain the same parameters for each one
parms = new string[parameters.Count];
parameters.CopyTo(parms, 0);
foreach(string s in parts)
{
sc = new StringCollection();
sc.AddRange(parms);
RDateProperty rd = new RDateProperty();
rd.DeserializeParameters(sc);
rd.EncodedValue = s;
vEvent.RecurDates.Add(rd);
}
break;
case PropertyType.ExceptionRule:
ExRuleProperty er = new ExRuleProperty();
er.DeserializeParameters(parameters);
er.EncodedValue = propertyValue;
vEvent.ExceptionRules.Add(er);
break;
case PropertyType.ExceptionDate:
// There may be more than one date in the value. If so, split them into separate ones. This
// makes it easier to manage. They'll get written back out as individual properties but
// that's okay.
parts = propertyValue.Split(',', ';');
// It's important that we retain the same parameters for each one
parms = new string[parameters.Count];
parameters.CopyTo(parms, 0);
foreach(string s in parts)
{
sc = new StringCollection();
sc.AddRange(parms);
ExDateProperty ed = new ExDateProperty();
ed.DeserializeParameters(sc);
ed.EncodedValue = s;
vEvent.ExceptionDates.Add(ed);
}
break;
case PropertyType.ExcludeStartDateTime:
// This is a custom property not defined by the spec
vEvent.ExcludeStartDateTime = (propertyValue[0] == '1');
break;
default: // Anything else is a custom property
CustomProperty cust = new CustomProperty(propertyName);
cust.DeserializeParameters(parameters);
cust.EncodedValue = propertyValue;
vEvent.CustomProperties.Add(cust);
break;
}
}
示例8: ObservanceRuleParser
/// <summary>
/// This is implemented to handle properties related to observance rule items in VTimeZone objects
/// </summary>
/// <param name="propertyName">The name of the property.</param>
/// <param name="parameters">A string collection containing the parameters and their values. If empty,
/// there are no parameters.</param>
/// <param name="propertyValue">The value of the property.</param>
protected virtual void ObservanceRuleParser(string propertyName, StringCollection parameters, string propertyValue)
{
StringCollection sc;
string[] parts, parms;
int idx;
// The last entry is always CustomProperty so scan for length minus one
for(idx = 0; idx < ntvORule.Length - 1; idx++)
if(ntvORule[idx].IsMatch(propertyName))
break;
// An opening BEGIN:STANDARD or BEGIN:DAYLIGHT property must have been seen.
if(obsRule == null)
throw new PDIParserException(this.LineNumber, LR.GetString("ExParseNoBeginProp",
"BEGIN:STANDARD/BEGIN:DAYLIGHT", propertyName));
// Handle or create the property
switch(ntvORule[idx].EnumValue)
{
case PropertyType.Begin: // Handle unknown nested objects
priorState.Push(currentState);
currentState = VCalendarParserState.Custom;
CustomObjectParser(propertyName, parameters, propertyValue);
break;
case PropertyType.End:
// For this, the value must be STANDARD or DAYLIGHT depending on the rule type
if((obsRule.RuleType == ObservanceRuleType.Standard &&
String.Compare(propertyValue.Trim(), "STANDARD", StringComparison.OrdinalIgnoreCase) != 0) ||
(obsRule.RuleType == ObservanceRuleType.Daylight &&
String.Compare(propertyValue.Trim(), "DAYLIGHT", StringComparison.OrdinalIgnoreCase) != 0))
{
throw new PDIParserException(this.LineNumber, LR.GetString("ExParseUnrecognizedTagValue",
ntvORule[idx].Name, propertyValue));
}
// The rule is added to the collection when created so we don't have to rely on an END tag to
// add it.
obsRule = null;
currentState = priorState.Pop();
break;
case PropertyType.StartDateTime:
obsRule.StartDateTime.DeserializeParameters(parameters);
obsRule.StartDateTime.EncodedValue = propertyValue;
break;
case PropertyType.TimeZoneOffsetFrom:
obsRule.OffsetFrom.DeserializeParameters(parameters);
obsRule.OffsetFrom.EncodedValue = propertyValue;
break;
case PropertyType.TimeZoneOffsetTo:
obsRule.OffsetTo.DeserializeParameters(parameters);
obsRule.OffsetTo.EncodedValue = propertyValue;
break;
case PropertyType.Comment:
// If this is seen more than once, just add the new stuff to the existing property
if(obsRule.Comment.Value != null)
{
obsRule.Comment.EncodedValue += "\r\n";
obsRule.Comment.EncodedValue += propertyValue;
}
else
{
obsRule.Comment.DeserializeParameters(parameters);
obsRule.Comment.EncodedValue = propertyValue;
}
break;
case PropertyType.RecurrenceRule:
RRuleProperty rr = new RRuleProperty();
rr.DeserializeParameters(parameters);
rr.EncodedValue = propertyValue;
obsRule.RecurrenceRules.Add(rr);
break;
case PropertyType.RecurDate:
// There may be more than one date in the value. If so, split them into separate ones. This
// makes it easier to manage. They'll get written back out as individual properties but
// that's okay.
parts = propertyValue.Split(',', ';');
// It's important that we retain the same parameters for each one
parms = new string[parameters.Count];
parameters.CopyTo(parms, 0);
foreach(string s in parts)
{
sc = new StringCollection();
sc.AddRange(parms);
//.........这里部分代码省略.........
示例9: VFreeBusyParser
//.........这里部分代码省略.........
break;
case PropertyType.Url:
vFreeBusy.Url.DeserializeParameters(parameters);
vFreeBusy.Url.EncodedValue = propertyValue;
break;
case PropertyType.UniqueId:
vFreeBusy.UniqueId.EncodedValue = propertyValue;
break;
case PropertyType.StartDateTime:
vFreeBusy.StartDateTime.DeserializeParameters(parameters);
vFreeBusy.StartDateTime.EncodedValue = propertyValue;
break;
case PropertyType.EndDateTime:
vFreeBusy.EndDateTime.DeserializeParameters(parameters);
vFreeBusy.EndDateTime.EncodedValue = propertyValue;
break;
case PropertyType.Duration:
vFreeBusy.Duration.DeserializeParameters(parameters);
vFreeBusy.Duration.EncodedValue = propertyValue;
break;
case PropertyType.TimeStamp:
vFreeBusy.TimeStamp.DeserializeParameters(parameters);
vFreeBusy.TimeStamp.EncodedValue = propertyValue;
break;
case PropertyType.Comment:
// If this is seen more than once, just add the new stuff to the existing property
if(vFreeBusy.Comment.Value != null)
{
vFreeBusy.Comment.EncodedValue += "\r\n";
vFreeBusy.Comment.EncodedValue += propertyValue;
}
else
{
vFreeBusy.Comment.DeserializeParameters(parameters);
vFreeBusy.Comment.EncodedValue = propertyValue;
}
break;
case PropertyType.Contact:
vFreeBusy.Contact.DeserializeParameters(parameters);
vFreeBusy.Contact.EncodedValue = propertyValue;
break;
case PropertyType.Organizer:
vFreeBusy.Organizer.DeserializeParameters(parameters);
vFreeBusy.Organizer.EncodedValue = propertyValue;
break;
case PropertyType.Attendee:
AttendeeProperty ap = new AttendeeProperty();
ap.DeserializeParameters(parameters);
ap.EncodedValue = propertyValue;
vFreeBusy.Attendees.Add(ap);
break;
case PropertyType.RequestStatus:
RequestStatusProperty rs = new RequestStatusProperty();
rs.DeserializeParameters(parameters);
rs.EncodedValue = propertyValue;
vFreeBusy.RequestStatuses.Add(rs);
break;
case PropertyType.FreeBusy:
// There may be more than one period in the value. If so, split them into separate ones.
// This makes it easier to manage. They'll get written back out as individual properties but
// that's okay.
parts = propertyValue.Split(',', ';');
// It's important that we retain the same parameters for each one
parms = new string[parameters.Count];
parameters.CopyTo(parms, 0);
foreach(string s in parts)
{
sc = new StringCollection();
sc.AddRange(parms);
FreeBusyProperty fb = new FreeBusyProperty();
fb.DeserializeParameters(sc);
fb.EncodedValue = s;
vFreeBusy.FreeBusy.Add(fb);
}
break;
default: // Anything else is a custom property
CustomProperty cust = new CustomProperty(propertyName);
cust.DeserializeParameters(parameters);
cust.EncodedValue = propertyValue;
vFreeBusy.CustomProperties.Add(cust);
break;
}
}
示例10: ReadResponseFile
private static string[] ReadResponseFile(string strFileName){
FileStream inputStream = new FileStream(strFileName, FileMode.Open,
FileAccess.Read, FileShare.Read);
Int64 size = inputStream.Length;
if (size == 0)
return null;
StreamReader reader = new StreamReader(inputStream);
string curLineArgs = reader.ReadLine();
// This regular expression is: blank*(nonblanks|stringLiteral)+
string strReArgs = "\\s*([^\\s\\\"]|(\\\"[^\\\"\\n]*\\\"))+";
Regex re = new Regex(strReArgs);
StringCollection args = new StringCollection();
while (curLineArgs != null){
if (!curLineArgs.Trim().StartsWith("#", StringComparison.Ordinal)){
MatchCollection matches = re.Matches(curLineArgs);
if (matches != null && matches.Count != 0){
foreach (Match match in matches){
string arg = match.ToString().Trim();
int iQuotes = 0;
while ((iQuotes = arg.IndexOf("\"", iQuotes)) != -1){
if (iQuotes == 0)
arg = arg.Substring(1);
else if (arg[iQuotes-1] == '\\')
iQuotes += 1;
else
arg = arg.Remove(iQuotes, 1);
}
args.Add(arg);
}
}
}
curLineArgs = reader.ReadLine();
}
if (args.Count == 0)
return null;
string[] argStrings = new string[args.Count];
args.CopyTo(argStrings, 0);
return argStrings;
}