예제 #1
0
        protected override void Execute(CodeActivityContext context)
        {
            var isSaveAs         = IsSaveAs.Get(context);
            var initialDirectory = InitialDirectory.Get(context);
            var title            = Title.Get(context);
            var defaultExt       = DefaultExt.Get(context);
            var filter           = Filter.Get(context);
            var filterIndex      = FilterIndex.Get(context);
            var checkFileExists  = CheckFileExists.Get(context);
            var checkPathExists  = CheckPathExists.Get(context);
            var multiselect      = Multiselect.Get(context);

            System.Windows.Forms.FileDialog dialog;
            if (isSaveAs)
            {
                dialog = new System.Windows.Forms.SaveFileDialog();
            }
            else
            {
                dialog = new System.Windows.Forms.OpenFileDialog();
                ((System.Windows.Forms.OpenFileDialog)dialog).Multiselect = multiselect;
            }
            if (!string.IsNullOrEmpty(title))
            {
                dialog.Title = title;
            }
            if (!string.IsNullOrEmpty(defaultExt))
            {
                dialog.DefaultExt = defaultExt;
            }
            if (!string.IsNullOrEmpty(filter))
            {
                dialog.Filter      = filter;
                dialog.FilterIndex = filterIndex;
            }
            dialog.CheckFileExists = checkFileExists;
            dialog.CheckPathExists = checkPathExists;


            System.Windows.Forms.DialogResult result = System.Windows.Forms.DialogResult.Cancel;
            GenericTools.RunUI(() =>
            {
                result = dialog.ShowDialog();
            });
            if (result != System.Windows.Forms.DialogResult.OK)
            {
                context.SetValue(FileName, null);
                context.SetValue(FileNames, null);
                return;
            }
            context.SetValue(FileName, dialog.FileName);
            context.SetValue(FileNames, dialog.FileNames);
        }
예제 #2
0
        /// <summary>Follows path selected by user</summary>
        /// <param name="fileName">Path to be followed</param>
        /// <param name="followLinks">True to follow links in *.lnk files</param>
        /// <returns>True when <paramref name="fileName"/> represents a file that can be returned from dialog; false otherwise (invalid path, directory navigation, filter change)</returns>
        private bool FollowPath(string fileName, bool followLinks)
        {
            //Empty path - do nothing
            if (string.IsNullOrEmpty(fileName))
            {
                return(false);
            }
            //Current directory
            else if (fileName == ".")
            {
                return(false);
            }
            //Up one level
            else if (fileName == "..")
            {
                if (currentDirectory == "\\")
                {
                    return(false);
                }
                var parts = currentDirectory.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar).TrimEnd(Path.DirectorySeparatorChar).Split(Path.DirectorySeparatorChar);
                try
                {
                    string newPath = string.Join(Path.DirectorySeparatorChar.ToString(), parts, 0, parts.Length - 1);
                    LoadFolder(string.IsNullOrEmpty(newPath) ? "\\" : newPath, SelectedFilter);
                }
                catch
                {
                    MessageBox.Show(Properties.Resources.err_NavigatingToParentFolder, Properties.Resources.FileDialogTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                }
                return(false);
            }
            //Path contains relative sgments - normalize it and combien with current
            else if (fileName.StartsWith("." + Path.DirectorySeparatorChar) ||
                     fileName.EndsWith(Path.DirectorySeparatorChar + ".") ||
                     fileName.StartsWith(".." + Path.DirectorySeparatorChar) ||
                     fileName.EndsWith(Path.DirectorySeparatorChar + "..") ||
                     fileName.IndexOf(Path.DirectorySeparatorChar + ".." + Path.DirectorySeparatorChar) >= 0 ||
                     fileName.IndexOf(Path.DirectorySeparatorChar + "." + Path.DirectorySeparatorChar) >= 0 ||

                     fileName.StartsWith("." + Path.AltDirectorySeparatorChar) ||
                     fileName.EndsWith(Path.AltDirectorySeparatorChar + ".") ||
                     fileName.StartsWith(".." + Path.AltDirectorySeparatorChar) ||
                     fileName.EndsWith(Path.AltDirectorySeparatorChar + "..") ||
                     fileName.IndexOf(Path.AltDirectorySeparatorChar + ".." + Path.AltDirectorySeparatorChar) >= 0 ||
                     fileName.IndexOf(Path.AltDirectorySeparatorChar + "." + Path.AltDirectorySeparatorChar) >= 0 ||

                     fileName.IndexOf(Path.DirectorySeparatorChar + ".." + Path.AltDirectorySeparatorChar) >= 0 ||
                     fileName.IndexOf(Path.DirectorySeparatorChar + "." + Path.AltDirectorySeparatorChar) >= 0 ||
                     fileName.IndexOf(Path.AltDirectorySeparatorChar + ".." + Path.DirectorySeparatorChar) >= 0 ||
                     fileName.IndexOf(Path.AltDirectorySeparatorChar + "." + Path.DirectorySeparatorChar) >= 0)
            {
                var           newPath = fileName.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar).TrimEnd(Path.DirectorySeparatorChar);
                List <string> comb    = new List <string>(currentDirectory.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar).TrimEnd(Path.DirectorySeparatorChar).Split(Path.DirectorySeparatorChar));
                foreach (var seg in newPath.Split(Path.DirectorySeparatorChar))
                {
                    if (seg == ".")
                    {
                        continue;
                    }
                    else if (seg == ".." && comb.Count > 0)
                    {
                        comb.RemoveAt(comb.Count - 1);
                    }
                    else
                    {
                        comb.Add(seg);
                    }
                }
                return(FollowPath(string.Join(Path.DirectorySeparatorChar.ToString(), comb.ToArray()), followLinks));
            }
            //Wildcards + directories (not supported)
            else if ((fileName.IndexOf('*') >= 0 || fileName.IndexOf('?') >= 0) && (fileName.IndexOf(Path.DirectorySeparatorChar) >= 0 || fileName.IndexOf(Path.AltDirectorySeparatorChar) >= 0))
            {
                MessageBox.Show(Properties.Resources.err_CombineWildcardsAndFolders, Properties.Resources.FileDialogTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                return(false);
            }
            //Wildcards - set filter
            else if (fileName.IndexOf('*') >= 0 || fileName.IndexOf('?') >= 0)
            {
                try
                {
                    LoadFolder(currentDirectory, fileName);
                }
                catch
                {
                    MessageBox.Show(string.Format(Properties.Resources.err_ApplyingFiler, fileName), Properties.Resources.FileDialogTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                }
                return(false);
            }
            //Relative or absolute path
            else if (fileName.IndexOf(Path.DirectorySeparatorChar) >= 0 || fileName.IndexOf(Path.AltDirectorySeparatorChar) >= 0)
            {
                try
                {
                    if (!Path.IsPathRooted(fileName))
                    {
                        fileName = Path.Combine(currentDirectory, fileName);
                    }
                    if (File.Exists(fileName))
                    {
                        return(AcceptFilename(fileName, followLinks));
                    }
                    else if (AddExtension && !string.IsNullOrEmpty(DefaultExt) &&
                             Path.GetExtension(fileName) == "" && File.Exists(fileName + (DefaultExt.StartsWith(".") ? "" : ".") + DefaultExt))
                    {
                        fileName = fileName + (DefaultExt.StartsWith(".") ? "" : ".") + DefaultExt;
                        return(AcceptFilename(fileName, followLinks));
                    }
                    LoadFolder(fileName, SelectedFilter);
                }
                catch
                {
                    MessageBox.Show(string.Format(Properties.Resources.err_LoadDirectory, fileName), Properties.Resources.FileDialogTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                }
                return(false);
            }
            //File or directory name
            else
            {
                try
                {
                    if (File.Exists(Path.Combine(currentDirectory, fileName)))
                    {
                        fileName = Path.Combine(currentDirectory, fileName);
                        return(AcceptFilename(fileName, followLinks));
                    }
                    else if (Directory.Exists(Path.Combine(currentDirectory, fileName)))
                    {
                        fileName = Path.Combine(currentDirectory, fileName);
                        LoadFolder(fileName, SelectedFilter);
                    }
                    else if (AddExtension && !string.IsNullOrEmpty(DefaultExt) &&
                             File.Exists(fileName = Path.Combine(currentDirectory, fileName + (DefaultExt.StartsWith(".") ? "" : ".") + DefaultExt)))
                    {
                        return(AcceptFilename(fileName, followLinks));
                    }
                    else
                    {
                        MessageBox.Show(string.Format(Properties.Resources.err_FileNotFound, fileName), Properties.Resources.FileDialogTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    }
                }
                catch
                {
                    MessageBox.Show(string.Format(Properties.Resources.err_InvalidFileName, fileName), Properties.Resources.FileDialogTitle, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                }
                return(false);
            }
        }
예제 #3
0
파일: Program.cs 프로젝트: shuaiagain/MyApi
        static void Main(string[] args)
        {
            ReverseWord.AA();
            //var sum = TwoSum.Sum(new int[] { 1, 2, 7,8, 5 }, 7);
            //Console.WriteLine("result={0},{1}",sum[0],sum[1]);

            //AutoMapperExt.Print();

            #region async/await
            //AsyncAwaitC.Print();

            //AsyncAwaitB.Print();

            //AsyncAwaitA.Print();
            #endregion

            #region stream
            //StreamExt5.Print();
            //StreamExt4.Print();
            //StreamExt3.Print();
            //StreamExt2.Print();
            //StreamExt.Print();
            //ReflectionPractice.Print();
            #endregion

            #region 2
            //Dictionary_Condictionary.Print();
            //HashTable_Dictionary_ConcurrentDictionary.Print();
            //ThreadExtE.Print();
            //ThreadExtC.Print();
            //ThreadExtB.Print();
            //StringEmptyDBNull.Print();
            //ThreadExtA.Print();
            #endregion

            #region before
            //Console.WriteLine("main thread start");
            //OneDto.AsynchronyWithTPL();
            //Console.WriteLine("main thread end ");
            //t.Wait();

            //t = OneDto.AsynchronyWithAwait();
            //t.Wait();

            //TwoDto.DelayExample();
            //TwoDto.DelayExampleTwo();
            //TwoDto.ThreadSleepEg();
            //TwoDto.MaxThreadpoolEg();

            //task的基本操作
            //OneDto.TaskBasicOperate();
            //TaskBasicOperate.TaskBasic();
            //OneDto.TaskBasicOperateTwo();
            //OneDto.TaskBasicOperateThree();
            //OneDto.ThreadPoolEg();
            //TaskBasicOperate.InsertSort();
            //ThreeTaskOperate.ContinueWithEg();

            //new ValueRefType().Test();

            //Console.WriteLine("{0} ", Marshal.SizeOf(new TypeSize()));

            //DelegateT dele = new DelegateT();

            //ConstReadOnly read = new ConstReadOnly();
            //read.ConstReadOnlyTest();

            //ClassInitOrder classOrder = new ClassInitOrder();

            //ClassB classB = new ClassB();
            //classB.print();

            //AutoProperty autoP = new AutoProperty();
            //autoP.printHandField();

            //RefOut ro = new RefOut();
            //ro.TestValueRef();
            //ro.TestObjRef();
            //ro.TestValueOut();

            //As_Is asIs = new As_Is();
            //asIs.ShowAs();

            //ConstReadOnly ro = new ConstReadOnly();
            //ro.Print();
            //ConstReadOnly roOne = new ConstReadOnly();
            //roOne.Print();

            //StringEmpty se = new StringEmpty();
            //se.Print();

            //StringBuilderString sb = new StringBuilderString();
            //sb.Print();

            //StringEmpty se = new StringEmpty();
            //se.TypeSize();

            //AnonymousDelegate an = new AnonymousDelegate();
            //an.Print();

            //EqualsGetHashCode.Print();
            //PointerType.Print();
            DefaultExt.Print();
            //IEnumeratorExt.Print();
            //ExpressionTree.Print();
            #endregion
        }
예제 #4
0
        protected override void Execute(CodeActivityContext context)
        {
            var isSaveAs         = IsSaveAs.Get(context);
            var initialDirectory = InitialDirectory.Get(context);
            var title            = Title.Get(context);
            var defaultExt       = DefaultExt.Get(context);
            var filter           = Filter.Get(context);
            var filterIndex      = FilterIndex.Get(context);
            var checkFileExists  = CheckFileExists.Get(context);
            var checkPathExists  = CheckPathExists.Get(context);
            var multiselect      = Multiselect.Get(context);

            System.Windows.Forms.FileDialog dialog;
            if (isSaveAs)
            {
                dialog = new System.Windows.Forms.SaveFileDialog();
            }
            else
            {
                dialog = new System.Windows.Forms.OpenFileDialog();
                ((System.Windows.Forms.OpenFileDialog)dialog).Multiselect = multiselect;
            }
            if (!string.IsNullOrEmpty(title))
            {
                dialog.Title = title;
            }
            if (!string.IsNullOrEmpty(defaultExt))
            {
                dialog.DefaultExt = defaultExt;
            }
            if (!string.IsNullOrEmpty(filter))
            {
                dialog.Filter      = filter;
                dialog.FilterIndex = filterIndex;
            }
            try
            {
                if (!string.IsNullOrEmpty(initialDirectory) && !initialDirectory.Contains("\\"))
                {
                    Enum.TryParse(initialDirectory, out Environment.SpecialFolder specialfolder);
                    initialDirectory = Environment.GetFolderPath(specialfolder);
                }
            }
            catch (Exception)
            {
                throw;
            }
            if (!string.IsNullOrEmpty(initialDirectory))
            {
                dialog.InitialDirectory = initialDirectory;
            }
            dialog.CheckFileExists = checkFileExists;
            dialog.CheckPathExists = checkPathExists;


            System.Windows.Forms.DialogResult result = System.Windows.Forms.DialogResult.Cancel;
            GenericTools.RunUI(() =>
            {
                result = dialog.ShowDialog();
            });
            if (result != System.Windows.Forms.DialogResult.OK)
            {
                context.SetValue(FileName, null);
                context.SetValue(FileNames, null);
                return;
            }
            context.SetValue(FileName, dialog.FileName);
            context.SetValue(FileNames, dialog.FileNames);
        }