please help me. I would like to create new folder and save a screenshots from selenium therein.
I want, when I click the button xxx_1, folder will automatically be created with text which I enter in txt_Box1 and currently date.
Folder should be looks like that:
Test_test2_18_test3-test4_test5_test_11-Jul-2017
Here's my code
private void xxx_1(object sender, EventArgs e)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
+ "C:/xxx/xxx" + "_" + textBox1 + "_" + "xxx_xxx_xx_" + DateTime.Now;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
//string path = #"C:\\xxx\xxx" + "_" + textBox1 + "_" + "xxx_xxx_xxx_" + DateTime.Now;
String xxx= "https://xxx.xxx.xxx";
IWebDriver driver_xx = new ChromeDriver();
driver_xx.Navigate().GoToUrl(xxx);
driver_xx.FindElement(By.Id("xxx")).SendKeys("xxx");
driver_xx.FindElement(By.Id("xx")).SendKeys("xxx");
driver_xx.FindElement(By.Id("xx")).Click();
Thread.Sleep(3000);
Screenshot ss_xx = ((ITakesScreenshot)driver_xx).GetScreenshot();
ss_xx.SaveAsFile("How to save the screenshots in new created folder??", OpenQA.Selenium.ScreenshotImageFormat.Jpeg);
}
You can't use a DateTime in your path like that as the default implementation of .ToString() on a DateTime will contain invalid characters. Use a format specifier:
string path = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
"xx\\xx",
textBox1.Text,
"xx_xxx_xxx_",
DateTime.Now.ToString("dd-MM-yyyy HH-mm-ss") // This will show '21-09-2017 16-11-15'
);
Directory.CreateDirectory(path);
Be careful that if textBox1.Text contains invalid path characters such as < > : then you'll get another exception.
Related
looked all over and found plenty of stuff regarding this but none are using variables to form paths. What I need to do is move a folder, sub-folders and files to a new path on button click. So far none of what I found worked. At the moment, i'm getting no files or folders moved what so ever. The current solution I tried is from MSDN and tried to adapt it to my code. If you could correct the code and show me an example it would be great. I don't know what i'm doing wrong here. Here is the code:
private void CopyPartsToProject()
{
string sourcePath = (pathToQuotes + "/" + client_name.Text + "/" + quote_id.Text);
string targetPath = (pathToClient + "/" + client_name.Text + "/" + project_number.Text);
string sourceFile = sourcePath + "/" + "*.*";
string destinationFile = targetPath + "/" + "*.*";
System.IO.File.Move(sourceFile, destinationFile);
System.IO.Directory.Move(sourcePath, targetPath);
}
pathToQuotes and pathToClient are retreived from a MySQL database (from user input) in another method. The info is getting retreived without any problems and the paths are correct. If you could give me a hand it would be appreciated. Thanks.
You need a recursive method to achieve moving directories including all files and sub-directories:
private void moveDirectory(string sourcePath ,string targetPath)
{
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
String[] files = Directory.GetFiles(sourcePath);
String[] directories = Directory.GetDirectories(sourcePath);
foreach (string f in files)
{
System.IO.File.Copy(f, Path.Combine(targetPath,Path.GetFileName(f)), true);
}
foreach(string d in directories)
{
// recursive call
moveDirectory(Path.Combine(sourcePath, Path.GetFileName(d)), Path.Combine(targetPath, Path.GetFileName(d)));
}
}
then the usage is like this:
private void CopyPartsToProject()
{
string sourcePath = (pathToQuotes + "/" + client_name.Text + "/" + quote_id.Text);
string targetPath = (pathToClient + "/" + client_name.Text + "/" + project_number.Text);
moveDirectory(sourcePath, targetPath);
}
Ok, so I'm new to C# and don't know what I'm doing wrong.
At the top of the button click event I have this variable:
InformationDump infodump;
infodump = new InformationDump();
After that I have the rest of the code which is meant to path to the user desktop and save whatever is filled out in the textbox's into a separate window (part of the same program):
infodump.richTextBox1.Text = textBox1.Text + ", " + textBox2.Text + ", " + textBox3.Text + ", " + textBox4.Text + ", " + comboBox1.SelectedItem;
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string filename = path + "\\" + DateTime.Now.ToString("HH.mm.ss") + System.Environment.UserName + ".txt";
infodump.richTextBox1.SaveFile(filename, RichTextBoxStreamType.RichText);
Thanks in advance.
This is happening because you are creating an instance of the form (InformationDump) then try to use the RichTextBox instance but the form then will not go through the standard loading and initialization process therefore as I reproduced, the file will be created but will be empty.
It is very interesting, but if you do repeated save it works!
infodump.richTextBox1.SaveFile(filename, RichTextBoxStreamType.RichText);
// zero bytes
infodump.richTextBox1.SaveFile(filename, RichTextBoxStreamType.RichText);
// works!
I have a question that's driving me nuts. I have a program that saves error messages to a string in an object, then writes the string to a file in the unloadContent() thing. For some reason I keep getting Not Supported Exceptions. Here is the code in unloadContent():
if (debug.getContent().Length > 0)
{
saveErrors save = new saveErrors();
if (Directory.Exists(System.IO.Directory.GetCurrentDirectory() + "\\Errors")) ;
Directory.CreateDirectory(System.IO.Directory.GetCurrentDirectory() + "\\Errors");
save.save(System.IO.Directory.GetCurrentDirectory().ToString() + "\\Errors\\errorLog_" + (System.DateTime.Now.ToString().Replace("/", "_")).Replace(" ","") + ".txt");
}
and here's the code in class save errors:
public class saveErrors
{
private string mess = debug.getContent();
public void save(string fileName)
{
Debug.WriteLine(fileName);
using (StreamWriter sw = new StreamWriter(fileName))
{
sw.Write(mess);
sw.Close();
}
}
}
I'm still a bit new to C#, so any help would be greatly appreciated!
Thanks!
Try this:
[Test]
public void SaveTextTest()
{
string relativePath=#"Errors\errorLog_";
string directoryPath = System.IO.Path.Combine( System.IO.Directory.GetCurrentDirectory() , relativePath);
var directoryInfo = new DirectoryInfo(directoryPath);
if(directoryInfo.Exists==false)
directoryInfo.Create();
string fileName = System.DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss") + ".txt";
string path = System.IO.Path.Combine(directoryPath, fileName);
string textToSave = "This will be saved";
File.WriteAllText(path, textToSave);
}
To get the DateTime.ToString() in the desired format you can pass a formatstring
save.save(System.IO.Directory.GetCurrentDirectory().ToString() + "\\Errors\\errorLog_" + (System.DateTime.Now.ToString().Replace("/", "_")).Replace(" ", "").Replace(":", "") + ".txt");
Change it to that. You need a .Replace(":", "") because : Is included in the date part of the code, but is invalid in a file name, so you must either remove it or replace it with something else.
As an alternative you could format the date as so:
save.save(System.IO.Directory.GetCurrentDirectory().ToString() + "\\Errors\\errorLog_" + System.DateTime.Now.ToString("yyyy-MM-dd_hh-mm-ss"));
I'm trying to save files
string path= "~/Pre/IntraExtra/" + Session["id"].ToString() + "_" + FileUpload1.FileName;
FileUpload11.SaveAs(Server.MapPath(path));
but it gives this error "The given path's format is not supported."
It is working now ..
I just removed the (~/) , Thank you all
for example if I had code that was set like the following on my end it works.. also notice the # symbol I am using .. this is for a literal file path this way I don't have to use "\ in the file path.. try the following code as see if it works.. replace with your code variables.
if (FileUpload1.HasFile)
{
fname = FileUpload1.FileName;
spath = "~\Pre\IntraExtra\" + FileUpload1.FileName;
fpath = Server.MapPath("Uploaded");
fpath = fpath + #"\" + FileUpload1.FileName;
desc = TextBox2.Text;
if (System.IO.File.Exists(fpath))
{
Label1.Text = "File Name already exists!";
return;
}
else
{
FileUpload1.SaveAs(fpath);
}
}
Maybe try to use the Path.Combine method:
string path= "~/Pre/IntraExtra/" + Session["id"].ToString() + "_"; ;
string combinedPath = System.IO.Path.Combine(path, FileUpload1.FileName);
FileUpload11.SaveAs(Server.MapPath(combinedPath));
If this does not work, then could you give us the filename and path?
It is working now .. I just removed the (~/) , Thank you all
After deployment first time image display and same page next time change the imageurl old image only is there new image not display. this is code i want change anything in code....
protected void btn_Upload_Click(object sender, EventArgs e)
{
Image1.Dispose();
if (FileUpload1.HasFile)
{
if (!string.IsNullOrEmpty(txtDesignNo.Text))
{
Image1.Visible = true;
Image1.Dispose();
Image1.ImageUrl = string.Empty;
filename = System.Web.HttpContext.Current.Server.MapPath("") + "\\Images" + "\\" + txtDesignNo.Text + ".jpg";
// filename =CGlobals.imagePath + txtDesignNo.Text + ".jpg";
// string filename = System.Web.HttpContext.Current.Server.MapPath("") + "\\ImageStorage" + "\\" + 1 + ".jpg";
FileUpload1.SaveAs(filename);
Image1.ImageUrl = "~/Masters/Images/" + txtDesignNo.Text + ".jpg";
// Image1.ImageUrl =CGlobals.imagePath + txtDesignNo.Text + ".jpg";
//Image1.ImageUrl = filename;
}
}
}
Try shift+f5 to force a full refresh. Depending on your browser, this might force the fetching of the new image instead of using the cached version.
Looks like you save the image under \Images\ but then you try to show it from \Masters\Images.
Try changing the upload path to match the imageurl or vice versa.
try to check the image path using firebug. You saved image to image folder, but you are accessing from master\images.