Create files in multiple folders - c#

I've created a for loop that creates n folders. I would like to create a text file in each folder. How do i do it?
for (int i = 1; i < 17; i++)
{
System.IO.Directory.CreateDirectory(
String.Format(#"C:\Users\xxx\Desktop\xx\Test{0:d2}", i));
}
I found a better solution.
for (int i = 1; i < 17; i++)
{
Directory.CreateDirectory(String.Format(#"C:\Users\xxx\Desktop\xx\Test"+i, i));
if (!File.Exists(string.Format(#"C:\Users\xxx\Desktop\xx\Test{0}/Test.txt", i)))
{
File.WriteAllText(string.Format(#"C:\Users\xxx\Desktop\xx\Test{0}/Test.txt", i), " ");
}

Try this
for (int i = 1; i < 17; i++)
{
var folder = System.IO.Directory.CreateDirectory(String.Format(#"C:\Users\xxx\Desktop\xx\Test{0:d2}", i));
System.IO.File.WriteAllText(folder.FullName + #"\WriteText.txt", "your text content");
}
Update
if you want more than one file
for (int i = 1; i < 17; i++)
{
var folder = System.IO.Directory.CreateDirectory(String.Format(#"C:\Users\xxx\Desktop\xx\Test{0:d2}", i));
System.IO.File.WriteAllText(folder.FullName + #"\WriteText1.txt", "your text content 1");
System.IO.File.WriteAllText(folder.FullName + #"\WriteText2.txt", "your text content 2");
}

Try this:
var desktop_path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
for (int i = 1; i < 17; i++)
{
var folder_path = System.IO.Path.Combine(desktop_path, String.Format(#"xx\Test{0:d2}", i));
var file_path = System.IO.Path.Combine(folder_path, "file.txt");
System.IO.Directory.CreateDirectory(folder_path);
System.IO.File.WriteAllText(file_path, "content");
}
This code finds the current user's desktop path, and then uses System.IO.Path.Combine to ensure that paths are correctly concatenated together.

for (int i = 1; i < 17; i++)
{
var dir = System.IO.Directory.CreateDirectory
(String.Format(#"C:\Users\xxx\Desktop\xx\Test{0:d2}", i));
System.IO.File.Create(dir.FullName+ #"\MyFile.txt");
}
To create add content on the file, we can use FileStream object returned by File.Create()

Try this above,
I hope this may easy for you
string path = #"d:\\dummyfolder";
for (int i = 0; i < 17; i++)
{
string _folderPath = string.Format("{0}\\{1}", path, i);
if (!Directory.Exists(_folderPath))
{
//creating folder
Directory.CreateDirectory(_folderPath);
//creating text file
string _filePath = string.Format("{0}\\{1}\\{1}.txt", path, i);
string text = i + " " + "Content of the text file ";
File.WriteAllText(_filePath, text);
}
}

Related

How can I write a loop to set the value of sum[i] and delete datatable columns where sum[i] <= 0

int i = 0;
string[] sum = new string[256];
for (i = 1; i < 256; i++)
{
sum[i] = sum[i].ToString();
sum[i] = Convert.ToInt32(dt.Compute("Sum(BIN" + i.ToString() + ")", string.Empty));
if(sum[i] == 0)
{
dt.Columns.Remove("BIN"+i.ToString()+"");
}
}
There is a problem where you're trying to assign an int to sum[i], because sum is a string[], and you can't assign an int to one of the items.
Instead, it seems you want to get the return value from the method and save it in a variable so you can compare it. In order to do this, we can modify the code like:
string[] sum = new string[256];
for (int i = 1; i < 256; i++)
{
int result = Convert.ToInt32(dt.Compute("Sum(BIN" + i.ToString() + ")", string.Empty));
if (result <= 0)
{
dt.Columns.Remove("BIN" + i.ToString() + "");
}
}
This can be simplified further by using the return value of the method directly in the if condition. We can also use interpolated strings instead of concatenation:
string[] sum = new string[256];
for (i = 1; i < sum.Length; i++)
{
if(Convert.ToInt32(dt.Compute($"Sum(BIN{i})", string.Empty)) <= 0)
{
dt.Columns.Remove($"BIN{i}");
}
}

Read Date Taken of a video

Is it possible to read date taken of a video not the Created Date or Modified Date ... i tried to check the video details but i always see the Date Taken is Empty
List<string> arrHeaders = new List<string>();
Shell32.Shell shell = new Shell32.Shell();
Shell32.Folder objFolder;
objFolder = shell.NameSpace(directoryPath);
for (int i = 0; i < short.MaxValue; i++)
{
string header = objFolder.GetDetailsOf(null, i);
if (String.IsNullOrEmpty(header))
break;
arrHeaders.Add(header);
}
foreach (Shell32.FolderItem2 item in objFolder.Items())
{
for (int i = 0; i < arrHeaders.Count; i++)
{
Console.WriteLine("{0}\t{1}: {2}", i, arrHeaders[i], objFolder.GetDetailsOf(item, i));
}
}

text files with string arrays get position substring

I am having a problem with the following:
I am loading a file into C# and then I am splitting it by lines with this code.
// Splitting by line from original file
string[] lines = showText.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
Now I need a for loop that will go through lines and get Substring from those lines, separately.
Here is what I am trying to accomplish, in a way:
for (int i = 0; i < lines.Length; i++)
{
int[] testing = new int[i];
testing[i] = int.Parse(lines[i].Substring(16, 1));
textBox1.Text = testing.ToString();
}
The error here is: Index was outside the bounds of the array.
Here is a picture also to get better idea as to what I'm trying to do.
http://s30.postimg.org/jbmjmqv1t/work.jpg
textBox1.Text = lines[0].Substring(16,1) + " " + lines[0].Substring(23,9);
textBox1.Text = lines[1].Substring(16,1) + " " + lines[1].Substring(23,9); //etc
Could anyone help me with this?
You are creating the array in the for loop, so it is being created for each line and with the wrong length. Instead of this part of the code:
for (int i = 0; i < lines.Length; i++)
{
int[] testing = new int[i];
testing[i] = int.Parse(lines[i].Substring(16, 1));
textBox1.Text = testing.ToString();
}
you should be doing this:
int[] testing = new int[lines.Length];
for (int i = 0; i < lines.Length; i++)
{
testing[i] = int.Parse(lines[i].Substring(16, 1));
textBox1.Text = testing.ToString();
}
This is how I've solved it.
int[] testing = new int[lines.Length];
textBox1.Clear(); //Just to clear it if button pressed again
for (int i = 0; i < lines.Length; i++)
{
testing[i] = int.Parse(lines[i].Substring(16, 1));//just getting the needed value
textBox1.Text += testing[i].ToString() + "\r\n" ;//adding each value to textBox1, separated by new line
}

How to delete Memory mapped file?

I work with memory-mapped files and after I finish my work I want to delete the files from the disk. but I have the UnauthorizedAccessException in File.Delete(fileName); I've read here that I should use "using", but i work with several files so I have the array of MemoryMappedFileAccessor's.
my code:
var mmf_acc_array = new MemoryMappedViewAccessor[numFiles];
var size = 1048576; //1Mb
var mmf = new MemoryMappedFile[numFiles];
for (int i = 0; i < numFiles; i++)
{
mmf[i] = MemoryMappedFile.CreateFromFile(Path.Combine("tmp", "tmp" + i.ToString()));
mmf_acc_array[i] = mmf[i].CreateViewAccessor(0, size);
}
do sm work
for (int i = 0; i < numFiles; i++)
{
mmf_acc_array[i].Dispose();
mmf[i].Dispose();
File.Delete(Path.Combine("tmp", "tmp" + i.ToString()));
}
Exception arises in File.Delete(); How can I free the file?

Passing array of strings from c# to javascript

I am building two arrays in c# and pass them to a js function like this:
//call js to show the map with the markers
string[] lats = new string[10];
string[] longs = new string[10];
for (int i = 0; i < 10; i++)
{
lats[i] = dv[i]["Latitude"].ToString();
}
for (int i = 0; i < 10; i++)
{
longs[i] = dv[i]["Longitude"].ToString();
}
StringBuilder sbLats = new StringBuilder();
string[] latsArray = lats.ToArray<string>();
//Build the JS array.
sbLats.Append("[");
for (int i = 0; i < latsArray.Length; i++)
{
sbLats.AppendFormat("'{0}', ", latsArray[i]);
}
sbLats.Append("]");
StringBuilder sbLongs = new StringBuilder();
string[] longsArray = longs.ToArray<string>();
//Build the JS array.
sbLongs.Append("[");
for (int i = 0; i < longs.Length; i++)
{
sbLongs.AppendFormat("'{0}', ", longsArray[i]);
}
sbLongs.Append("]");
ScriptManager.RegisterStartupScript(this, this.GetType(), "mapMarket", "buildMapWithMarkers('map_market', " + latsArray + ", " + longsArray + ", " + "false" + ");", true);
For some unknown reason this throws an exception here (in the aspx page, part of generated js):
buildMapWithMarkers('map_market', System.String[], System.String[], false)
which says:
Uncaught SyntaxError: Unexpected token ]
Can you please tell me where I am wrong?
Solved it using #Skilwz suggestion (JavaScriptSerializer):
//call js to show the map with the markers
string[] lats = new string[10];
string[] longs = new string[10];
for (int i = 0; i < 10; i++)
{
lats[i] = dv[i]["Latitude"].ToString();
}
for (int i = 0; i < 10; i++)
{
longs[i] = dv[i]["Longitude"].ToString();
}
string serializedLat = (new JavaScriptSerializer()).Serialize(lats);
string serializedLong = (new JavaScriptSerializer()).Serialize(longs);
ScriptManager.RegisterStartupScript(this, this.GetType(), "mapMarket", "buildMapWithMarkers('map_market', " + serializedLat + ", " + serializedLong + ", " + "false" + ");", true);

Categories

Resources