File tree Expand file tree Collapse file tree 1 file changed +4
-1
lines changed Expand file tree Collapse file tree 1 file changed +4
-1
lines changed Original file line number Diff line number Diff line change @@ -88,7 +88,7 @@ static String GetScreenshotName()
88
88
Directory . CreateDirectory ( path ) ;
89
89
path = Path . Combine ( path , DateTime . Now . ToString ( "yyyyMMdd_HH-mm-ss" ) ) ;
90
90
int num = 0 ;
91
- for ( ; File . Exists ( Path . Combine ( path , num == 0 ? "" : ( "_" + num ) ) + ".png" ) ; num ++ ) ;
91
+ for ( ; File . Exists ( path + ( num == 0 ? "" : ( "_" + num ) ) + ".png" ) ; num ++ ) ;
92
92
return path + ( num == 0 ? "" : ( "_" + num ) ) + ".png" ;
93
93
}
94
94
@@ -105,6 +105,9 @@ static String PrintScreen()
105
105
Bitmap bmp = new Bitmap ( bounds . Width , bounds . Height ) ;
106
106
using ( Graphics g = Graphics . FromImage ( bmp ) )
107
107
g . CopyFromScreen ( bounds . Location , Point . Empty , bounds . Size ) ;
108
+ // Since the program is single-threaded, race conditions won't happen
109
+ // For multi-threaded program, use the following with try-catch can guarantee that no file is overwritten:
110
+ // File.Open(path, FileMode.CreateNew)
108
111
String name = GetScreenshotName ( ) ;
109
112
bmp . Save ( name , ImageFormat . Png ) ;
110
113
return name ;
You can’t perform that action at this time.
0 commit comments