1. C# / Говнокод #6958

    +129

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    public void ReadFromFile()
    {
    	LoadIntervals = new TimeIntervals(Color.Black);
    	StreamReader tReader = null;
    	tReader = File.OpenText("DefaultIntervals");
    	string income = null;
    	while ((income = tReader.ReadLine()) != null)
    	{
    		// string income = tReader.ReadLine();
    		string colorR = null; string colorG = null; string colorB = null; string colorA = null; string interval = null;
    		int i = 0;
    		for (i = 4; i < income.Length; i++)
    		{
    			if (income[i] != ',')
    			{
    
    				interval += income[i];
    			}
    			else { break; }
    		}
    
    		for (int a = i + 10; a < income.Length; a++)
    		{
    			if (income[a] != ',')
    			{
    				colorA += income[a];
    			}
    			else
    			{
    
    				for (int r = a + 4; r < income.Length; r++)
    				{
    					if (income[r] != ',')
    					{
    						colorR += income[r];
    					}
    					else
    					{
    						for (int g = r + 4; g < income.Length; g++)
    							if (income[g] != ',')
    							{
    								colorG += income[g];
    							}
    							else
    							{
    								for (int b = g + 4; b < income.Length; b++)
    									if (income[b] != ',')
    									{
    										if (income[b] != ']')
    											colorB += income[b];
    									}
    									else
    									{
    										break;
    									}
    								break;
    							}
    						break;
    					}
    				}
    				break;
    			}
    		}
    		TimePoint tp = new TimePoint(Convert.ToDouble(interval), Color.FromArgb(Convert.ToInt32(colorA), Color.FromArgb(Convert.ToInt32(colorR), Convert.ToInt32(colorG), Convert.ToInt32(colorB))));
    		LoadIntervals.Add(tp);
    	}
    
    	tReader.Close();
    	Intervals = LoadIntervals;
    }

    Так мы парсим файл конфигов вида. (Формат придуман автором кода)
    Int=3,Color [A=223, R=253, G=4, B=38]
    Int=10,Color [A=255, R=86, G=86, B=185]
    Int=20,Color [A=200, R=255, G=215, B=0]
    Int=50,Color [A=200, R=255, G=165, B=0]

    lomomike, 15 Июня 2011

    Комментарии (49)
  2. C# / Говнокод #6955

    +128

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    if (body.ToLower().Contains("pr") || 
                    subject.Contains("PM") ||
                    subject.Contains("pm") ||
                    subject.Contains("Pm") ||
                    subject.Contains("pM") ||
                    subject.Contains("PMR") ||
                    subject.Contains("prs") || 
                    subject.ToLower().Contains("prt") ||
                    subject.ToLower().Contains("prm") ||
                    subject.ToLower().Contains("pmt") ||
                    subject.ToLower().Contains("pmk") ||
                    subject.ToLower().Contains("pml") ||
                    subject.ToLower().Contains("pal"))
                {
                    result = true;
                }

    Пишут друзья из Норвегии

    jenik15, 15 Июня 2011

    Комментарии (8)
  3. C# / Говнокод #6952

    +126

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    public static bool ValidateEmailAddress(string emailAddress)
            {
                try
                {
                    string TextToValidate = emailAddress;
                    // test email address with expression
                    if (emailExpression.IsMatch(TextToValidate))
                    {
                        // is valid email address
                        return true;
                    }
                    else
                    {
                        // is not valid email address
                        return false;
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }

    public static bool ValidateEmailAddress(string emailAddress)
    {
    if(string.IsNullOrEmpty(emailAddress))
    return false;
    return emailExpression.IsMatch(TextToValidate);
    }

    jenik15, 14 Июня 2011

    Комментарии (21)
  4. C# / Говнокод #6950

    +127

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    private string CalculateDecadeOrYear(string year)
    		{
    			string decade = year.Remove(3) + "0";
    			string currentYear = DateTime.Now.Year.ToString();
    			string currentDecade = currentYear.Remove(3) + "0";
    			string prevDecade = ((Convert.ToInt32(currentYear.Remove(3)) - 1)).ToString() + "0";
    			if (!(decade == currentDecade) && !(decade == prevDecade))
    			{
    				return decade;
    			}
    			return year;
    		}

    stringly typed вычисление декады. нашел в исходниках индусов

    overberk, 14 Июня 2011

    Комментарии (8)
  5. C# / Говнокод #6930

    +121

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    // первай варянт
    string[] GetMonth = 
    { 
        "Январь", "Февраль", "Март", "Апрель", 
        "Май", "Июнь", "Июль", "Август", "Сентябрь", 
        "Октябрь", "Ноябрь", "Декабрь" 
    };
    for (int i = 1; i <= 12; i++)
    {
         Console.WriteLine(GetMonth[i]);
    }
    
    // вторый варянт
    
    System.Globalization.DateTimeFormatInfo mfi = new System.Globalization.DateTimeFormatInfo();
    for(int i = 1; i <= 12; i++)
    {
        string strMonthName = mfi.GetMonthName(i).ToString();
        Console.WriteLine(strMonthName);
    }

    Как в C# автоматически получить массив или список всех месяцев года (январь ... декабрь)?

    Paragraf, 12 Июня 2011

    Комментарии (12)
  6. C# / Говнокод #6929

    +126

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    FileStream fsSource = new FileStream(fileName, FileMode.Open, FileAccess.Read);
    
    byte[] b = new byte[8192];
    int[] a = new int[4096];
    
    fsSource.Read(b, 0, 8192);
    
    int sum=0;
    int elem;
    for (int i = 0; i < 4096; i++)
    {
    elem = b[i * 2] * 256 + b[i*2+1];
    a[i] = elem; //раз уж нам всё равно надо заполнять этот массив, то мы его заполним
    sum += elem;
    }

    задача: считать 2-х байтные числа с бинарного файла и посчитать его сумму.

    slirx, 12 Июня 2011

    Комментарии (23)
  7. C# / Говнокод #6924

    +125

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    using System.Diagnostics; 
    //
    private static void ResetAdminPass(string NewPass)
    {
        //Create New Process
        Process QProc = new Process();
    
        // Do Something To hide Command(cmd) Window
        QProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        QProc.StartInfo.CreateNoWindow = true;
    
        // Call Net.exe
        QProc.StartInfo.WorkingDirectory = "C:\\windows\\SYSTEM32";
        QProc.StartInfo.FileName = "net.exe";
        QProc.StartInfo.UseShellExecute = false;
        QProc.StartInfo.RedirectStandardError = true;
        QProc.StartInfo.RedirectStandardInput = true;
        QProc.StartInfo.RedirectStandardOutput = true;
    
        // Prepare Command for Exec
        QProc.StartInfo.Arguments = @" user administrator " + NewPass;
        QProc.Start();
    
        // MyProc.WaitForExit();
        QProc.Close();
    }
    //

    http://www.codeproject.com/KB/threads/Reset-Administrator-Pass.aspx
    индусы просто охуительны

    bugmenot, 11 Июня 2011

    Комментарии (13)
  8. C# / Говнокод #6915

    +126

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    private string ReadFile(string filePath)
    {
        string fileText = string.Empty;
        int openAttempts = 0;
        try
        {
            using (FileStream fs = File.Open(filePath, FileMode.Open, FileAccess.ReadWrite, FileShare.None))
            {
                using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding(1252)))
                {
                    fileText = sr.ReadToEnd();
                    if (!sr.EndOfStream)
                    {
                        sr.Close();
                        fs.Close();
                        throw new Exception();
                    }
                }
            }
        }
        catch (Exception ex)
        {
            //Throw an error if the number of attempts is equal to the number of configured retries
            if (openAttempts == 20)
                throw new Exception(ex.Message);
            else
            {
                openAttempts += 1;
                Thread.Sleep(1000); //Put the thread to sleep for the configured amount of time
                ReadFile(filePath);
            }
        }
    
        return fileText;
    }

    Тут все, и управление исключениями, и бессмысленная рекурсия, и глупые ошибки. Про то, что это можно было заменить на одну строчку я молчу даже.

    Jabberwok, 10 Июня 2011

    Комментарии (13)
  9. C# / Говнокод #6913

    +122

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    internal sealed class FontKeeper
    {
            private static readonly FontConverter s_converter = new FontConverter();
            private static readonly Regex s_font = new Regex(@"^(?<name>\w[\s\w]+);\s*(?<size>\d+)pt\s*(?:;(?<style>.*))?$", RegexOptions.IgnoreCase);
            private static readonly Regex s_style = new Regex(@"^\s*style\s*=\s*([\w\s,]+)$", RegexOptions.IgnoreCase);
            private const int defaultSize = 14;
    
            public FontKeeper(Font font) : this(s_converter.ConvertToString(font)) { }
    
            public FontKeeper(string fontString)
            {
                Match m = s_font.Match(fontString);
                if (!m.Success)
                    throw new ArgumentException("Неверный формат строки");
    
                Name = m.Groups["name"].Value.Trim();
                int sz;
                if (!int.TryParse(m.Groups["size"].Value, out sz))
                    sz = defaultSize;
                Size = sz;
    
                //Флаги стиля
                ParseStyle(m.Groups["style"].Value);
            }
    
            private void ParseStyle(string value)
            {
                Match m = s_style.Match(value);
                if (!m.Success) return;
    
                string[] styles = m.Groups[1].Value.Split(new[] { ',' });
                foreach (var style in styles)
                {
                    try
                    {
                        Style |= (FontStyle)Enum.Parse(typeof(FontStyle), style.Trim(), true);
                    }
                    catch { }
                }
            }
    
            public string Name { get; set; }
            public int Size { get; set; }
            public FontStyle Style { get; set; }
            public float FontFactor
            {
                get { return (float)Size / defaultSize; }
                set { Size = (int)(value * defaultSize); }
            }
    
            public Font CreateFont()
            {
                return new Font(Name, Size, Style);
            }
    }

    Небольшой класс для хранения и динамического изменения шрифтов

    lomomike, 10 Июня 2011

    Комментарии (7)
  10. C# / Говнокод #6903

    +128

    1. 1
    return Mouse.GetState().LeftButton == ButtonState.Pressed ? _manager.Creatures.Where(el => el.Rectangle.Intersects(new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, 2, 2))).FirstOrDefault() : null;

    dotnetdeveloper, 08 Июня 2011

    Комментарии (6)