public void SetCurrentDirectoryToNonExistant()
 {
     using (var cleaner = new TestFileCleaner())
     {
         string directoryPath = cleaner.GetTestPath();
         Action action        = () => DirectoryMethods.SetCurrentDirectory(directoryPath);
         action.ShouldThrow <System.IO.FileNotFoundException>();
     }
 }
        public void SetCurrentDirectoryBasic()
        {
            string currentDirectory = DirectoryMethods.GetCurrentDirectory();

            StoreHelper.ValidateStoreGetsUnauthorizedAccess(() =>
            {
                DirectoryMethods.SetCurrentDirectory(@"C:\");
                DirectoryMethods.GetCurrentDirectory().Should().Be(@"C:\");
            });
            DirectoryMethods.SetCurrentDirectory(currentDirectory);
            DirectoryMethods.GetCurrentDirectory().Should().Be(currentDirectory);
        }
예제 #3
0
        unsafe static LRESULT WindowProcedure(WindowHandle window, WindowMessage message, WPARAM wParam, LPARAM lParam)
        {
            const string filter = "*.*";

            switch (message)
            {
            case WindowMessage.Create:
                SIZE baseUnits = Windows.GetDialogBaseUnits();
                rect.left = 20 * baseUnits.cx;
                rect.top  = 3 * baseUnits.cy;

                // Create listbox and static text windows.
                hwndList = Windows.CreateWindow("listbox", null,
                                                WindowStyles.Child | WindowStyles.Visible | (WindowStyles)ListBoxStyles.Standard, ExtendedWindowStyles.None,
                                                baseUnits.cx, baseUnits.cy * 3, baseUnits.cx * 13 + Windows.GetSystemMetrics(SystemMetric.CXVSCROLL), baseUnits.cy * 10,
                                                window, (IntPtr)ID_LIST, ((CREATESTRUCT *)lParam)->hInstance, IntPtr.Zero);

                hwndText = Windows.CreateWindow("static", DirectoryMethods.GetCurrentDirectory(),
                                                WindowStyles.Child | WindowStyles.Visible | (WindowStyles)StaticStyles.Left, ExtendedWindowStyles.None,
                                                baseUnits.cx, baseUnits.cy, baseUnits.cx * 260, baseUnits.cy,
                                                window, (IntPtr)ID_TEXT, ((CREATESTRUCT *)lParam)->hInstance, IntPtr.Zero);

                OldList = hwndList.SetWindowProcedure(s_ListBoxProcedure);

                fixed(char *f = filter)
                hwndList.SendMessage(ListBoxMessage.Directory, (uint)DIRATTR, f);

                return(0);

            case WindowMessage.Size:
                rect.right  = lParam.LowWord;
                rect.bottom = lParam.HighWord;
                return(0);

            case WindowMessage.SetFocus:
                hwndList.SetFocus();
                return(0);

            case WindowMessage.Command:
                if (wParam.LowWord == ID_LIST &&
                    (wParam.HighWord == (ushort)ListBoxNotification.DoubleClick))
                {
                    uint i = hwndList.SendMessage(ListBoxMessage.GetCurrentSelection, 0, 0);
                    if (i == WindowDefines.LB_ERR)
                    {
                        break;
                    }

                    int   iLength    = hwndList.SendMessage(ListBoxMessage.GetTextLength, i, 0) + 1;
                    char *textBuffer = stackalloc char[iLength];
                    int   result     = hwndList.SendMessage(ListBoxMessage.GetText, i, textBuffer);
                    szFile = new string(textBuffer, 0, result);

                    SafeFileHandle hFile = null;
                    try
                    {
                        using (hFile = FileMethods.CreateFile(szFile, CreationDisposition.OpenExisting,
                                                              DesiredAccess.GenericRead, ShareModes.Read))
                        {
                            if (!hFile.IsInvalid)
                            {
                                bValidFile = true;
                                hwndText.SetWindowText(DirectoryMethods.GetCurrentDirectory());
                            }
                        }
                        hFile = null;
                    }
                    catch
                    {
                    }

                    if (hFile == null && szFile[0] == ('['))
                    {
                        bValidFile = false;

                        // If setting the directory doesn’t work, maybe it’s a drive change, so try that.
                        try { DirectoryMethods.SetCurrentDirectory(szFile.Substring(1, szFile.Length - 2)); }
                        catch
                        {
                            try { DirectoryMethods.SetCurrentDirectory($"{szFile[2]}:"); }
                            catch { }
                        }

                        // Get the new directory name and fill the list box.
                        hwndText.SetWindowText(DirectoryMethods.GetCurrentDirectory());
                        hwndList.SendMessage(ListBoxMessage.ResetContent, 0, 0);

                        fixed(char *f = filter)
                        hwndList.SendMessage(ListBoxMessage.Directory, (uint)DIRATTR, f);
                    }

                    window.Invalidate();
                }
                return(0);

            case WindowMessage.Paint:
                if (!bValidFile)
                {
                    break;
                }

                uint bytesRead;
                using (var hFile = FileMethods.CreateFile(szFile, CreationDisposition.OpenExisting,
                                                          DesiredAccess.GenericRead, ShareModes.Read))
                {
                    if (hFile.IsInvalid)
                    {
                        bValidFile = false;
                        break;
                    }

                    bytesRead = FileMethods.ReadFile(hFile, buffer, MAXREAD);
                }

                using (DeviceContext dc = window.BeginPaint())
                {
                    dc.SelectObject(StockFont.SystemFixed);
                    dc.SetTextColor(Windows.GetSystemColor(SystemColor.ButtonText));
                    dc.SetBackgroundColor(Windows.GetSystemColor(SystemColor.ButtonFace));
                    dc.DrawText(Encoding.UTF8.GetString(buffer), rect, DTFLAGS);
                }

                return(0);

            case WindowMessage.Destroy:
                Windows.PostQuitMessage(0);
                return(0);
            }

            return(Windows.DefaultWindowProcedure(window, message, wParam, lParam));
        }