0

I am making this program but I'm not sure if it will work on windows xp. I've done it using net framework 3.5 so its compatible but it would help alot more if someone with windows xp could test it.

This program so far takes a screenshot every 30 seconds (timer tick) and saves it to a hidden folder which is why I need help as the document paths are different on XP. Its sort of a screenshot logger, gotta say I'm not intending to use on anyone, I just enjoy programming different things.

One last thing before anyone points it out, I know the "Current folder" variable seems redundant but i'm planning on making a new folder every 20 screenshots or so.

public partial class Form1 : Form
{
    public static Int32 ScreenshotNumber = 1;
    public static Int32 CurrentFolder = 1;

    public Form1()
    {
        InitializeComponent();
    }

    private void ImageTimer_Tick(object sender, EventArgs e)
    { //checking if primary folder exists, if not, make it
        if (!Directory.Exists(@"C:\Users\" + Environment.UserName + @"\AppData\Roaming\SystemEX"))
        {
            try
            {
                Directory.CreateDirectory(@"C:\Users\" + Environment.UserName + @"\AppData\Roaming\" + "SystemEX");
                Directory.CreateDirectory(@"C:\Users\" + Environment.UserName + @"\AppData\Roaming\SystemEX\" + "01I28SJ3");
                try { File.SetAttributes(@"C:\Users\" + Environment.UserName + @"\AppData\Roaming\SystemEX\" + "01I28SJ3", FileAttributes.Hidden); }
                catch { }
                Directory.CreateDirectory(@"C:\Users\" + Environment.UserName + @"\AppData\Roaming\SystemEX\01I28SJ3\" + "1");
            }
            catch { }

        } //checking if secondary folder exists, if not, make it
        else if (!Directory.Exists(@"C:\Users\" + Environment.UserName + @"\AppData\Roaming\SystemEX\01I28SJ3"))
        {
            try
            {
                Directory.CreateDirectory(@"C:\Users\" + Environment.UserName + @"\AppData\Roaming\SystemEX\" + "01I28SJ3");
                try { File.SetAttributes(@"C:\Users\" + Environment.UserName + @"\AppData\Roaming\SystemEX\" + "01I28SJ3", FileAttributes.Hidden); }
                catch { }
                Directory.CreateDirectory(@"C:\Users\" + Environment.UserName + @"\AppData\Roaming\SystemEX\01I28SJ3\" + "1");
            }
            catch { }
        }

        try
        { //take a screenshot

            Bitmap Printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            Graphics graphics = Graphics.FromImage(Printscreen as Image);
            graphics.CopyFromScreen(0, 0, 0, 0, Printscreen.Size);
            //save it
            if (Directory.Exists(@"C:\Users\" + Environment.UserName + @"\AppData\Roaming\SystemEX\01I28SJ3\"))
            {
                Printscreen.Save(@"C:\Users\" + Environment.UserName + @"\AppData\Roaming\SystemEX\01I28SJ3\" + CurrentFolder + @"\" + "Screenshot" + Convert.ToString(ScreenshotNumber) + ".jpg", ImageFormat.Jpeg);
                ScreenshotNumber += 1;
            }
        }
        catch { }
    }
}
Gabriel GM
  • 6,391
  • 2
  • 31
  • 34
Will
  • 23
  • 5
  • Side note: `Directory.CreateDirectory` is able to create directories recursively. Let's say you want to create a directory X:\1\2\3\4, but only X:\1\2 exists. Calling `Directory.CreateDirectory(@"X:\1\2\3\4")` will create the sub directories 3 and 4. No need to call Directory.CreateDirectory for each sub directory separately... –  Aug 14 '15 at 23:45
  • thanks ill try that out – Will Aug 14 '15 at 23:48

1 Answers1

3

No. Windows XP doesn't use C:\Users, and there's no guarantee that future versions of Windows will either. You should use Environment.SpecialFolder to find the user's profile directory, rather than trying to guess the location yourself.

Polynomial
  • 27,674
  • 12
  • 80
  • 107
  • so would this work instead? Directory.CreateDirectory(Environment.SpecialFolder.ApplicationData + @"\Roaming\ ... and then the folder name i want to create? – Will Aug 14 '15 at 23:46
  • @Will, please check the MSDN documentation for the Environment.SpecialFolder enumeration. Environment.SpecialFolder.ApplicationData is the **roaming** ApplicationData folder. (Environment.SpecialFolder.LocalApplicationData would be the *local* ApplicationData folder) –  Aug 14 '15 at 23:48
  • @Will No. The `SpecialFolder.ApplicationData` will give you the path of the roaming application data directory anyway, so appending `Roaming` to the end won't work. You should also be using `Path.Combine` rather than string concatenation to get to subdirectories. As elgonzo says, if you want the local profile (i.e. profile on this machine only, not a profile that'll move with the user on a domain) then you should use `SpecialFolder.LocalApplicationData`. – Polynomial Aug 14 '15 at 23:50
  • ah i see, ill look into that more, thanks – Will Aug 14 '15 at 23:50