I am trying to download you tube video using c# code but i am not getting proper code. I have searched many link but din't get any proper links and code.
I want to download the you tube video in my local folder with c# code. I have tried one link but that code just getting null video in my local folder so any one have the idea how can do that.
Below is the code i have tried so far.
var VedioUrl = "https://www.youtube.com/embed/" + objYouTube.VideoID + ".mp4";
WebRequest MyRequest = HttpWebRequest.Create(VedioUrl);
WebResponse MyResponse = MyRequest.GetResponse();
string RealURL = MyResponse.ResponseUri.ToString();
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(RealURL);
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Stream receiveStream = myHttpWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(receiveStream, encode);
StreamWriter writer = new StreamWriter(Server.MapPath("~/youtube/" + objYouTube.VideoID + ".mp4"), true);
writer.Write(readStream.ReadToEnd());
writer.Close();
so here is my video url which i am trying to download : "https://www.youtube.com/embed/UCsiNPbLbwZk43FOCRrdKBlA.mp4"
I have find the solution for download you tube videos using c# code.
First need install the "libvideo" on NuGet package manager console in visual studio.
Here Fire this command on package manger console :
Install-Package VideoLibrary
First add this namespace on top in your controller :
using VideoLibrary;
Now here just write just code and pass the url link :
var VedioUrl = "https://www.youtube.com/embed/" + objYouTube.VideoID + ".mp4";
var youTube = YouTube.Default;
var video = youTube.GetVideo(VedioUrl);
System.IO.File.WriteAllBytes(Server.MapPath("~/youtube/" + video.FullName + ".mp4"), video.GetBytes());
You can embed youtube-dl in your app.
It provides extensive Youtube download options.
Basically, you do something like this.
using System;
using System.Diagnostics;
using System.ComponentModel;
namespace MyProcessSample
{
class MyProcess
{
public static void Main()
{
Process myProcess = new Process();
try
{
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = #"yourpath\youtube-dl.exe";
myProcess.StartInfo.CreateNoWindow = false;
myProcess.StartInfo.Arguments = "https://www.youtube.com/watch?v=KFqrp4KSxio";
myProcess.Start();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
You can use this C# wapper for youtube-dl.
You can extend it to suite your needs.
Process Class
For a more up to date solution, check out YoutubeExplode. It offers rich API to query and download Youtube videos and, unlike other libraries, is still actively maintained.
after install the "libvideo" on NuGet package manager console in visual studio.
and download ffmpeg for merge audio and video for best output .
follow this code :
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using VideoLibrary;
namespace you_tube_download
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("write url video : ");
string url = Console.ReadLine();
string information = "";
var videos = YouTube.Default.GetAllVideos(url);
int hightaudio = 1;
int hightvideo = 1;
Console.WriteLine("\nlist all format \n");
foreach (var item in videos)//write all file on this url
{
Console.WriteLine(item.Resolution+","+item.Format + "," + item.AudioFormat + "," + item.AudioBitrate + "," + item.ContentLength + "," + item.AdaptiveKind);
if (item.AdaptiveKind.ToString() == "Audio" && item.AudioBitrate > hightaudio)
{
hightaudio = item.AudioBitrate;
information = item.AudioFormat + "," + item.AudioBitrate+","+item.ContentLength;
}
if (item.Resolution > hightvideo)
{
hightvideo = item.Resolution;
}
}
Console.WriteLine("\ndownload high video resolotion {0} and high audio bitrate {1}",hightvideo,hightaudio);
string[] split = information.Split(',');
foreach (var item in videos)//download audio
{
if (split[0]== item.AudioFormat.ToString() && split[1] == item.AudioBitrate.ToString() && split[2] == item.ContentLength.ToString())
{
Console.WriteLine("\ndownload audio with bitrate {0} and size {1}MB",item.AudioBitrate, Math.Round((double)item.ContentLength / 1000000, 2));
downloadbest(item, Directory.GetCurrentDirectory() + "\\file123456798.mp3");
Console.Write("end\n");
}
}
foreach (var item in videos)//download video
{
if (item.Resolution==hightvideo)
{
Console.WriteLine("\ndownload video with Resolution {0} and size {1}MB", item.Resolution, Math.Round((double)item.ContentLength/1000000,2));
downloadbest(item, Directory.GetCurrentDirectory() + "\\file123456798.mp4");
Console.Write("end\n");
break;
}
}
Console.WriteLine("wait for marge");
combine();
File.Delete(Directory.GetCurrentDirectory() + "\\file123456798.mp3");
File.Delete(Directory.GetCurrentDirectory() + "\\file123456798.mp4");
Console.WriteLine("press any key to continue...");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine("\n\n\n\n" + ex);
Console.ReadKey();
}
Process.Start(Directory.GetCurrentDirectory());
}
static void combine()
{
Process p = new Process();
p.StartInfo.FileName = "ffmpeg.exe";
p.StartInfo.Arguments = "-i \"" + Directory.GetCurrentDirectory() + "\\file123456798.mp4\" -i \"" + Directory.GetCurrentDirectory() + "\\file123456798.mp3\" -preset veryfast \"" + Directory.GetCurrentDirectory() + "\\final.mp4\"";
p.Start();
p.WaitForExit();
}
static void downloadbest(YouTubeVideo y, string patch)
{
int total = 0;
FileStream fs =null;
Stream streamweb = null;
WebResponse w_response = null;
try
{
WebRequest w_request = WebRequest.Create(y.Uri);
if (w_request != null)
{
w_response = w_request.GetResponse();
if (w_response != null)
{
fs = new FileStream(patch, FileMode.Create);
byte[] buffer = new byte[128*1024];
int bytesRead = 0;
streamweb = w_response.GetResponseStream();
Console.WriteLine("Download Started");
do
{
bytesRead = streamweb.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, bytesRead);
total += bytesRead;
Console.Write($"\rDownloading ({Math.Round(((double)total/(int)y.ContentLength) * 100, 2)}%) {total}/{y.ContentLength} ");
} while (bytesRead > 0);
Console.WriteLine("\nDownload Complete");
}
}
}
catch (Exception ex)
{
Console.WriteLine("\n\n\n\n" + ex);
Console.ReadKey();
Process.Start(Directory.GetCurrentDirectory());
}
finally
{
if (w_response != null) w_response.Close();
if (fs != null) fs.Close();
if (streamweb != null) streamweb.Close();
}
}
}
}
Related
I'm doing a c# wcf service in which I receive a bunch of images and the service merge them in a multiimage Tiff file. At the end of the service I want to delete the original files but I'm receiving an error that some other process is locking the file.
This is the code that receives the images (as a byte[] list) and write them to disk
public static List<string> SaveByteImagesToFile(List<byte[]> bytesToCopyIntoFiles, string imageReferenceType, string imageReferenceValue)
{
_applicationLogger.Debug(MethodBase.GetCurrentMethod().DeclaringType.Name, MethodBase.GetCurrentMethod().Name);
string imageFinalPath = string.Empty;
string joinImagesFilePath = string.Empty;
List<string> imagesFilePath = new List<string>();
int count = 1;
try
{
if (bytesToCopyIntoFiles.Count == 0)
{
throw new ArgumentNullException("bytesToCopyIntoFiles");
}
else
{
joinImagesFilePath = SettingsManager.GetServiceSetting(AppSettingsKeys.CopyImagesToFilePath, "NO_VALID_FILEPATH");
if (joinImagesFilePath.IsValidFilePath(out string errorMessage, true, true))
{
foreach (byte[] image in bytesToCopyIntoFiles)
{
var imageFileName = imageReferenceType + "_" + imageReferenceValue + "_" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + count.ToString();
imageFinalPath = joinImagesFilePath + Path.DirectorySeparatorChar + imageFileName + ".tiff";
using (FileStream stream = new FileStream(imageFinalPath, FileMode.Create, FileAccess.ReadWrite))
{
stream.Write(image, 0, image.Length);
stream.Flush();
}
imagesFilePath.Add(imageFinalPath);
count++;
}
}
else
{
exceptionMessageType = MainRepository.GetExceptionMessage("E171");
throw new IOException(exceptionMessageType.ExceptionMessage + " " + errorMessage);
}
}
return imagesFilePath;
}
catch
{
throw;
}
}
How or what can I use to prevent the service or any process to lock the file. As you can see I'm using the using scope for filestream without any luck.
Any ideas? Thanks
Resolved! By organizing the files in a certain order, when creating the multipage tiff, by the time the logic ends the worker already unlock the resources and I'm able now to delete them without any issue.
I have a list of mp3 which I am downloading. After some files are downloaded, not all of them - around 5-7, I get WebException. I did a stacktrace and this is the result.
Exception thrown: 'System.Net.WebException' in System.dll
Debug message: The operation has timed out
InnerEx: at System.Net.HttpWebRequest.GetResponse()
at iBlock.Main._InetGetHTMLSearch(String sArtist) in C:\Users\...\Main.cs:line 590
My _InetGetHTMLSearch looks like this
private void _InetGetHTMLSearch(string sArtist)
{
aLinks.Clear();
if (AudioDumpQuery == string.Empty)
{
//return string.Empty;
}
string[] sStringArray;
string sResearchURL = "http://www.audiodump.biz/music.html?" + AudioDumpQuery + sArtist.Replace(" ", "+");
string aRet;
HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(sResearchURL);
webReq.UserAgent = "Mozilla / 5.0(Macintosh; Intel Mac OS X 10_9_3) AppleWebKit / 537.75.14(KHTML, like Gecko) Version / 7.0.3 Safari / 7046A194A";
webReq.Referer = "http://www.audiodump.com/";
webReq.Timeout = 5000;
try
{
webReq.CookieContainer = new CookieContainer();
webReq.Method = "GET";
using (WebResponse response = webReq.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream);
aRet = reader.ReadToEnd();
//Console.WriteLine(aRet);
string[] aTable = _StringBetween(aRet, "<BR><table", "table><BR>", RegexOptions.Singleline);
if (aTable != null)
{
string[] aInfos = _StringBetween(aTable[0], ". <a href=\"", "<a href=\"");
if (aInfos != null)
{
for (int i = 0; i < aInfos.Length; i++)
{
//do some magic here
}
}
else
{
//debug
}
}
else
{
//debug 2
}
}
response.Dispose();
}
}
catch (Exception ex)
{
Console.WriteLine("Debug message: " + ex.Message + "InnerEx: " + ex.StackTrace);
aLinks.Clear();
return;
//throw exception
}
}
what this method does is simple. A simple search of the sArtist given at audiodump.com
I have a timer which runs very fast, every 10ms.
private void MainTimer_Tick(object sender, EventArgs e)
{
_DoDownload(DoubleDimList[i][y], ref mp3ToPlay);
if (muted) Mute(0);
if (Downloading)
{
StatusLabel.Text = "Downloading: " + DoubleDimList[i][y];
}
}
Now this timer handles the download in the background in a Global scope.
The _DoDownload methos which basically starts the entire process looks like this
private void _DoDownload(string dArtist, ref string dPath)
{
if (!Contain && skip <= 3 && !Downloading)
{
try
{
_InetGetHTMLSearch(dArtist);
if (aLinks.Count < 1)
{
//skip and return
Console.WriteLine("Skipping: " + dArtist);
IniWriteValue(_playlists[i], "Track " + y, dArtist + " -iBlockSkip");
y++;
return;
}
string path = mp3Path + "\\" + dArtist + ".mp3";
if (DownloadOne(aLinks[0], path, false))
{
hTimmer.Start();
Downloading = true;
}
}
catch (Exception Ex)
{
MessageBox.Show("Download start error: " + Ex.Message);
}
}
else if (Downloading)
{
try {
int actualBytes = strm.Read(barr, 0, arrSize);
fs.Write(barr, 0, actualBytes);
bytesCounter += actualBytes;
double percent = 0d;
if (fileLength > 0)
percent =
100.0d * bytesCounter /
(preloadedLength + fileLength);
label1.Text = Math.Round(percent) + "%";
if (Math.Round(percent) >= 100)
{
string path = mp3Path + "\\" + dArtist + ".mp3";
label1.Text = "";
dPath = path;
aLinks.Clear();
hTimmer.Stop();
hTimmer.Reset();
fs.Flush();
fs.Close();
lastArtistName = "N/A";
Downloading = false;
y++;
if (y >= DoubleDimList[i].Count)
{
i++;
}
}
if (Math.Round(percent) <= 1)
{
if (hTimmer.ElapsedMilliseconds >= 3000)
{
string path = mp3Path + "\\" + dArtist + ".mp3";
hTimmer.Stop();
hTimmer.Reset();
fs.Flush();
fs.Close();
System.IO.File.Delete(path);
Contain = false;
skip += 1;
Downloading = false;
}
} }
catch(Exception Ex)
{
MessageBox.Show("Downloading error: " + Ex.Message);
}
}
}
Now once the exception is thrown it messes up the entire project. As you see in the last method, if _InetGetHTMLSearch doesn't update the search(returns nothing) I am skipping and moving to next search. However the exception will be thrown in every next search. I tried setting new cookies in every search but still didn't work.
Any solutions how to avoid this issue?
P.S. I have to say that if I change the timer's Interval to 500ms it will download more mp3 before the exception is thrown but not all of them.
Edit: The issue here is obvious. The request timesout but even if I set it to Timeout.Infinite it will hand there forever
I'm trying to run a Python script from C# as a stream, and to repeatedly pass inputs and outputs between Python and the stream, using StreamWriter and StreamReader.
I can read and write, but apparently only once, and not multiple times. (Which is what I need.)
Hopefully, somebody can tell me what I'm doing wrong.
(I'm aware that I can probably do what I need to do by reading and writing to a file. However, I'd like to avoid this if I can, since using the Stream seems cleaner.)
Here's my C# code:
using System;
using System.Diagnostics;
using System.IO;
public class Stream_Read_Write
{
public static void Main()
{
string path = "C:\\Users\\thomas\\Documents\\Python_Scripts\\io_test.py";
string iter = "3";
string input = "Hello!";
stream_read_write(path, iter, input);
//Keep Console Open for Debug
Console.Write("end");
Console.ReadKey();
}
private static void stream_read_write(string path, string iter, string input)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "C:\\Python27\\python.exe";
start.Arguments = string.Format("{0} {1}", path, iter);
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
start.RedirectStandardInput = true;
start.CreateNoWindow = true;
using (Process process = Process.Start(start))
using (StreamWriter writer = process.StandardInput)
using (StreamReader reader = process.StandardOutput)
{
for (int i = 0; i < Convert.ToInt32(iter); i++)
{
Console.WriteLine("writing...");
writer.WriteLine(input);
writer.Flush();
Console.WriteLine("written: " + input + "\n");
Console.WriteLine("reading...");
string result = reader.ReadLine();
Console.WriteLine("read: " + result + "\n");
}
}
}
}
The Python code looks like this:
import sys
iter = int(sys.argv[1])
for i in range(iter):
input = raw_input()
print (input)
And this is the output that I get:
writing...
written: Hello!
reading...
Strangely, when I remove the loops from both Python and C#, it works.
(For one iteration)
writing...
written: Hello!
reading...
read: Hello!
end
It's not clear to me why this happens, or what the solution could be, so any help is much appreciated.
I found a solution to my problem. I'm not really sure why this works, though.
For C#:
using System;
using System.Diagnostics;
using System.IO;
public class Stream_Read_Write
{
public static void Main()
{
string path = "C:\\Users\\thomas_wortmann\\Documents\\Python_Scripts\\io_test.py";
string iter = "3";
string input = "Hello";
stream_read_write(path, iter, input);
//Keep Console Open for Debug
Console.Write("end");
Console.ReadKey();
}
private static void stream_read_write(string path, string iter, string input)
{
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = "C:\\Python27\\python.exe";
start.Arguments = string.Format("{0} {1}", path, iter);
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
start.RedirectStandardInput = true;
start.CreateNoWindow = true;
using (Process process = Process.Start(start))
using (StreamWriter writer = process.StandardInput)
using (StreamReader reader = process.StandardOutput)
{
for (int i = 0; i < Convert.ToInt32(iter); i++)
{
writer.WriteLine(input + i);
Console.WriteLine("written: " + input + i);
string result = null;
while (result == null || result.Length == 0)
{ result = reader.ReadLine(); }
Console.WriteLine("read: " + result + "\n");
}
}
}
}
And the python code looks like this:
import sys
def reverse(input):
return input [::-1]
iter = int(sys.argv[1])
for i in range(iter):
input = sys.stdin.readline()
print reverse(input)
sys.stdout.flush()
This is a conventional stream problem.
You have to “flush” the stream
(e.g. stdout.flush() , where stdout is a stream object)
in order to send the data
no matter in C# or in Python.
In C#, as I know, you have to execute stream.Close()
to complete the flush itself,
or you can wrap the stream with “using”
and it send the data when the brackets is closed.
Edit:
Btw, the stream is only available to one side at a time.
I'm trying to extract an ISO to a folder with the same name without .iso on the end.
I'm having a problem with winrar as it will not start the extract when I start up with the seach starting in the folder with the ISO.
UPDATED with answer code
private void ExtractISO(string toExtract, string folderName)
{
// reads the ISO
CDReader Reader = new CDReader(File.Open(toExtract, FileMode.Open), true);
// passes the root directory the folder name and the folder to extract
ExtractDirectory(Reader.Root, folderName /*+ Path.GetFileNameWithoutExtension(toExtract)*/ + "\\", "");
// clears reader and frees memory
Reader.Dispose();
}
private void ExtractDirectory(DiscDirectoryInfo Dinfo, string RootPath, string PathinISO)
{
if (!string.IsNullOrWhiteSpace(PathinISO))
{
PathinISO += "\\" + Dinfo.Name;
}
RootPath += "\\" + Dinfo.Name;
AppendDirectory(RootPath);
foreach (DiscDirectoryInfo dinfo in Dinfo.GetDirectories())
{
ExtractDirectory(dinfo, RootPath, PathinISO);
}
foreach (DiscFileInfo finfo in Dinfo.GetFiles())
{
using (Stream FileStr = finfo.OpenRead())
{
using (FileStream Fs = File.Create(RootPath + "\\" + finfo.Name)) // Here you can Set the BufferSize Also e.g. File.Create(RootPath + "\\" + finfo.Name, 4 * 1024)
{
FileStr.CopyTo(Fs, 4 * 1024); // Buffer Size is 4 * 1024 but you can modify it in your code as per your need
}
}
}
}
static void AppendDirectory(string path)
{
try
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
catch (DirectoryNotFoundException Ex)
{
AppendDirectory(Path.GetDirectoryName(path));
}
catch (PathTooLongException Ex)
{
AppendDirectory(Path.GetDirectoryName(path));
}
}
The user selects the folder to extract (.ISO) toExtract. I then use it in the Process.Start() in the background worker. That just seems to open the mounting software and doesn't extract the ISO to the desired folder name.
Thanks in advance for your help.
Or if anyone could give me a batch to extract the ISO instead and to call it from c# passing toExtract and the folder name that would be helpful too.
Thanks
If external Class Libraries are OK!
Then use SevenZipSharp or .NET DiscUtils to extract ISO's...
These two ClassLibraries can manage ISO and Extract them!
For DiscUtils you can find some codes for ISO Management [CDReader Class] at the Link I provided.
But For SevenZipSharp, Please Explore the ClassLibrary source and find the Code to Extract or Google to find it!
To get the Name of the folder just use Path.GetFileNameWithoutExtension((string)ISOFileName) which will return "ISOFile" for an iso named "ISOFile.iso". And then you can use it with your desired path.
UPDATE
Code To Extract ISO Image with DiscUtils :
using DiscUtils;
using DiscUtils.Iso9660;
void ExtractISO(string ISOName, string ExtractionPath)
{
using (FileStream ISOStream = File.Open(ISOName, FileMode.Open))
{
CDReader Reader = new CDReader(ISOStream, true, true);
ExtractDirectory(Reader.Root, ExtractionPath + Path.GetFileNameWithoutExtension(ISOName) + "\\", "");
Reader.Dispose();
}
}
void ExtractDirectory(DiscDirectoryInfo Dinfo, string RootPath, string PathinISO)
{
if (!string.IsNullOrWhiteSpace(PathinISO))
{
PathinISO += "\\" + Dinfo.Name;
}
RootPath += "\\" + Dinfo.Name;
AppendDirectory(RootPath);
foreach (DiscDirectoryInfo dinfo in Dinfo.GetDirectories())
{
ExtractDirectory(dinfo, RootPath, PathinISO);
}
foreach (DiscFileInfo finfo in Dinfo.GetFiles())
{
using (Stream FileStr = finfo.OpenRead())
{
using (FileStream Fs = File.Create(RootPath + "\\" + finfo.Name)) // Here you can Set the BufferSize Also e.g. File.Create(RootPath + "\\" + finfo.Name, 4 * 1024)
{
FileStr.CopyTo(Fs, 4 * 1024); // Buffer Size is 4 * 1024 but you can modify it in your code as per your need
}
}
}
}
static void AppendDirectory(string path)
{
try
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
catch (DirectoryNotFoundException Ex)
{
AppendDirectory(Path.GetDirectoryName(path));
}
catch (PathTooLongException Exx)
{
AppendDirectory(Path.GetDirectoryName(path));
}
}
Use It with Like This :
ExtractISO(ISOFileName, Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\");
Working! Tested By Me!
And Of Course You can always add more Optimization to the code...
This Code is Just a Basic One!
For UDF or for making Windows ISO Files after servicing(DISM) with out needs the above accepted answer is not working for me so i tried this working method with DiscUtils
using DiscUtils;
public static void ReadIsoFile(string sIsoFile, string sDestinationRootPath)
{
Stream streamIsoFile = null;
try
{
streamIsoFile = new FileStream(sIsoFile, FileMode.Open);
DiscUtils.FileSystemInfo[] fsia = FileSystemManager.DetectDefaultFileSystems(streamIsoFile);
if (fsia.Length < 1)
{
MessageBox.Show("No valid disc file system detected.");
}
else
{
DiscFileSystem dfs = fsia[0].Open(streamIsoFile);
ReadIsoFolder(dfs, #"", sDestinationRootPath);
return;
}
}
finally
{
if (streamIsoFile != null)
{
streamIsoFile.Close();
}
}
}
public static void ReadIsoFolder(DiscFileSystem cdReader, string sIsoPath, string sDestinationRootPath)
{
try
{
string[] saFiles = cdReader.GetFiles(sIsoPath);
foreach (string sFile in saFiles)
{
DiscFileInfo dfiIso = cdReader.GetFileInfo(sFile);
string sDestinationPath = Path.Combine(sDestinationRootPath, dfiIso.DirectoryName.Substring(0, dfiIso.DirectoryName.Length - 1));
if (!Directory.Exists(sDestinationPath))
{
Directory.CreateDirectory(sDestinationPath);
}
string sDestinationFile = Path.Combine(sDestinationPath, dfiIso.Name);
SparseStream streamIsoFile = cdReader.OpenFile(sFile, FileMode.Open);
FileStream fsDest = new FileStream(sDestinationFile, FileMode.Create);
byte[] baData = new byte[0x4000];
while (true)
{
int nReadCount = streamIsoFile.Read(baData, 0, baData.Length);
if (nReadCount < 1)
{
break;
}
else
{
fsDest.Write(baData, 0, nReadCount);
}
}
streamIsoFile.Close();
fsDest.Close();
}
string[] saDirectories = cdReader.GetDirectories(sIsoPath);
foreach (string sDirectory in saDirectories)
{
ReadIsoFolder(cdReader, sDirectory, sDestinationRootPath);
}
return;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
it has extracted from a application source ISOReader but modified for my requirements
total source is available at http://www.java2s.com/Open-Source/CSharp_Free_CodeDownload/i/isoreader.zip
Try this:
string Desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Process.Start("Winrar.exe", string.Format("x {0} {1}",
Desktop + "\\test.rar",
Desktop + "\\SomeFolder"));
That would extract the file test.rar to the folder SomeFolder. You can change the .rar extention to .iso, it'll work the same.
As far as I can see in your current code, there is no command given to extract a file, and no path to the file that has to be extracted. Try this example and let me know if it works =]
P.S. If you'd like to hide the extracting screen, you can set the YourProcessInfo.WindowStyle to ProcessWindowStyle.Hidden.
I hace confrunted recently with this kind of .iso extraction issue. After trying several methods, 7zip did the job for me, you just have to make sure that the latest version of 7zip is installed on your system. Maybe it will help
try
{
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = false;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
cmd.Start();
cmd.StandardInput.WriteLine("C:");
//Console.WriteLine(cmd.StandardOutput.Read());
cmd.StandardInput.Flush();
cmd.StandardInput.WriteLine("cd C:\\\"Program Files\"\\7-Zip\\");
//Console.WriteLine(cmd.StandardOutput.ReadToEnd());
cmd.StandardInput.Flush();
cmd.StandardInput.WriteLine(string.Format("7z x -y -o{0} {1}", source, copyISOLocation.TempIsoPath));
//Console.WriteLine(cmd.StandardOutput.ReadToEnd());
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
Console.WriteLine(cmd.StandardOutput.ReadToEnd());
}
catch (Exception e)
{
Console.WriteLine(e.Message + "\n" + e.StackTrace);
if (e.InnerException != null)
{
Console.WriteLine(e.InnerException.Message + "\n" + e.InnerException.StackTrace);
}
}
I asked guru but I still couldn't solve the problem that I have.
I want to write a console program searching certain files, like xls, doc or *pdf.
I wrote a code like this but when it comes to the say, Users Directory, it cates UnauthorizedAccessException.
How can I write a console application which can search Users Directory?
I set clickonce off and build it with manifest which requireAdministrator.
So, on Vista or 7, it runs as an administrator, with the elevation dialogue.
Here's the full code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication2
{
class Program
{
//
private const string FILE_NAME = "search.txt";
private const string SEARCH_WORDS1 = "*.doc";
private const string SEARCH_WORDS2 = "*.ppt";
private const string SEARCH_WORDS3 = "*.jtd";
private const string SEARCH_WORDS4 = "*.pdf";
private const string END_WORDS = "\r\nSearch is finished.\r\n";
//This funcion echoes the messages.
void FileCheck()
{
string echo_words = "\r\nNow starts searching these files!" + SEARCH_WORDS1 + " "
+ SEARCH_WORDS2 + " " + SEARCH_WORDS3 + " " + SEARCH_WORDS4 + " "
+ "!\r\n";
if (File.Exists(FILE_NAME))
{
Console.WriteLine("{0} is already exists. Replace it to the new one.", FILE_NAME);
Console.WriteLine(echo_words);
File.Delete(FILE_NAME);
using (StreamWriter sw = File.CreateText(FILE_NAME))
{
sw.WriteLine(FILE_NAME + " is already exists. Replace it to the new one.\r\n");
sw.WriteLine(echo_words);
sw.Close();
}
}
else
{
using (StreamWriter sw = File.CreateText(FILE_NAME))
{
Console.WriteLine(echo_words);
sw.WriteLine(echo_words);
sw.Close();
}
}
}
//This function write to a file that search is finished.
void EndMessage()
{
using (StreamWriter sw = File.AppendText(FILE_NAME))
{
Console.WriteLine(END_WORDS);
sw.WriteLine(END_WORDS);
sw.Close();
}
}
//This function searches files given and write to a file.
void DirSearch(string sDir, string SEARCH_WORDS, int row)
{
int i;
i = 0;
string DeviceError = "off";
try
{
foreach (var d in Directory.GetDirectories(sDir))
{
DirectoryInfo di = new DirectoryInfo(d);
if ((di.Attributes & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint) {
//ReparsePoint could not be serached
continue;
}
try
{
foreach (string file in Directory.GetFiles(d, SEARCH_WORDS, SearchOption.AllDirectories))
{
Console.WriteLine(file);
using (StreamWriter sw = File.AppendText(FILE_NAME))
{
sw.WriteLine(file);
sw.Close();
i++;
}
}
}
catch (UnauthorizedAccessException)
{
//Unauthorized
Console.WriteLine(d + " is not allowd to be read !!");
using (StreamWriter sw = File.AppendText(FILE_NAME))
{
sw.WriteLine(d + " is not allowd to be read");
sw.Close();
}
}
}
}
catch (IOException)
{
//Device is not ready
DeviceError = "on";
}
if (DeviceError == "off")
{
if (i > 0)
{
Console.WriteLine(i + "numbers " + SEARCH_WORDS + " Files were found!\r\n");
using (StreamWriter sw = File.AppendText(FILE_NAME))
{
sw.WriteLine(i + "numbers " + SEARCH_WORDS + " Files were found!\r\n");
sw.Close();
}
}
else
{
Console.WriteLine(SEARCH_WORDS + " Files were not found !\r\n");
using (StreamWriter sw = File.AppendText(FILE_NAME))
{
sw.WriteLine(SEARCH_WORDS + " Files were not found !\r\n");
sw.Close();
}
}
}
}
//Main
static void Main(string[] args)
{
Program x = new Program();
string[] drives = Environment.GetLogicalDrives();
int row = drives.GetLength(0);
string my_documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Console.WriteLine("Logical Drives are " + row + ".");
using (StreamWriter sw = File.AppendText(FILE_NAME))
{
sw.WriteLine("Logical Drives are " + row + ".");
sw.Close();
}
int i = 0;
x.FileCheck();
while (row > 0)
{
x.DirSearch(drives[i], SEARCH_WORDS1, row);
x.DirSearch(drives[i], SEARCH_WORDS2, row);
x.DirSearch(drives[i], SEARCH_WORDS3, row);
x.DirSearch(drives[i], SEARCH_WORDS4, row);
row--;
i++;
}
x.EndMessage();
}
}
}
The error you're getting is caused by the file system permissions. The only way around would be to grant the credentials you're using access to the specified folders, run the application as 'Administrator' or run the application as the specific user for each User's folder.