I am creating a project to do with some simple tasks to help my work.
The application is being made as a windows form in C#.
Everything seems to be fine other than one issue, the issue is that when copying files from a shared location to a local location, all of them work other than one.
the code is as follows:
private void Form1_Load_1(object sender, EventArgs e)
{
string fileName = "Analyst's Name.txt";
string fileName1 = "Group Name.txt";
string fileName2 = "KB Number.txt";
string fileName3 = "Return Notes.txt";
string sourcePath = #"\\remote location";
string targetPath = #"C:\SUPPORT\";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string sourceFile1 = System.IO.Path.Combine(sourcePath, fileName1);
string sourceFile2 = System.IO.Path.Combine(sourcePath, fileName2);
string sourceFile3 = System.IO.Path.Combine(sourcePath, fileName3);
string destFile = System.IO.Path.Combine(targetPath, fileName);
string destFile1 = System.IO.Path.Combine(targetPath, fileName1);
string destFile2 = System.IO.Path.Combine(targetPath, fileName2);
string destFile3 = System.IO.Path.Combine(targetPath, fileName3);
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
System.IO.File.Copy(sourceFile, destFile, true);
System.IO.File.Copy(sourceFile1, destFile1, true);
System.IO.File.Copy(sourceFile2, destFile2, true);
System.IO.File.Copy(sourceFile3, destFile3, true);
if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);
foreach (string s in files)
{
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, fileName);
destFile1 = System.IO.Path.Combine(targetPath, fileName1);
destFile2 = System.IO.Path.Combine(targetPath, fileName2);
destFile3 = System.IO.Path.Combine(targetPath, fileName3);
System.IO.File.Copy(s, destFile, true);
System.IO.File.Copy(s, destFile1, true);
System.IO.File.Copy(s, destFile2, true);
System.IO.File.Copy(s, destFile3, true);
now the problem is that the return notes file that is locally on the C drive, is being copied but the data within it is showing as the analyst name data, the name of the file is correct, but the data inside is from a different file, any help would be appreciated.
It should be as simple as this:
List<string> files = new List<string>
{
"Analyst's Name.txt",
"Group Name.txt",
"KB Number.txt",
"Return Notes.txt"
};
string sourcePath = #"\\remote location";
string targetPath = #"C:\SUPPORT\";
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
for (int i = 0; i < files.Count; i++)
{
string filePath = Path.Combine(sourcePath, files[i]);
if (File.Exists(filePath))
{
File.Copy(filePath, Path.Combine(targetPath, files[i]), true);
}
}
Related
I am getting an error in line player.PlaySync().I am a creating .wav file. It is showing me an error
"The file located at
D:\Seo-app\SEO-Automation-Page-Generation.APP\SEO-Automation-Page-Generation.APP\Download\Autoplay.wav
is not a valid wave file."
var response = client.SynthesizeSpeech(input, voice, config);
int count = 1;
string virtualPath = HttpContext.Server.MapPath("/Download/");
if (!Directory.Exists(virtualPath))
Directory.CreateDirectory(virtualPath);
string fullPath = HttpContext.Server.MapPath("/Download/Autoplay.wav");
string fileNameOnly = Path.GetFileNameWithoutExtension(fullPath);
string extension = Path.GetExtension(fullPath);
string path = Path.GetDirectoryName(fullPath);
while (System.IO.File.Exists(fullPath))
{
string tempFileName = string.Format("{0}({1})", fileNameOnly, count++);
fullPath = Path.Combine(path, tempFileName + extension);
}
using (Stream output = System.IO.File.Create(fullPath))
{
response.AudioContent.WriteTo(output);
}
using (SoundPlayer player = new SoundPlayer())
{
player.SoundLocation = HttpContext.Server.MapPath("/Download/Autoplay.wav");
player.PlaySync();
}
I need a copy file on folder to another folder and i'm use this
string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
foreach (string file in files)
{
folderBrowserDialog1.ShowDialog();
string xx = folderBrowserDialog1.SelectedPath;
folderBrowserDialog1.ShowDialog();
string yy = folderBrowserDialog1.SelectedPath;
File.Copy(xx, yy);
But is not working.
Why?
Try this code.
I assume you want to read a file then write it to new one.
Hope it helps.
string sourceFile;
string newFile = "C:\NewFile\NewFile.txt";
string fileToRead = "C:\ReadFile\ReadFile.txt";
bool overwriteExistingFile = true; //change to false if you want no to overwrite the existing file.
bool isReadSuccess = getDataFromFile(fileToRead, ref sourceFile);
if (isReadSuccess)
{
File.Copy(sourceFile, newFile, overwriteExistingFile);
}
else
{
Console.WriteLine("An error occured :" + sourceFile);
}
//Reader Method you can use this or modify it depending on your needs.
public static bool getDataFromFile(string FileToRead, ref string readMessage)
{
try
{
readMessage = "";
if (!File.Exists(FileToRead))
{
readMessage = "File not found: " + FileToRead;
return false;
}
using (StreamReader r = new StreamReader(FileToRead))
{
readMessage = r.ReadToEnd();
}
return true;
}
catch (Exception ex)
{
readMessage = ex.Message;
return false;
}
}
It seems that you do not use filenames in your source code.
I made an example. File copy function.
public void SaveStockInfoToAnotherFile(string sPath, string dPath, string filename)
{
string sourcePath = sPath;
string destinationPath = dPath;
string sourceFile = System.IO.Path.Combine(sourcePath, filename);
string destinationFile = System.IO.Path.Combine(destinationPath, filename);
if (!System.IO.Directory.Exists(destinationPath))
{
System.IO.Directory.CreateDirectory(destinationPath);
}
System.IO.File.Copy(sourceFile, destinationFile, true);
}
//folderBrowserDialog1.ShowDialog();
//string xx = folderBrowserDialog1.SelectedPath;
//string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
//folderBrowserDialog1.ShowDialog();
//string yy = folderBrowserDialog1.SelectedPath;
//foreach (string file in files)
//{
// File.Copy(xx + "\\" + Path.GetFileName(file), yy + "\\" + Path.GetFileName(file));
HttpPostedFile file = context.Request.Files[j];
string fileName = file.FileName;
string fileExtension = System.IO.Path.GetExtension(filepath + file.FileName);
if (!string.IsNullOrEmpty(fileName)
{
string pathToSave_100 = HttpContext.Current.Server.MapPath(filepath) + fileName + fileExtension;
if (File.Exists(pathToSave_100))
{
File.Delete(pathToSave_100);
file.SaveAs(pathToSave_100);
}
else
{
file.SaveAs(pathToSave_100);
}
}
You can do like this:
string strFileExtension = Path.GetExtension(file.FileName);
Using below code, how can I set the code in SQLiteConnetion object ?
public SQLiteConnection dbConnection = new SQLiteConnection();
string fileName = "test.s3db";
string sourcePath = #"E:\File\DMS\DAL\Model";
string targetPath = #"C:\ProgramData\CompanyName\appName";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
System.IO.File.Copy(sourceFile, destFile, true);
if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);
}
I want to automatically create a DB path if the path does not exist.
dbConnection = ??
I have this method to copy database:
public static void BackupDatabase(string sourceFile, string destFile)
{
using (SQLiteConnection source = new SQLiteConnection(String.Format("Data Source = {0}", sourceFile)))
using (SQLiteConnection destination = new SQLiteConnection(String.Format("Data Source = {0}", destFile)))
{
source.Open();
destination.Open();
source.BackupDatabase(destination, "main", "main", -1, null, -1);
}
}
I am trying to upload files with same names to the server using GUID, but its not working and is still replacing the old files, can anybody help me by telling where I am making the mistake?
here is y code to upload:
protected void btnAddExpenditure_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string FileName = FileUpload1.PostedFile.FileName;
if (File.Exists(FileName))
{
FileName = Guid.NewGuid() + FileName;
}
//check file Extension & Size
int filesize = FileUpload1.PostedFile.ContentLength;
if (filesize > (20 * 1024))
{
Label1.Text = "Please upload a zip or a pdf file";
}
string fileextention = System.IO.Path.GetExtension(FileUpload1.FileName);
if (fileextention.ToLower() != ".zip" && fileextention.ToLower() != ".pdf")
{
Label1.ForeColor = System.Drawing.Color.Green;
Label1.Text = "Please upload a zip or a pdf file";
}
else
{
string ReceiptFileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
//save file to disk
FileUpload1.SaveAs(Server.MapPath("Reciepts/" + ReceiptFileName));
}
string FileName = FileUpload1.PostedFile.FileName;
if (File.Exists(FileName))
{
FileName = Guid.NewGuid() + FileName;
}
...
string ReceiptFileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
Here's your problem. You're creating a new string variable that holds the file name (FileName). If it exists, you modify FileName with a new GUID. But at the very end...
string ReceiptFileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
you're still using the original FileUpload1.PostedFile.FileName. This should be changed to
string ReceiptFileName = Path.GetFileName(FileName);
EDIT: Reading through the code again, I think you may have other problems as well. Assuming that FileUpload1.PostedFile.FileName is a full path (i.e. C:\Folder\File.txt), then
FileName = Guid.NewGuid() + FileName;
would result in something like 123-4321-GUIDC:\Folder\File.txt
I doubt that's what you want. You might want to flip that around
FileName = FileName + Guid.NewGuid();